Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Postgres native identity column support #5257

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function supportsPartialIndexes()
*/
public function usesSequenceEmulatedIdentityColumns()
{
return true;
return false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that this isn't fine, and is a BC break?

Copy link
Member

@morozov morozov Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A BC break will happen when someone upgrades to the version of the DBAL that uses IDENTITY instead of sequences and has their sequence values reset. See #4744 (comment) for more details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be handled in the \Doctrine\DBAL\Schema\PostgreSqlSchemaManager, when we detect going from a non-native identity autoinc column, to a native one?

Copy link
Member

@morozov morozov Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two ways here:

  1. We do it in the schema manager as you suggested. This way, the upgrade process will be implemented in the library, so the change won't be breaking and could be accepted in a minor release.
  2. We do it via documentation and a separate upgrade tool. This way, it will be a breaking change.

Both approaches have their pros and cons. BTW, apparently, I prototyped the same thing back in August: 3.4.x...morozov:pgsql-identity.

}

/**
Expand Down Expand Up @@ -413,7 +413,8 @@ public function getListTableColumnsSQL($table, $database = null)
) AS default,
(SELECT pg_description.description
FROM pg_description WHERE pg_description.objoid = c.oid AND a.attnum = pg_description.objsubid
) AS comment
) AS comment,
a.attidentity AS identity
FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n
WHERE " . $this->getTableWhereClause($table, 'c', 'n') . '
AND a.attnum > 0
Expand Down Expand Up @@ -920,7 +921,7 @@ public function getBooleanTypeDeclarationSQL(array $column)
public function getIntegerTypeDeclarationSQL(array $column)
{
if (! empty($column['autoincrement'])) {
return 'SERIAL';
return 'INT GENERATED ALWAYS AS IDENTITY';
}

return 'INT';
Expand Down
4 changes: 3 additions & 1 deletion src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ protected function _getPortableTableColumnDefinition($tableColumn)

$autoincrement = false;

if (
if (in_array($tableColumn['identity'], ['a', 'd'])) {
$autoincrement = true;
} elseif (
$tableColumn['default'] !== null
&& preg_match("/^nextval\('(.*)'(::.*)?\)$/", $tableColumn['default'], $matches) === 1
) {
Expand Down