diff --git a/src/Platforms/PostgreSqlPlatform.php b/src/Platforms/PostgreSqlPlatform.php index 951736d1..f478ef5f 100644 --- a/src/Platforms/PostgreSqlPlatform.php +++ b/src/Platforms/PostgreSqlPlatform.php @@ -88,9 +88,14 @@ public function getColumns(string $table, ?string $schema = null): array CASE WHEN a.atttypmod = -1 THEN NULL ELSE a.atttypmod -4 END AS size, pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default, COALESCE(co.contype = 'p', FALSE) AS is_primary, - COALESCE(co.contype = 'p' AND strpos(pg_get_expr(ad.adbin, ad.adrelid), 'nextval') = 1, FALSE) AS is_autoincrement, + COALESCE(co.contype = 'p' AND (strpos(pg_get_expr(ad.adbin, ad.adrelid), 'nextval') = 1 OR a.attidentity != ''), FALSE) AS is_autoincrement, NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS is_nullable, - SUBSTRING(pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass) FROM %s) AS sequence + ") . ( + count($tableArgs) > 1 + ? "pg_get_serial_sequence('%table.%table', a.attname) AS sequence" + : "pg_get_serial_sequence('%table', a.attname) AS sequence" + ) + . (/** @lang GenericSQL */ " FROM pg_catalog.pg_attribute AS a JOIN pg_catalog.pg_class AS c ON a.attrelid = c.oid @@ -108,7 +113,7 @@ public function getColumns(string $table, ?string $schema = null): array AND NOT a.attisdropped ORDER BY a.attnum - "), "nextval[(]'\"?([^'\"]+)", ...$tableArgs); + "), ...$tableArgs, ...$tableArgs); $columns = []; foreach ($result as $row) { diff --git a/tests/cases/integration/platform.postgres.phpt b/tests/cases/integration/platform.postgres.phpt index e60b8a5e..9acd1cef 100644 --- a/tests/cases/integration/platform.postgres.phpt +++ b/tests/cases/integration/platform.postgres.phpt @@ -50,7 +50,7 @@ class PlatformPostgresTest extends IntegrationTestCase 'isAutoincrement' => true, 'isUnsigned' => false, 'isNullable' => false, - 'meta' => ['sequence' => 'books_id_seq'], + 'meta' => ['sequence' => 'public.books_id_seq'], ], 'author_id' => [ 'name' => 'author_id', @@ -109,6 +109,34 @@ class PlatformPostgresTest extends IntegrationTestCase ], ], $columns); + $identityColumns = $this->connection->getPlatform()->getColumns('eans'); + $identityColumns = \array_map(function ($column) { return (array) $column; }, $identityColumns); + + Assert::same([ + 'id' => [ + 'name' => 'id', + 'type' => 'INT4', + 'size' => null, + 'default' => null, + 'isPrimary' => true, + 'isAutoincrement' => true, + 'isUnsigned' => false, + 'isNullable' => false, + 'meta' => ['sequence' => 'public.eans_id_seq'], + ], + 'code' => [ + 'name' => 'code', + 'type' => 'VARCHAR', + 'size' => 50, + 'default' => null, + 'isPrimary' => false, + 'isAutoincrement' => false, + 'isUnsigned' => false, + 'isNullable' => false, + 'meta' => [], + ], + ], $identityColumns); + $schemaColumns = $this->connection->getPlatform()->getColumns('authors', 'second_schema'); $schemaColumns = \array_map(function ($column) { return (array) $column; }, $schemaColumns); @@ -217,7 +245,7 @@ class PlatformPostgresTest extends IntegrationTestCase public function testPrimarySequence() { - Assert::same('books_id_seq', $this->connection->getPlatform()->getPrimarySequenceName('books')); + Assert::same('public.books_id_seq', $this->connection->getPlatform()->getPrimarySequenceName('books')); } diff --git a/tests/data/pgsql-init.sql b/tests/data/pgsql-init.sql index 1c60abf1..b9f07a09 100644 --- a/tests/data/pgsql-init.sql +++ b/tests/data/pgsql-init.sql @@ -22,7 +22,7 @@ CREATE TABLE "tags" ( ); CREATE TABLE "eans" ( - "id" SERIAL4 NOT NULL, + "id" INT4 NOT NULL GENERATED ALWAYS AS IDENTITY, "code" varchar(50) NOT NULL, PRIMARY KEY("id") );