Skip to content

Commit

Permalink
Add migration to set external ID's
Browse files Browse the repository at this point in the history
This also produces an error if entities would get an external ID that is already in use somewhere else.
  • Loading branch information
nickygerritsen committed Jun 4, 2024
1 parent 2d6c5f1 commit f0b4771
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions webapp/migrations/Version20240604202419.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240604202419 extends AbstractMigration
{
public function getDescription(): string
{
return 'Set external ID\'s for entities that don\'t have them';
}

public function up(Schema $schema): void
{
$djPrefixed = [
'contest' => 'cid',
'language' => 'langid',
'problem' => 'probid',
'team' => 'teamid',
'team_affiliation' => 'affilid',
'team_category' => 'categoryid',
];
$notPrefixed = [
'clarification' => 'clarid',
'submission' => 'submitid',
'user' => 'username',
];

foreach ($djPrefixed as $table => $column) {
$this->setExternalIds($table, $column, 'dj-');
}

foreach ($notPrefixed as $table => $column) {
$this->setExternalIds($table, $column);
}
}

protected function setExternalIds(string $table, string $column, string $prefix = '')
{
$entries = $this->connection->fetchAllAssociative("SELECT $column FROM $table WHERE externalid IS NULL");
foreach ($entries as $entry) {
$newExternalId = $prefix . $entry[$column];
// Check if any entity already has this external ID
$existingEntity = $this->connection->fetchAssociative("SELECT externalid FROM $table WHERE externalid = :externalid", ['externalid' => $newExternalId]);
$humanReadableTable = ucfirst(str_replace('_', ' ', $table));
$this->abortIf((bool)$existingEntity, "$humanReadableTable entity with external ID $newExternalId already exists, manually set a different external ID");
$this->addSql("UPDATE $table SET externalid = :externalid WHERE $column = :$column", [
'externalid' => $newExternalId,
$column => $entry[$column],
]);
}
}

public function down(Schema $schema): void
{
// No down migration needed
}

public function isTransactional(): bool
{
return false;
}
}

0 comments on commit f0b4771

Please sign in to comment.