Skip to content

Commit a0a552c

Browse files
authored
Merge pull request #236 from jiripudil/233-pg-identity-column
Support PostgreSQL identity columns
2 parents a6d998d + bcf9ab1 commit a0a552c

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/Platforms/PostgreSqlPlatform.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ public function getColumns(string $table, ?string $schema = null): array
8888
CASE WHEN a.atttypmod = -1 THEN NULL ELSE a.atttypmod -4 END AS size,
8989
pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass)::varchar AS default,
9090
COALESCE(co.contype = 'p', FALSE) AS is_primary,
91-
COALESCE(co.contype = 'p' AND strpos(pg_get_expr(ad.adbin, ad.adrelid), 'nextval') = 1, FALSE) AS is_autoincrement,
91+
COALESCE(co.contype = 'p' AND (strpos(pg_get_expr(ad.adbin, ad.adrelid), 'nextval') = 1 OR a.attidentity != ''), FALSE) AS is_autoincrement,
9292
NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS is_nullable,
93-
SUBSTRING(pg_catalog.pg_get_expr(ad.adbin, 'pg_catalog.pg_attrdef'::regclass) FROM %s) AS sequence
93+
") . (
94+
count($tableArgs) > 1
95+
? "pg_get_serial_sequence('%table.%table', a.attname) AS sequence"
96+
: "pg_get_serial_sequence('%table', a.attname) AS sequence"
97+
)
98+
. (/** @lang GenericSQL */ "
9499
FROM
95100
pg_catalog.pg_attribute AS a
96101
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
108113
AND NOT a.attisdropped
109114
ORDER BY
110115
a.attnum
111-
"), "nextval[(]'\"?([^'\"]+)", ...$tableArgs);
116+
"), ...$tableArgs, ...$tableArgs);
112117

113118
$columns = [];
114119
foreach ($result as $row) {

tests/cases/integration/platform.postgres.phpt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PlatformPostgresTest extends IntegrationTestCase
5050
'isAutoincrement' => true,
5151
'isUnsigned' => false,
5252
'isNullable' => false,
53-
'meta' => ['sequence' => 'books_id_seq'],
53+
'meta' => ['sequence' => 'public.books_id_seq'],
5454
],
5555
'author_id' => [
5656
'name' => 'author_id',
@@ -109,6 +109,34 @@ class PlatformPostgresTest extends IntegrationTestCase
109109
],
110110
], $columns);
111111

112+
$identityColumns = $this->connection->getPlatform()->getColumns('eans');
113+
$identityColumns = \array_map(function ($column) { return (array) $column; }, $identityColumns);
114+
115+
Assert::same([
116+
'id' => [
117+
'name' => 'id',
118+
'type' => 'INT4',
119+
'size' => null,
120+
'default' => null,
121+
'isPrimary' => true,
122+
'isAutoincrement' => true,
123+
'isUnsigned' => false,
124+
'isNullable' => false,
125+
'meta' => ['sequence' => 'public.eans_id_seq'],
126+
],
127+
'code' => [
128+
'name' => 'code',
129+
'type' => 'VARCHAR',
130+
'size' => 50,
131+
'default' => null,
132+
'isPrimary' => false,
133+
'isAutoincrement' => false,
134+
'isUnsigned' => false,
135+
'isNullable' => false,
136+
'meta' => [],
137+
],
138+
], $identityColumns);
139+
112140
$schemaColumns = $this->connection->getPlatform()->getColumns('authors', 'second_schema');
113141
$schemaColumns = \array_map(function ($column) { return (array) $column; }, $schemaColumns);
114142

@@ -217,7 +245,7 @@ class PlatformPostgresTest extends IntegrationTestCase
217245

218246
public function testPrimarySequence()
219247
{
220-
Assert::same('books_id_seq', $this->connection->getPlatform()->getPrimarySequenceName('books'));
248+
Assert::same('public.books_id_seq', $this->connection->getPlatform()->getPrimarySequenceName('books'));
221249
}
222250

223251

tests/data/pgsql-init.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CREATE TABLE "tags" (
2222
);
2323

2424
CREATE TABLE "eans" (
25-
"id" SERIAL4 NOT NULL,
25+
"id" INT4 NOT NULL GENERATED ALWAYS AS IDENTITY,
2626
"code" varchar(50) NOT NULL,
2727
PRIMARY KEY("id")
2828
);

0 commit comments

Comments
 (0)