Skip to content

Commit

Permalink
Merge pull request #236 from jiripudil/233-pg-identity-column
Browse files Browse the repository at this point in the history
Support PostgreSQL identity columns
  • Loading branch information
hrach authored Mar 22, 2024
2 parents a6d998d + bcf9ab1 commit a0a552c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
32 changes: 30 additions & 2 deletions tests/cases/integration/platform.postgres.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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'));
}


Expand Down
2 changes: 1 addition & 1 deletion tests/data/pgsql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
Expand Down

0 comments on commit a0a552c

Please sign in to comment.