Skip to content

Commit 2b3668d

Browse files
authored
Update according changes in db package (#426)
1 parent 5406a47 commit 2b3668d

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
- Enh #403, #404: Use `DbArrayHelper::arrange()` instead of `DbArrayHelper::index()` method (@Tigrov)
4242
- New #397: Realize `Schema::loadResultColumn()` method (@Tigrov)
4343
- New #407: Use `DateTimeColumn` class for datetime column types (@Tigrov)
44-
- Enh #411: Refactor constraints (@Tigrov)
44+
- Enh #411, #426: Refactor constraints (@Tigrov)
4545
- New #408, #410: Implement `DMLQueryBuilder::upsertReturning()` method (@Tigrov)
4646
- Enh #412: Reduce binding parameters (@Tigrov)
4747
- Chg #414: Rename `DMLQueryBuilder::insertWithReturningPks()` to `DMLQueryBuilder::insertReturningPks()` (@Tigrov)

src/Schema.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Yiisoft\Db\Constant\ColumnType;
88
use Yiisoft\Db\Constant\ReferentialAction;
9-
use Yiisoft\Db\Constraint\CheckConstraint;
10-
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
11-
use Yiisoft\Db\Constraint\IndexConstraint;
9+
use Yiisoft\Db\Constraint\Check;
10+
use Yiisoft\Db\Constraint\ForeignKey;
11+
use Yiisoft\Db\Constraint\Index;
1212
use Yiisoft\Db\Driver\Pdo\AbstractPdoSchema;
1313
use Yiisoft\Db\Exception\NotSupportedException;
1414
use Yiisoft\Db\Helper\DbArrayHelper;
@@ -165,15 +165,15 @@ protected function loadTableSchema(string $name): TableSchemaInterface|null
165165
return null;
166166
}
167167

168-
protected function loadTablePrimaryKey(string $tableName): IndexConstraint|null
168+
protected function loadTablePrimaryKey(string $tableName): Index|null
169169
{
170-
/** @var IndexConstraint|null */
170+
/** @var Index|null */
171171
return $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
172172
}
173173

174174
protected function loadTableForeignKeys(string $tableName): array
175175
{
176-
/** @var ForeignKeyConstraint[] */
176+
/** @var ForeignKey[] */
177177
return $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
178178
}
179179

