Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update setColumnNotNullable method to drop constraint if already exists #4

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading