Skip to content

Commit

Permalink
Fix table indexes (#331)
Browse files Browse the repository at this point in the history
* Fix table indexes

* Support by pgsql 11 and above

* Support query by pgsql versions below 11

* Add line to CHANGELOG.md [skip ci]
  • Loading branch information
Tigrov authored Jan 16, 2024
1 parent 02a2a87 commit be590ac
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Enh #324: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov)
- Enh #303: Support composite types (@Tigrov)
- Enh #330: Create instance of `ArrayParser` directly (@Tigrov)
- Bug #331: Exclude from index column names fields specified in `INCLUDE` clause (@Tigrov)

## 1.2.0 November 12, 2023

Expand Down
2 changes: 1 addition & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ protected function loadTableIndexes(string $tableName): array
INNER JOIN "pg_class" AS "ic"
ON "ic"."oid" = "i"."indexrelid"
INNER JOIN "pg_attribute" AS "ia"
ON "ia"."attrelid" = "i"."indexrelid"
ON "ia"."attrelid" = "i"."indexrelid" AND "ia"."attnum" <= cardinality("i"."indoption")
WHERE "tcns"."nspname" = :schemaName AND "tc"."relname" = :tableName
ORDER BY "ia"."attnum" ASC
SQL;
Expand Down
38 changes: 38 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Throwable;
use Yiisoft\Db\Command\CommandInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -605,4 +606,41 @@ private function testCompositeTypeColumnSchemaRecursive(array $columns, string $

$db->close();
}

public function testTableIndexes(): void
{
$this->fixture = 'pgsql11.sql';

if (version_compare($this->getConnection()->getServerVersion(), '11.0', '<')) {
$this->markTestSkipped('PostgresSQL < 11.0 does not support INCLUDE clause.');
}

$db = $this->getConnection(true);
$schema = $db->getSchema();

/** @var IndexConstraint[] $tableIndexes */
$tableIndexes = $schema->getTableIndexes('table_index');

$this->assertCount(5, $tableIndexes);

$this->assertSame(['id'], $tableIndexes[0]->getColumnNames());
$this->assertTrue($tableIndexes[0]->isPrimary());
$this->assertTrue($tableIndexes[0]->isUnique());

$this->assertSame(['one_unique'], $tableIndexes[1]->getColumnNames());
$this->assertFalse($tableIndexes[1]->isPrimary());
$this->assertTrue($tableIndexes[1]->isUnique());

$this->assertSame(['two_unique_1', 'two_unique_2'], $tableIndexes[2]->getColumnNames());
$this->assertFalse($tableIndexes[2]->isPrimary());
$this->assertTrue($tableIndexes[2]->isUnique());

$this->assertSame(['unique_index'], $tableIndexes[3]->getColumnNames());
$this->assertFalse($tableIndexes[3]->isPrimary());
$this->assertTrue($tableIndexes[3]->isUnique());

$this->assertSame(['non_unique_index'], $tableIndexes[4]->getColumnNames());
$this->assertFalse($tableIndexes[4]->isPrimary());
$this->assertFalse($tableIndexes[4]->isUnique());
}
}
14 changes: 14 additions & 0 deletions tests/Support/Fixture/pgsql11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DROP TABLE IF EXISTS "table_index";

CREATE TABLE "table_index" (
"id" serial PRIMARY KEY,
"one_unique" integer UNIQUE,
"two_unique_1" integer,
"two_unique_2" integer,
"unique_index" integer,
"non_unique_index" integer,
UNIQUE ("two_unique_1", "two_unique_2")
);

CREATE UNIQUE INDEX ON "table_index" ("unique_index") INCLUDE ("non_unique_index");
CREATE INDEX ON "table_index" ("non_unique_index") INCLUDE ("unique_index");

0 comments on commit be590ac

Please sign in to comment.