diff --git a/src/Doctrine/Migration.php b/src/Doctrine/Migration.php index c6b35de..342b66e 100644 --- a/src/Doctrine/Migration.php +++ b/src/Doctrine/Migration.php @@ -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, diff --git a/tests/Database/Doctrine/Migrations/MigrationTest.php b/tests/Database/Doctrine/Migrations/MigrationTest.php index ae3c191..3c506a4 100644 --- a/tests/Database/Doctrine/Migrations/MigrationTest.php +++ b/tests/Database/Doctrine/Migrations/MigrationTest.php @@ -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"',