Skip to content

Commit 4f80770

Browse files
authored
Update according changes in db package (#343)
1 parent 87ea622 commit 4f80770

File tree

6 files changed

+64
-57
lines changed

6 files changed

+64
-57
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
- Enh #324: Refactor `Command::insertWithReturningPks()` method (@Tigrov)
5050
- Enh #325: Refactor `DMLQueryBuilder::upsert()` method (@Tigrov)
5151
- Chg #326: Add alias in `DQLQueryBuilder::selectExists()` method for consistency with other DBMS (@Tigrov)
52-
- Enh #327: Refactor constraints (@Tigrov)
52+
- Enh #327, #343: Refactor constraints (@Tigrov)
5353
- Chg #330: Rename `insertWithReturningPks()` to `insertReturningPks()` in `Command` and `DMLQueryBuilder` classes (@Tigrov)
5454
- Enh #336: Provide `yiisoft/db-implementation` virtual package (@vjik)
5555
- Enh #340: Adapt to `Param` refactoring in `yiisoft/db` package (@vjik)

src/DMLQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function upsert(
8080
$quotedTableName = $this->quoter->quoteTableName($table);
8181

8282
foreach ($constraints as $constraint) {
83-
$columnNames = $constraint->getColumnNames();
83+
$columnNames = $constraint->columnNames;
8484
$constraintCondition = ['and'];
8585

8686
foreach ($columnNames as $name) {

src/Schema.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use Yiisoft\Db\Connection\ConnectionInterface;
99
use Yiisoft\Db\Constant\ColumnType;
1010
use Yiisoft\Db\Constant\ReferentialAction;
11-
use Yiisoft\Db\Constraint\CheckConstraint;
12-
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
13-
use Yiisoft\Db\Constraint\IndexConstraint;
11+
use Yiisoft\Db\Constraint\Check;
12+
use Yiisoft\Db\Constraint\ForeignKey;
13+
use Yiisoft\Db\Constraint\Index;
1414
use Yiisoft\Db\Driver\Pdo\AbstractPdoSchema;
1515
use Yiisoft\Db\Exception\NotSupportedException;
1616
use Yiisoft\Db\Helper\DbArrayHelper;
@@ -230,15 +230,15 @@ protected function loadTableSchema(string $name): TableSchemaInterface|null
230230
return null;
231231
}
232232

233-
protected function loadTablePrimaryKey(string $tableName): IndexConstraint|null
233+
protected function loadTablePrimaryKey(string $tableName): Index|null
234234
{
235-
/** @var IndexConstraint|null */
235+
/** @var Index|null */
236236
return $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
237237
}
238238

239239
protected function loadTableForeignKeys(string $tableName): array
240240
{
241-
/** @var ForeignKeyConstraint[] */
241+
/** @var ForeignKey[] */
242242
return $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
243243
}
244244

@@ -280,7 +280,7 @@ protected function loadTableIndexes(string $tableName): array
280280
}
281281

282282
/** @var string[] $columnNames */
283-
$result[] = new IndexConstraint(
283+
$result[] = new Index(
284284
$name,
285285
$columnNames,
286286
(bool) $index[0]['is_unique'],
@@ -293,13 +293,13 @@ protected function loadTableIndexes(string $tableName): array
293293

294294
protected function loadTableUniques(string $tableName): array
295295
{
296-
/** @var IndexConstraint[] */
296+
/** @var Index[] */
297297
return $this->loadTableConstraints($tableName, self::UNIQUES);
298298
}
299299

300300
protected function loadTableChecks(string $tableName): array
301301
{
302-
/** @var CheckConstraint[] */
302+
/** @var Check[] */
303303
return $this->loadTableConstraints($tableName, self::CHECKS);
304304
}
305305

@@ -598,9 +598,9 @@ public function findUniqueIndexes(TableSchemaInterface $table): array
598598
* - uniques
599599
* - checks
600600
*
601-
* @return CheckConstraint[]|ForeignKeyConstraint[]|IndexConstraint|IndexConstraint[]|null Constraints.
601+
* @return Check[]|ForeignKey[]|Index|Index[]|null Constraints.
602602
*/
603-
private function loadTableConstraints(string $tableName, string $returnType): array|IndexConstraint|null
603+
private function loadTableConstraints(string $tableName, string $returnType): array|Index|null
604604
{
605605
$sql = <<<SQL
606606
SELECT
@@ -647,32 +647,33 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
647647
foreach ($names as $name => $constraint) {
648648
switch ($type) {
649649
case 'P':
650-
$result[self::PRIMARY_KEY] = new IndexConstraint(
650+
$result[self::PRIMARY_KEY] = new Index(
651651
$name,
652652
array_column($constraint, 'column_name'),
653653
true,
654654
true,
655655
);
656656
break;
657657
case 'R':
658-
$result[self::FOREIGN_KEYS][] = new ForeignKeyConstraint(
658+
$result[self::FOREIGN_KEYS][] = new ForeignKey(
659659
$name,
660660
array_column($constraint, 'column_name'),
661-
$constraint[0]['foreign_table_schema'] . '.' . $constraint[0]['foreign_table_name'],
661+
$constraint[0]['foreign_table_schema'],
662+
$constraint[0]['foreign_table_name'],
662663
array_column($constraint, 'foreign_column_name'),
663-
null,
664664
$constraint[0]['on_delete'],
665+
null,
665666
);
666667
break;
667668
case 'U':
668-
$result[self::UNIQUES][] = new IndexConstraint(
669+
$result[self::UNIQUES][] = new Index(
669670
$name,
670671
array_column($constraint, 'column_name'),
671672
true,
672673
);
673674
break;
674675
case 'C':
675-
$result[self::CHECKS][] = new CheckConstraint(
676+
$result[self::CHECKS][] = new Check(
676677
$name,
677678
array_column($constraint, 'column_name'),
678679
$constraint[0]['check_expr'],

tests/CommandTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\Attributes\DataProviderExternal;
88
use Yiisoft\Db\Constant\ColumnType;
99
use Yiisoft\Db\Constant\PseudoType;
10+
use Yiisoft\Db\Constraint\Index;
1011
use Yiisoft\Db\Exception\Exception;
1112
use Yiisoft\Db\Exception\NotSupportedException;
1213
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
@@ -16,6 +17,7 @@
1617
use Yiisoft\Db\Query\Query;
1718
use Yiisoft\Db\Query\QueryInterface;
1819
use Yiisoft\Db\Tests\Common\CommonCommandTest;
20+
use Yiisoft\Db\Tests\Support\Assert;
1921

2022
use function is_resource;
2123
use function str_pad;
@@ -608,18 +610,16 @@ public function testCreateSearchIndex()
608610
$command->createTable($tableName, ['col1' => ColumnBuilder::text()])->execute();
609611
$command->createIndex($tableName, $indexName, ['col1'], IndexType::SEARCH)->execute();
610612

611-
$this->assertCount(2, $schema->getTableIndexes($tableName));
613+
$indexes = $schema->getTableIndexes($tableName);
614+
Assert::setPropertyValue($indexes[1], 'name', '');
612615

613-
$index = $schema->getTableIndexes($tableName)[0];
614-
615-
$this->assertSame(['col1'], $index->getColumnNames());
616-
$this->assertFalse($index->isUnique());
617-
$this->assertFalse($index->isPrimaryKey());
618-
619-
$sysIndex = $schema->getTableIndexes($tableName)[1];
620-
$this->assertSame([], $sysIndex->getColumnNames());
621-
$this->assertTrue($sysIndex->isUnique());
622-
$this->assertFalse($sysIndex->isPrimaryKey());
616+
$this->assertEquals(
617+
[
618+
new Index($indexName, ['col1']),
619+
new Index('', [], true),
620+
],
621+
$indexes,
622+
);
623623

624624
$db->close();
625625
}

tests/Provider/QueryBuilderProvider.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Yiisoft\Db\Constant\DataType;
1111
use Yiisoft\Db\Constant\PseudoType;
1212
use Yiisoft\Db\Constant\ReferentialAction;
13-
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
13+
use Yiisoft\Db\Constraint\ForeignKey;
1414
use Yiisoft\Db\Expression\Expression;
1515
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
1616
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
@@ -243,15 +243,19 @@ public static function upsert(): array
243243

244244
public static function buildColumnDefinition(): array
245245
{
246-
$referenceRestrict = new ForeignKeyConstraint();
247-
$referenceRestrict->foreignColumnNames(['id']);
248-
$referenceRestrict->foreignTableName('ref_table');
249-
$referenceRestrict->onDelete(ReferentialAction::RESTRICT);
250-
$referenceRestrict->onUpdate(ReferentialAction::RESTRICT);
251-
252-
$referenceSetNull = clone $referenceRestrict;
253-
$referenceSetNull->onDelete(ReferentialAction::SET_NULL);
254-
$referenceRestrict->onUpdate(ReferentialAction::SET_NULL);
246+
$referenceRestrict = new ForeignKey(
247+
foreignTableName: 'ref_table',
248+
foreignColumnNames: ['id'],
249+
onDelete: ReferentialAction::RESTRICT,
250+
onUpdate: ReferentialAction::RESTRICT,
251+
);
252+
253+
$referenceSetNull = new ForeignKey(
254+
foreignTableName: 'ref_table',
255+
foreignColumnNames:['id'],
256+
onDelete: ReferentialAction::SET_NULL,
257+
onUpdate: ReferentialAction::SET_NULL,
258+
);
255259

256260
$values = parent::buildColumnDefinition();
257261

@@ -343,8 +347,8 @@ public static function buildColumnDefinition(): array
343347
$values['unsigned()'][0] = 'number(10)';
344348
$values['scale(2)'][0] = 'number(10,2)';
345349
$values['integer(8)->scale(2)'][0] = 'number(8)';
346-
$values['reference($reference)'][0] = 'number(10) REFERENCES "ref_table" ("id") ON DELETE CASCADE';
347-
$values['reference($referenceWithSchema)'][0] = 'number(10) REFERENCES "ref_schema"."ref_table" ("id") ON DELETE CASCADE';
350+
$values['reference($reference)'][0] = 'number(10) REFERENCES "ref_table" ("id") ON DELETE SET NULL';
351+
$values['reference($referenceWithSchema)'][0] = 'number(10) REFERENCES "ref_schema"."ref_table" ("id") ON DELETE SET NULL';
348352

349353
$db = self::getDb();
350354

tests/Provider/SchemaProvider.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use DateTimeImmutable;
88
use DateTimeZone;
99
use Yiisoft\Db\Constant\ColumnType;
10-
use Yiisoft\Db\Constraint\CheckConstraint;
10+
use Yiisoft\Db\Constraint\Check;
1111
use Yiisoft\Db\Expression\Expression;
1212
use Yiisoft\Db\Oracle\Column\BinaryColumn;
1313
use Yiisoft\Db\Oracle\Column\BooleanColumn;
@@ -17,6 +17,7 @@
1717
use Yiisoft\Db\Schema\Column\DoubleColumn;
1818
use Yiisoft\Db\Schema\Column\IntegerColumn;
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
{
@@ -175,24 +176,25 @@ public static function constraints(): array
175176
{
176177
$constraints = parent::constraints();
177178

178-
$constraints['1: check'][2][0]->expression('"C_check" <> \'\'');
179-
$constraints['1: check'][2][] = new CheckConstraint('', ['C_id'], '"C_id" IS NOT NULL');
180-
$constraints['1: check'][2][] = new CheckConstraint('', ['C_not_null'], '"C_not_null" IS NOT NULL');
181-
$constraints['1: check'][2][] = new CheckConstraint('', ['C_unique'], '"C_unique" IS NOT NULL');
182-
$constraints['1: check'][2][] = new CheckConstraint('', ['C_default'], '"C_default" IS NOT NULL');
179+
Assert::setPropertyValue($constraints['1: check'][2][0], 'expression', '"C_check" <> \'\'');
180+
$constraints['1: check'][2][] = new Check('', ['C_id'], '"C_id" IS NOT NULL');
181+
$constraints['1: check'][2][] = new Check('', ['C_not_null'], '"C_not_null" IS NOT NULL');
182+
$constraints['1: check'][2][] = new Check('', ['C_unique'], '"C_unique" IS NOT NULL');
183+
$constraints['1: check'][2][] = new Check('', ['C_default'], '"C_default" IS NOT NULL');
183184

184-
$constraints['2: check'][2][] = new CheckConstraint('', ['C_id_1'], '"C_id_1" IS NOT NULL');
185-
$constraints['2: check'][2][] = new CheckConstraint('', ['C_id_2'], '"C_id_2" IS NOT NULL');
185+
$constraints['2: check'][2][] = new Check('', ['C_id_1'], '"C_id_1" IS NOT NULL');
186+
$constraints['2: check'][2][] = new Check('', ['C_id_2'], '"C_id_2" IS NOT NULL');
186187

187-
$constraints['3: foreign key'][2][0]->foreignTableName('SYSTEM.T_constraints_2');
188-
$constraints['3: foreign key'][2][0]->onUpdate(null);
188+
Assert::setPropertyValue($constraints['3: foreign key'][2][0], 'foreignSchemaName', 'SYSTEM');
189+
Assert::setPropertyValue($constraints['3: foreign key'][2][0], 'foreignTableName', 'T_constraints_2');
190+
Assert::setPropertyValue($constraints['3: foreign key'][2][0], 'onUpdate', null);
189191
$constraints['3: index'][2] = [];
190-
$constraints['3: check'][2][] = new CheckConstraint('', ['C_fk_id_1'], '"C_fk_id_1" IS NOT NULL');
191-
$constraints['3: check'][2][] = new CheckConstraint('', ['C_fk_id_2'], '"C_fk_id_2" IS NOT NULL');
192-
$constraints['3: check'][2][] = new CheckConstraint('', ['C_id'], '"C_id" IS NOT NULL');
192+
$constraints['3: check'][2][] = new Check('', ['C_fk_id_1'], '"C_fk_id_1" IS NOT NULL');
193+
$constraints['3: check'][2][] = new Check('', ['C_fk_id_2'], '"C_fk_id_2" IS NOT NULL');
194+
$constraints['3: check'][2][] = new Check('', ['C_id'], '"C_id" IS NOT NULL');
193195

194-
$constraints['4: check'][2][] = new CheckConstraint('', ['C_id'], '"C_id" IS NOT NULL');
195-
$constraints['4: check'][2][] = new CheckConstraint('', ['C_col_2'], '"C_col_2" IS NOT NULL');
196+
$constraints['4: check'][2][] = new Check('', ['C_id'], '"C_id" IS NOT NULL');
197+
$constraints['4: check'][2][] = new Check('', ['C_col_2'], '"C_col_2" IS NOT NULL');
196198

197199
return $constraints;
198200
}

0 commit comments

Comments
 (0)