Skip to content

Commit

Permalink
Update setColumnNotNullable method to drop constraint if already exis…
Browse files Browse the repository at this point in the history
…ts (#4)

When setColumnNotNullable execution timed out, the retry mechanism is
blocked because the constraint is already created.

We should drop the constraint before being able to add it again.
  • Loading branch information
ndousson authored Jan 24, 2024
2 parents b291973 + 947214e commit 4de40e9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Doctrine/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ protected function setColumnNullable(string $table, string $name): void
protected function setColumnNotNullable(string $table, string $name): void
{
$constraintName = sprintf('chk_null_%s_%s', $table, $name);
$this->addUnsafeSql(sprintf('ALTER TABLE %s DROP CONSTRAINT IF EXISTS "%s"',
$table,
$constraintName,
));
$this->addUnsafeSql(sprintf('ALTER TABLE %s ADD CONSTRAINT "%s" CHECK (%s IS NOT NULL) NOT VALID',
$table,
$constraintName,
Expand Down
1 change: 1 addition & 0 deletions tests/Database/Doctrine/Migrations/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public function testSetColumnNotNullable(): void
$this->migration->setColumnNotNullable('signer', 'email');

$this->assertSql([
'ALTER TABLE signer DROP CONSTRAINT IF EXISTS "chk_null_signer_email"',
'ALTER TABLE signer ADD CONSTRAINT "chk_null_signer_email" CHECK (email IS NOT NULL) NOT VALID',
"SET statement_timeout TO '0'",
'ALTER TABLE signer VALIDATE CONSTRAINT "chk_null_signer_email"',
Expand Down

0 comments on commit 4de40e9

Please sign in to comment.