-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This also produces an error if entities would get an external ID that is already in use somewhere else.
- Loading branch information
1 parent
2d6c5f1
commit f0b4771
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |