Skip to content
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
2 changes: 1 addition & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ protected function getCheckConstraints(string $tableName): array
protected function getAddCheckConstraintInstructions(TableMetadata $table, CheckConstraint $checkConstraint): AlterInstructions
{
$constraintName = $checkConstraint->getName();
if ($constraintName === null) {
if ($constraintName === null || $constraintName === '') {
// Auto-generate constraint name if not provided
$constraintName = $table->getName() . '_chk_' . substr(md5($checkConstraint->getExpression()), 0, 8);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ protected function getCheckConstraints(string $tableName): array
protected function getAddCheckConstraintInstructions(TableMetadata $table, CheckConstraint $checkConstraint): AlterInstructions
{
$constraintName = $checkConstraint->getName();
if ($constraintName === null) {
if ($constraintName === null || $constraintName === '') {
// Auto-generate constraint name if not provided
$parts = $this->getSchemaName($table->getName());
$constraintName = $parts['table'] . '_chk_' . substr(md5($checkConstraint->getExpression()), 0, 8);
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Adapter/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ protected function getAddCheckConstraintInstructions(TableMetadata $table, Check

$instructions->addPostStep(function (array $state) use ($checkConstraint): array {
$constraintName = $checkConstraint->getName();
if ($constraintName === null) {
if ($constraintName === null || $constraintName === '') {
// Auto-generate constraint name if not provided
$constraintName = 'chk_' . substr(md5($checkConstraint->getExpression()), 0, 8);
}
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2677,8 +2677,10 @@ public function testAddCheckConstraintWithAutoGeneratedName(): void
$dialect = $driver->schemaDialect();
$constraints = $dialect->describeCheckConstraints('check_table2');
$this->assertCount(1, $constraints);
$expected = $driver->isMariaDb() ? 'CONSTRAINT_1' : 'check_table2_chk_';
$this->assertStringContainsString($expected, $constraints[0]['name']);
// The name is generated by migrations (not left to the DB), so it is
// identical on MySQL and MariaDB instead of the DB fallback CONSTRAINT_1.
$expectedName = 'check_table2_chk_' . substr(md5('age >= 18'), 0, 8);
$this->assertSame($expectedName, $constraints[0]['name']);
}

public function testHasCheckConstraint(): void
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,21 @@ public function testAddCheckConstraint(): void
$this->assertTrue($this->adapter->hasCheckConstraint('check_table', 'price_positive'));
}

public function testAddCheckConstraintAutoGeneratesName(): void
{
$table = new Table('check_table_auto', [], $this->adapter);
$table->addColumn('quantity', 'integer')
->create();

$expression = 'quantity >= 0';
// An empty name must trigger auto-generation, not emit invalid `ADD CONSTRAINT CHECK`.
$checkConstraint = new CheckConstraint('', $expression);
$this->adapter->addCheckConstraint($table->getTable(), $checkConstraint);

$expectedName = 'check_table_auto_chk_' . substr(md5($expression), 0, 8);
$this->assertTrue($this->adapter->hasCheckConstraint('check_table_auto', $expectedName));
}

public function testHasCheckConstraint(): void
{
$table = new Table('check_table3', [], $this->adapter);
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Db/Adapter/SqliteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3173,6 +3173,21 @@ public function testAddCheckConstraint(): void
$this->assertTrue($this->adapter->hasCheckConstraint('check_table', 'price_positive'));
}

public function testAddCheckConstraintAutoGeneratesName(): void
{
$table = new Table('check_table_auto', [], $this->adapter);
$table->addColumn('quantity', 'integer')
->create();

$expression = 'quantity >= 0';
// An empty name must trigger auto-generation, not produce an unnamed constraint.
$checkConstraint = new CheckConstraint('', $expression);
$this->adapter->addCheckConstraint($table->getTable(), $checkConstraint);

$expectedName = 'chk_' . substr(md5($expression), 0, 8);
$this->assertTrue($this->adapter->hasCheckConstraint('check_table_auto', $expectedName));
}

public function testHasCheckConstraint(): void
{
$table = new Table('check_table3', [], $this->adapter);
Expand Down
Loading