Skip to content

Commit 5dbca2d

Browse files
authored
Update according changes in db package (#377)
1 parent 582fa18 commit 5dbca2d

File tree

6 files changed

+45
-47
lines changed

6 files changed

+45
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
- Enh #361, #362: Refactor `DMLQueryBuilder::insertWithReturningPks()` method (@Tigrov)
4646
- New #361, #362: Implement `DMLQueryBuilder::upsertReturning()` method (@Tigrov)
4747
- Chg #363: Add alias in `DQLQueryBuilder::selectExists()` method for consistency with other DBMS (@Tigrov)
48-
- Enh #364: Refactor constraints (@Tigrov)
48+
- Enh #364, #377: Refactor constraints (@Tigrov)
4949
- Chg #366: Rename `DMLQueryBuilder::insertWithReturningPks()` to `DMLQueryBuilder::insertReturningPks()` (@Tigrov)
5050
- Enh #372: Provide `yiisoft/db-implementation` virtual package (@vjik)
5151
- Enh #375: 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
@@ -209,7 +209,7 @@ private function prepareUpsertParts(
209209

210210
foreach ($constraints as $constraint) {
211211
$constraintCondition = ['and'];
212-
$columnNames = $constraint->getColumnNames();
212+
$columnNames = $constraint->columnNames;
213213

214214
foreach ($columnNames as $name) {
215215
$quotedName = $this->quoter->quoteColumnName($name);

src/Schema.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Yiisoft\Db\Mssql;
66

7-
use Yiisoft\Db\Constraint\CheckConstraint;
8-
use Yiisoft\Db\Constraint\DefaultValueConstraint;
9-
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
10-
use Yiisoft\Db\Constraint\IndexConstraint;
7+
use Yiisoft\Db\Constraint\Check;
8+
use Yiisoft\Db\Constraint\DefaultValue;
9+
use Yiisoft\Db\Constraint\ForeignKey;
10+
use Yiisoft\Db\Constraint\Index;
1111
use Yiisoft\Db\Driver\Pdo\AbstractPdoSchema;
1212
use Yiisoft\Db\Exception\Exception;
1313
use Yiisoft\Db\Helper\DbArrayHelper;
@@ -197,15 +197,15 @@ protected function loadTableSchema(string $name): TableSchemaInterface|null
197197
return null;
198198
}
199199

200-
protected function loadTablePrimaryKey(string $tableName): IndexConstraint|null
200+
protected function loadTablePrimaryKey(string $tableName): Index|null
201201
{
202-
/** @var IndexConstraint|null */
202+
/** @var Index|null */
203203
return $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
204204
}
205205

206206
protected function loadTableForeignKeys(string $tableName): array
207207
{
208-
/** @var ForeignKeyConstraint[] */
208+
/** @var ForeignKey[] */
209209
return $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
210210
}
211211

@@ -241,7 +241,7 @@ protected function loadTableIndexes(string $tableName): array
241241
* > $indexes
242242
*/
243243
foreach ($indexes as $name => $index) {
244-
$result[] = new IndexConstraint(
244+
$result[] = new Index(
245245
$name,
246246
array_column($index, 'column_name'),
247247
(bool) $index[0]['is_unique'],
@@ -254,19 +254,19 @@ protected function loadTableIndexes(string $tableName): array
254254

255255
protected function loadTableUniques(string $tableName): array
256256
{
257-
/** @var IndexConstraint[] */
257+
/** @var Index[] */
258258
return $this->loadTableConstraints($tableName, self::UNIQUES);
259259
}
260260

261261
protected function loadTableChecks(string $tableName): array
262262
{
263-
/** @var CheckConstraint[] */
263+
/** @var Check[] */
264264
return $this->loadTableConstraints($tableName, self::CHECKS);
265265
}
266266

267267
protected function loadTableDefaultValues(string $tableName): array
268268
{
269-
/** @var DefaultValueConstraint[] */
269+
/** @var DefaultValue[] */
270270
return $this->loadTableConstraints($tableName, self::DEFAULTS);
271271
}
272272

@@ -537,10 +537,10 @@ public function findUniqueIndexes(TableSchemaInterface $table): array
537537
* - checks
538538
* - defaults
539539
*
540-
* @return CheckConstraint[]|DefaultValueConstraint[]|ForeignKeyConstraint[]|IndexConstraint|IndexConstraint[]|null
540+
* @return Check[]|DefaultValue[]|ForeignKey[]|Index|Index[]|null
541541
* Constraints of the specified type.
542542
*/
543-
private function loadTableConstraints(string $tableName, string $returnType): array|IndexConstraint|null
543+
private function loadTableConstraints(string $tableName, string $returnType): array|Index|null
544544
{
545545
$sql = <<<SQL
546546
SELECT
@@ -604,31 +604,32 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
604604
foreach ($names as $name => $constraint) {
605605
/** @psalm-suppress ArgumentTypeCoercion */
606606
match ($type) {
607-
'PK' => $result[self::PRIMARY_KEY] = new IndexConstraint(
607+
'PK' => $result[self::PRIMARY_KEY] = new Index(
608608
$name,
609609
array_column($constraint, 'column_name'),
610610
true,
611611
true,
612612
),
613-
'F' => $result[self::FOREIGN_KEYS][] = new ForeignKeyConstraint(
613+
'F' => $result[self::FOREIGN_KEYS][] = new ForeignKey(
614614
$name,
615615
array_column($constraint, 'column_name'),
616-
$constraint[0]['foreign_table_schema'] . '.' . $constraint[0]['foreign_table_name'],
616+
$constraint[0]['foreign_table_schema'],
617+
$constraint[0]['foreign_table_name'],
617618
array_column($constraint, 'foreign_column_name'),
618-
str_replace('_', ' ', $constraint[0]['on_update']),
619619
str_replace('_', ' ', $constraint[0]['on_delete']),
620+
str_replace('_', ' ', $constraint[0]['on_update']),
620621
),
621-
'UQ' => $result[self::UNIQUES][] = new IndexConstraint(
622+
'UQ' => $result[self::UNIQUES][] = new Index(
622623
$name,
623624
array_column($constraint, 'column_name'),
624625
true,
625626
),
626-
'C' => $result[self::CHECKS][] = new CheckConstraint(
627+
'C' => $result[self::CHECKS][] = new Check(
627628
$name,
628629
array_column($constraint, 'column_name'),
629630
$constraint[0]['check_expr'],
630631
),
631-
'D' => $result[self::DEFAULTS][] = new DefaultValueConstraint(
632+
'D' => $result[self::DEFAULTS][] = new DefaultValue(
632633
$name,
633634
array_column($constraint, 'column_name'),
634635
$constraint[0]['default_expr'],

tests/CommandTest.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Mssql\Tests;
66

77
use PHPUnit\Framework\Attributes\DataProviderExternal;
8+
use Yiisoft\Db\Constraint\Index;
89
use Yiisoft\Db\Exception\IntegrityException;
910
use Yiisoft\Db\Exception\NotSupportedException;
1011
use Yiisoft\Db\Expression\Expression;
@@ -301,13 +302,10 @@ public function testCreateClusteredColumnstoreIndex(): void
301302
$command->createTable($tableName, ['col1' => ColumnBuilder::integer()])->execute();
302303
$command->createIndex($tableName, $indexName, '', IndexType::CLUSTERED_COLUMNSTORE)->execute();
303304

304-
$this->assertCount(1, $schema->getTableIndexes($tableName));
305-
306-
$index = $schema->getTableIndexes($tableName)[0];
307-
308-
$this->assertSame(['col1'], $index->getColumnNames());
309-
$this->assertFalse($index->isUnique());
310-
$this->assertFalse($index->isPrimaryKey());
305+
$this->assertEquals(
306+
[new Index($indexName, ['col1'])],
307+
$schema->getTableIndexes($tableName),
308+
);
311309

312310
$db->close();
313311
}
@@ -333,10 +331,9 @@ public function testCreateXmlIndex(): void
333331

334332
$this->assertCount(3, $schema->getTableIndexes($tableName));
335333

336-
$index = array_filter($schema->getTableIndexes($tableName), static fn ($index) => !$index->isPrimaryKey())[1];
334+
$index = array_filter($schema->getTableIndexes($tableName), static fn ($index) => !$index->isPrimaryKey)[1];
337335

338-
$this->assertSame($indexColumns, $index->getColumnNames());
339-
$this->assertFalse($index->isUnique());
336+
$this->assertEquals(new Index($xmlIndexName, $indexColumns), $index);
340337

341338
$db->close();
342339
}

tests/Provider/QueryBuilderProvider.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Yiisoft\Db\Constant\DataType;
99
use Yiisoft\Db\Constant\PseudoType;
1010
use Yiisoft\Db\Constant\ReferentialAction;
11-
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
11+
use Yiisoft\Db\Constraint\ForeignKey;
1212
use Yiisoft\Db\Expression\Expression;
1313
use Yiisoft\Db\Mssql\Column\ColumnBuilder;
1414
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
@@ -774,14 +774,15 @@ public static function buildColumnDefinition(): array
774774
$values['unique()'][0] = 'nvarchar(255) UNIQUE';
775775
$values['unsigned()'][0] = 'int';
776776
$values['integer(8)->scale(2)'][0] = 'int';
777-
$values['reference($reference)'][0] = 'int REFERENCES [ref_table] ([id]) ON DELETE CASCADE ON UPDATE CASCADE';
778-
$values['reference($referenceWithSchema)'][0] = 'int REFERENCES [ref_schema].[ref_table] ([id]) ON DELETE CASCADE ON UPDATE CASCADE';
779-
780-
$referenceRestrict = new ForeignKeyConstraint();
781-
$referenceRestrict->foreignColumnNames(['id']);
782-
$referenceRestrict->foreignTableName('ref_table');
783-
$referenceRestrict->onDelete(ReferentialAction::RESTRICT);
784-
$referenceRestrict->onUpdate(ReferentialAction::RESTRICT);
777+
$values['reference($reference)'][0] = 'int REFERENCES [ref_table] ([id]) ON DELETE SET NULL ON UPDATE CASCADE';
778+
$values['reference($referenceWithSchema)'][0] = 'int REFERENCES [ref_schema].[ref_table] ([id]) ON DELETE SET NULL ON UPDATE CASCADE';
779+
780+
$referenceRestrict = new ForeignKey(
781+
foreignTableName: 'ref_table',
782+
foreignColumnNames: ['id'],
783+
onDelete: ReferentialAction::RESTRICT,
784+
onUpdate: ReferentialAction::RESTRICT,
785+
);
785786

786787
$values[] = ['int REFERENCES [ref_table] ([id])', ColumnBuilder::integer()->reference($referenceRestrict)];
787788

tests/Provider/SchemaProvider.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use DateTimeImmutable;
88
use DateTimeZone;
99
use Yiisoft\Db\Constant\ColumnType;
10-
use Yiisoft\Db\Constraint\DefaultValueConstraint;
10+
use Yiisoft\Db\Constraint\DefaultValue;
1111
use Yiisoft\Db\Mssql\Column\BinaryColumn;
1212
use Yiisoft\Db\Mssql\Column\DateTimeColumn;
1313
use Yiisoft\Db\Schema\Column\BooleanColumn;
1414
use Yiisoft\Db\Schema\Column\DoubleColumn;
1515
use Yiisoft\Db\Schema\Column\IntegerColumn;
1616
use Yiisoft\Db\Schema\Column\JsonColumn;
1717
use Yiisoft\Db\Schema\Column\StringColumn;
18+
use Yiisoft\Db\Tests\Support\Assert;
1819

1920
final class SchemaProvider extends \Yiisoft\Db\Tests\Provider\SchemaProvider
2021
{
@@ -136,12 +137,10 @@ public static function constraints(): array
136137
{
137138
$constraints = parent::constraints();
138139

139-
$constraints['1: check'][2][0]->expression('([C_check]<>\'\')');
140-
$constraints['1: default'][2] = [new DefaultValueConstraint('', ['C_default'], '((0))')];
141-
140+
Assert::setPropertyValue($constraints['1: check'][2][0], 'expression', "([C_check]<>'')");
141+
$constraints['1: default'][2] = [new DefaultValue('', ['C_default'], '((0))')];
142142
$constraints['2: default'][2] = [];
143-
144-
$constraints['3: foreign key'][2][0]->foreignTableName('dbo.T_constraints_2');
143+
Assert::setPropertyValue($constraints['3: foreign key'][2][0], 'foreignSchemaName', 'dbo');
145144
$constraints['3: index'][2] = [];
146145
$constraints['3: default'][2] = [];
147146
$constraints['4: default'][2] = [];

0 commit comments

Comments
 (0)