From 489502c4b25ac4dc3cdef771d1eed0c64f7b7305 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 21 Jul 2025 17:09:31 +0300 Subject: [PATCH 1/3] Update according changes in `db` package --- src/Schema.php | 35 ++++++++++++------------- tests/Provider/QueryBuilderProvider.php | 4 +-- tests/Provider/SchemaProvider.php | 5 ++-- tests/SchemaTest.php | 10 +++---- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Schema.php b/src/Schema.php index eddacd344..10948da8e 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -6,8 +6,8 @@ use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\ReferentialAction; -use Yiisoft\Db\Constraint\ForeignKeyConstraint; -use Yiisoft\Db\Constraint\IndexConstraint; +use Yiisoft\Db\Constraint\ForeignKey; +use Yiisoft\Db\Constraint\Index; use Yiisoft\Db\Driver\Pdo\AbstractPdoSchema; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\NotSupportedException; @@ -59,7 +59,7 @@ * name: string, * column_name: string, * type: string, - * foreign_table_schema: string|null, + * foreign_table_schema: string, * foreign_table_name: string, * foreign_column_name: string, * on_update: ReferentialAction::*, @@ -419,9 +419,9 @@ protected function loadTableChecks(string $tableName): array * - foreignKeys * - uniques * - * @psalm-return ForeignKeyConstraint[]|IndexConstraint[]|IndexConstraint|null + * @psalm-return ForeignKey[]|Index[]|Index|null */ - private function loadTableConstraints(string $tableName, string $returnType): array|IndexConstraint|null + private function loadTableConstraints(string $tableName, string $returnType): array|Index|null { $sql = << $names) { foreach ($names as $name => $constraint) { match ($type) { - 'PRIMARY KEY' => $result[self::PRIMARY_KEY] = new IndexConstraint( + 'PRIMARY KEY' => $result[self::PRIMARY_KEY] = new Index( '', array_column($constraint, 'column_name'), true, true, ), - 'FOREIGN KEY' => $result[self::FOREIGN_KEYS][] = new ForeignKeyConstraint( + 'FOREIGN KEY' => $result[self::FOREIGN_KEYS][] = new ForeignKey( $name, array_column($constraint, 'column_name'), - $constraint[0]['foreign_table_schema'] !== null - ? ($constraint[0]['foreign_table_schema'] . '.' . $constraint[0]['foreign_table_name']) - : $constraint[0]['foreign_table_name'], + $constraint[0]['foreign_table_schema'], + $constraint[0]['foreign_table_name'], array_column($constraint, 'foreign_column_name'), - $constraint[0]['on_update'], $constraint[0]['on_delete'], + $constraint[0]['on_update'], ), - 'UNIQUE' => $result[self::UNIQUES][] = new IndexConstraint( + 'UNIQUE' => $result[self::UNIQUES][] = new Index( $name, array_column($constraint, 'column_name'), true, @@ -538,7 +537,7 @@ protected function loadTableDefaultValues(string $tableName): array protected function loadTableForeignKeys(string $tableName): array { - /** @var ForeignKeyConstraint[] */ + /** @var ForeignKey[] */ return $this->loadTableConstraints($tableName, self::FOREIGN_KEYS); } @@ -575,7 +574,7 @@ protected function loadTableIndexes(string $tableName): array foreach ($indexes as $name => $index) { $isPrimaryKey = (bool) $index[0]['is_primary_key']; - $result[] = new IndexConstraint( + $result[] = new Index( $isPrimaryKey ? '' : $name, array_column($index, 'column_name'), (bool) $index[0]['is_unique'], @@ -586,9 +585,9 @@ protected function loadTableIndexes(string $tableName): array return $result; } - protected function loadTablePrimaryKey(string $tableName): IndexConstraint|null + protected function loadTablePrimaryKey(string $tableName): Index|null { - /** @var IndexConstraint|null */ + /** @var Index|null */ return $this->loadTableConstraints($tableName, self::PRIMARY_KEY); } @@ -609,7 +608,7 @@ protected function loadTableSchema(string $name): TableSchemaInterface|null protected function loadTableUniques(string $tableName): array { - /** @var IndexConstraint[] */ + /** @var Index[] */ return $this->loadTableConstraints($tableName, self::UNIQUES); } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index c744a3393..932927576 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -256,8 +256,8 @@ public static function buildColumnDefinition(): array $values["integer()->defaultValue('')"][0] = 'int DEFAULT NULL'; $values['unsigned()'][0] = 'int UNSIGNED'; $values['integer(8)->scale(2)'][0] = 'int(8)'; - $values['reference($reference)'][0] = 'int REFERENCES `ref_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE'; - $values['reference($referenceWithSchema)'][0] = 'int REFERENCES `ref_schema`.`ref_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE'; + $values['reference($reference)'][0] = 'int REFERENCES `ref_table` (`id`) ON DELETE SET NULL ON UPDATE CASCADE'; + $values['reference($referenceWithSchema)'][0] = 'int REFERENCES `ref_schema`.`ref_table` (`id`) ON DELETE SET NULL ON UPDATE CASCADE'; $values[] = ["enum('a','b','c')", ColumnBuilder::string()->dbType("enum('a','b','c')")]; diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index f3bbf3c50..ca63a4133 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -18,6 +18,7 @@ use Yiisoft\Db\Schema\Column\IntegerColumn; use Yiisoft\Db\Schema\Column\JsonColumn; use Yiisoft\Db\Schema\Column\StringColumn; +use Yiisoft\Db\Tests\Support\Assert; final class SchemaProvider extends \Yiisoft\Db\Tests\Provider\SchemaProvider { @@ -253,8 +254,8 @@ public static function constraints(): array $constraints = parent::constraints(); $constraints['1: check'][2] = false; - $constraints['2: primary key'][2]->name(''); - $constraints['2: index'][2][0]->name(''); + Assert::setPropertyValue($constraints['2: primary key'][2], 'name', ''); + Assert::setPropertyValue($constraints['2: index'][2][0], 'name', ''); $constraints['2: check'][2] = false; $constraints['3: check'][2] = false; $constraints['4: check'][2] = false; diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 28ac230be..8e56eb051 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -10,6 +10,7 @@ use Throwable; use Yiisoft\Db\Command\CommandInterface; use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constraint\Index; use Yiisoft\Db\Constraint\IndexConstraint; use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; use Yiisoft\Db\Exception\Exception; @@ -391,11 +392,10 @@ public function testWorkWithPrimaryKeyConstraint(): void $this->createTableForIndexAndConstraintTests($db, $tableName, $columnName); $db->createCommand()->addPrimaryKey($tableName, $constraintName, $columnName)->execute(); - $constraints = $db->getSchema()->getTablePrimaryKey($tableName, true); - - $this->assertInstanceOf(IndexConstraint::class, $constraints); - $this->assertEquals('', $constraints->getName()); - $this->assertEquals([$columnName], $constraints->getColumnNames()); + $this->assertEquals( + new Index('', [$columnName], true, true), + $db->getSchema()->getTablePrimaryKey($tableName), + ); $db->createCommand()->dropPrimaryKey($tableName, $constraintName)->execute(); From ffbb84997b8fd114fac5fb5f248f2888df420a69 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 21 Jul 2025 14:16:43 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- tests/SchemaTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 8e56eb051..f4608dc7e 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Command\CommandInterface; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Constraint\Index; -use Yiisoft\Db\Constraint\IndexConstraint; use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; From 6334f5fddfdc2760e99e2da31a61d186ac352e2d Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 21 Jul 2025 18:15:24 +0300 Subject: [PATCH 3/3] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cee44120a..3749f497f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,7 @@ - Enh #389, #390: Use `DbArrayHelper::arrange()` instead of `DbArrayHelper::index()` method (@Tigrov) - New #387: Realize `Schema::loadResultColumn()` method (@Tigrov) - New #393: Use `DateTimeColumn` class for datetime column types (@Tigrov) -- Enh #396: Refactor constraints (@Tigrov) +- Enh #396, #409: Refactor constraints (@Tigrov) - New #394, #395, #398: Implement `Command::upsertReturning()` method (@Tigrov) - Enh #394, #395: Refactor `Command::insertWithReturningPks()` method (@Tigrov) - Chg #399: Rename `insertWithReturningPks()` to `insertReturningPks()` in `Command` and `DMLQueryBuilder` classes (@Tigrov)