@@ -183,8 +183,8 @@ protected function loadTableIndexes(string $tableName): array
183183
SELECT
184184
"ic"."relname" AS "name",
185185
"ia"."attname" AS "column_name",
186-
"i"."indisunique" AS "index_is_unique",
187-
"i"."indisprimary" AS "index_is_primary"
186+
"i"."indisunique" AS "is_unique",
187+
"i"."indisprimary" AS "is_primary_key"
188188
FROM "pg_class" AS "tc"
189189
INNER JOIN "pg_namespace" AS "tcns"
190190
ON "tcns"."oid" = "tc"."relnamespace"
@@ -218,17 +218,17 @@ protected function loadTableIndexes(string $tableName): array
218218
* array{
219219
* name: string,
220220
* column_name: string,
221-
* index_is_unique: bool,
222-
* index_is_primary: bool
221+
* is_unique: bool,
222+
* is_primary_key: bool
223223
* }
224224
* > $index
225225
*/
226226
foreach ($indexes as $name => $index) {
227-
$result[] = new IndexConstraint(
227+
$result[] = new Index(
228228
$name,
229229
array_column($index, 'column_name'),
230-
$index[0]['index_is_unique'],
231-
$index[0]['index_is_primary'],
230+
$index[0]['is_unique'],
231+
$index[0]['is_primary_key'],
232232
);
233233
}
234234

@@ -237,13 +237,13 @@ protected function loadTableIndexes(string $tableName): array
237237

238238
protected function loadTableUniques(string $tableName): array
239239
{
240-
/** @var IndexConstraint[] */
240+
/** @var Index[] */
241241
return $this->loadTableConstraints($tableName, self::UNIQUES);
242242
}
243243

244244
protected function loadTableChecks(string $tableName): array
245245
{
246-
/** @var CheckConstraint[] */
246+
/** @var Check[] */
247247
return $this->loadTableConstraints($tableName, self::CHECKS);
248248
}
249249

@@ -717,9 +717,9 @@ private function loadColumn(array $info): ColumnInterface
717717
* - uniques
718718
* - checks
719719
*
720-
* @return CheckConstraint[]|ForeignKeyConstraint[]|IndexConstraint|IndexConstraint[]|null Constraints.
720+
* @return Check[]|ForeignKey[]|Index|Index[]|null Constraints.
721721
*/
722-
private function loadTableConstraints(string $tableName, string $returnType): array|IndexConstraint|null
722+
private function loadTableConstraints(string $tableName, string $returnType): array|Index|null
723723
{
724724
$sql = <<<SQL
725725
SELECT
@@ -788,26 +788,27 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
788788
*/
789789
foreach ($names as $name => $constraint) {
790790
match ($type) {
791-
'p' => $result[self::PRIMARY_KEY] = new IndexConstraint(
791+
'p' => $result[self::PRIMARY_KEY] = new Index(
792792
$name,
793793
array_column($constraint, 'column_name'),
794794
true,
795795
true,
796796
),
797-
'f' => $result[self::FOREIGN_KEYS][] = new ForeignKeyConstraint(
797+
'f' => $result[self::FOREIGN_KEYS][] = new ForeignKey(
798798
$name,
799799
array_values(array_unique(array_column($constraint, 'column_name'))),
800-
$constraint[0]['foreign_table_schema'] . '.' . $constraint[0]['foreign_table_name'],
800+
$constraint[0]['foreign_table_schema'],
801+
$constraint[0]['foreign_table_name'],
801802
array_values(array_unique(array_column($constraint, 'foreign_column_name'))),
802-
$actionTypes[$constraint[0]['on_update']] ?? null,
803803
$actionTypes[$constraint[0]['on_delete']] ?? null,
804+
$actionTypes[$constraint[0]['on_update']] ?? null,
804805
),
805-
'u' => $result[self::UNIQUES][] = new IndexConstraint(
806+
'u' => $result[self::UNIQUES][] = new Index(
806807
$name,
807808
array_column($constraint, 'column_name'),
808809
true,
809810
),
810-
'c' => $result[self::CHECKS][] = new CheckConstraint(
811+
'c' => $result[self::CHECKS][] = new Check(
811812
$name,
812813
array_column($constraint, 'column_name'),
813814
$constraint[0]['check_expr'],

tests/Provider/SchemaProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Yiisoft\Db\Schema\Column\DoubleColumn;
1818
use Yiisoft\Db\Schema\Column\JsonColumn;
1919
use Yiisoft\Db\Schema\Column\StringColumn;
20+
use Yiisoft\Db\Tests\Support\Assert;
2021

2122
final class SchemaProvider extends \Yiisoft\Db\Tests\Provider\SchemaProvider
2223
{
@@ -258,8 +259,9 @@ public static function constraints(): array
258259
{
259260
$constraints = parent::constraints();
260261

261-
$constraints['1: check'][2][0]->expression('CHECK ((("C_check")::text <> \'\'::text))');
262-
$constraints['3: foreign key'][2][0]->foreignTableName('public.T_constraints_2');
262+
Assert::setPropertyValue($constraints['1: check'][2][0], 'expression', 'CHECK ((("C_check")::text <> \'\'::text))');
263+
Assert::setPropertyValue($constraints['3: foreign key'][2][0], 'foreignSchemaName', 'public');
264+
Assert::setPropertyValue($constraints['3: foreign key'][2][0], 'foreignTableName', 'T_constraints_2');
263265
$constraints['3: index'][2] = [];
264266

265267
return $constraints;

tests/SchemaTest.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Throwable;
1010
use Yiisoft\Db\Command\CommandInterface;
1111
use Yiisoft\Db\Connection\ConnectionInterface;
12-
use Yiisoft\Db\Constraint\IndexConstraint;
12+
use Yiisoft\Db\Constraint\Index;
1313
use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
1414
use Yiisoft\Db\Exception\Exception;
1515
use Yiisoft\Db\Exception\InvalidConfigException;
@@ -591,30 +591,19 @@ public function testTableIndexes(): void
591591
$db = $this->getConnection(true);
592592
$schema = $db->getSchema();
593593

594-
/** @var IndexConstraint[] $tableIndexes */
594+
/** @var Index[] $tableIndexes */
595595
$tableIndexes = $schema->getTableIndexes('table_index');
596596

597-
$this->assertCount(5, $tableIndexes);
598-
599-
$this->assertSame(['id'], $tableIndexes[0]->getColumnNames());
600-
$this->assertTrue($tableIndexes[0]->isPrimaryKey());
601-
$this->assertTrue($tableIndexes[0]->isUnique());
602-
603-
$this->assertSame(['one_unique'], $tableIndexes[1]->getColumnNames());
604-
$this->assertFalse($tableIndexes[1]->isPrimaryKey());
605-
$this->assertTrue($tableIndexes[1]->isUnique());
606-
607-
$this->assertSame(['two_unique_1', 'two_unique_2'], $tableIndexes[2]->getColumnNames());
608-
$this->assertFalse($tableIndexes[2]->isPrimaryKey());
609-
$this->assertTrue($tableIndexes[2]->isUnique());
610-
611-
$this->assertSame(['unique_index'], $tableIndexes[3]->getColumnNames());
612-
$this->assertFalse($tableIndexes[3]->isPrimaryKey());
613-
$this->assertTrue($tableIndexes[3]->isUnique());
614-
615-
$this->assertSame(['non_unique_index'], $tableIndexes[4]->getColumnNames());
616-
$this->assertFalse($tableIndexes[4]->isPrimaryKey());
617-
$this->assertFalse($tableIndexes[4]->isUnique());
597+
$this->assertEquals(
598+
[
599+
new Index('table_index_pkey', ['id'], true, true),
600+
new Index('table_index_one_unique_key', ['one_unique'], true),
601+
new Index('table_index_two_unique_1_two_unique_2_key', ['two_unique_1', 'two_unique_2'], true),
602+
new Index('table_index_unique_index_non_unique_index_idx', ['unique_index'], true),
603+
new Index('table_index_non_unique_index_unique_index_idx', ['non_unique_index']),
604+
],
605+
$tableIndexes
606+
);
618607

619608
$db->close();
620609
}

0 commit comments

Comments
 (0)