From 46dd4b44e5b0cbefbdf0b7d9de693f1a0525ff9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= Date: Fri, 22 Mar 2024 14:35:32 +0100 Subject: [PATCH 1/2] failing test --- .../cases/integration/platform.postgres.phpt | 28 +++++++++++++++++++ tests/data/pgsql-init.sql | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/cases/integration/platform.postgres.phpt b/tests/cases/integration/platform.postgres.phpt index e60b8a5e..0ac4c7da 100644 --- a/tests/cases/integration/platform.postgres.phpt +++ b/tests/cases/integration/platform.postgres.phpt @@ -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' => '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); 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") ); From bcf9ab130ce2999331eeb2e8719c9b90fb3f9aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= Date: Fri, 22 Mar 2024 15:05:08 +0100 Subject: [PATCH 2/2] support postgres identity columns --- src/Platforms/PostgreSqlPlatform.php | 11 ++++++++--- tests/cases/integration/platform.postgres.phpt | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) 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 0ac4c7da..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', @@ -122,7 +122,7 @@ class PlatformPostgresTest extends IntegrationTestCase 'isAutoincrement' => true, 'isUnsigned' => false, 'isNullable' => false, - 'meta' => ['sequence' => 'eans_id_seq'], + 'meta' => ['sequence' => 'public.eans_id_seq'], ], 'code' => [ 'name' => 'code', @@ -245,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')); }