Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique indexes can be loaded in 3 different ways #742

Open
Tigrov opened this issue Aug 15, 2023 · 1 comment
Open

Unique indexes can be loaded in 3 different ways #742

Tigrov opened this issue Aug 15, 2023 · 1 comment

Comments

@Tigrov
Copy link
Member

Tigrov commented Aug 15, 2023

Unique indexes can be loaded in 3 different ways:

  • Schema::loadTableConstraints(): Constraint[] <- AbstractSchema::getTableUniques()
  • Schema::loadTableIndexes(): IndexConstraint[] <- AbstractSchema::getTableIndexes() with check $index->isUnique()
  • Schema::findUniqueIndexes(): array <- SchemaInterface::findUniqueIndexes()

PgSQL
https://github.com/yiisoft/db-pgsql/blob/535975396d865da6cb15ed7bee5648c27a9b7360/src/Schema.php#L894
https://github.com/yiisoft/db-pgsql/tree/535975396d865da6cb15ed7bee5648c27a9b7360/src/Schema.php#L357
https://github.com/yiisoft/db-pgsql/tree/535975396d865da6cb15ed7bee5648c27a9b7360/src/Schema.php#L630

MySQL
https://github.com/yiisoft/db-mysql/blob/4dd4e13502a0f047627c398854fc733a41054370/src/Schema.php#L618
https://github.com/yiisoft/db-mysql/blob/4dd4e13502a0f047627c398854fc733a41054370/src/Schema.php#L770
https://github.com/yiisoft/db-mysql/blob/4dd4e13502a0f047627c398854fc733a41054370/src/Schema.php#L158

Suggestion

  • Load all unique indexes include primary key in getTableUniques()
  • Mark as @deprecated SchemaInterface::findUniqueIndexes(): array use getTableUniques() instead
  • Load all indexes loadTableIndexes() and unique indexes in one way
    • get uniques from getTableIndexes() with check $index->isUnique()
    • or load all indexes together with uniques as in SQLite Schema::loadTableConstraints() in one or more queries
@Tigrov
Copy link
Member Author

Tigrov commented Aug 19, 2023

Related with

private function getTableUniqueColumnNames(string $name, array $columns, array &$constraints = []): array
{
$primaryKey = $this->schema->getTablePrimaryKey($name);
if ($primaryKey !== null) {
$constraints[] = $primaryKey;
}
/** @psalm-var IndexConstraint[] $tableIndexes */
$tableIndexes = $this->schema->getTableIndexes($name);
foreach ($tableIndexes as $constraint) {
if ($constraint->isUnique()) {
$constraints[] = $constraint;
}
}
$constraints = array_merge($constraints, $this->schema->getTableUniques($name));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant