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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 17 additions & 18 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::*,
Expand Down Expand Up @@ -419,17 +419,17 @@ 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 = <<<SQL
SELECT
`kcu`.`CONSTRAINT_NAME` AS `name`,
`kcu`.`COLUMN_NAME` AS `column_name`,
`tc`.`CONSTRAINT_TYPE` AS `type`,
CASE
WHEN :schemaName IS NULL AND `kcu`.`REFERENCED_TABLE_SCHEMA` = DATABASE() THEN NULL
WHEN :schemaName IS NULL AND `kcu`.`REFERENCED_TABLE_SCHEMA` = DATABASE() THEN ''
ELSE `kcu`.`REFERENCED_TABLE_SCHEMA`
END AS `foreign_table_schema`,
`kcu`.`REFERENCED_TABLE_NAME` AS `foreign_table_name`,
Expand Down Expand Up @@ -496,23 +496,22 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
foreach ($constraints as $type => $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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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'],
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')")];

Expand Down
5 changes: 3 additions & 2 deletions tests/Provider/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 5 additions & 6 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Throwable;
use Yiisoft\Db\Command\CommandInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Constraint\Index;
use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -391,11 +391,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();

Expand Down