From 68b5204a022781d777b2bce86a93873f3b05e105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 12 Oct 2026 14:49:33 +0200 Subject: [PATCH] IBX-11939: Replaced the SERIAL conversion loop with plain SQL statements (#166) * IBX-11939: Replaced the SERIAL conversion loop with plain SQL statements sql/convert-serial-columns-to-identity-postgresql.sql was a PL/pgSQL DO block that looped over the columns, read each one's sequence from information_schema and skipped a column that was no longer SERIAL. It now holds the plain statements for each column, so it can be read and run by hand. The check moved to the migration. Doctrine DBAL 4 reports only identity columns as autoincrement, and PostgreSQL runs the migration in one transaction, so the first column tells whether the columns are converted already, as on a database a 6.0 SchemaBuilderEvent install created. --- ...greSqlSerialColumnsToIdentityMigration.php | 25 ++++++++-- ...-serial-columns-to-identity-postgresql.sql | 48 ++++++++----------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/bundle/Migration/ConvertPostgreSqlSerialColumnsToIdentityMigration.php b/src/bundle/Migration/ConvertPostgreSqlSerialColumnsToIdentityMigration.php index 2fcbeb6..038f159 100644 --- a/src/bundle/Migration/ConvertPostgreSqlSerialColumnsToIdentityMigration.php +++ b/src/bundle/Migration/ConvertPostgreSqlSerialColumnsToIdentityMigration.php @@ -23,8 +23,9 @@ * sequence. The install migrations, which a 6.0 install runs too, are the ones from 4.6 and 5.0, so * they created these as SERIAL, and a database upgraded from 5.0 keeps them. * sql/convert-serial-columns-to-identity-postgresql.sql turns each SERIAL column into an identity - * column that carries on from its sequence's current value, drops the old sequence, and skips a - * column that's no longer SERIAL. + * column that carries on from its sequence's current value, and drops the old sequence. The file is + * skipped when the columns are identity columns already, as on a database a 6.0 SchemaBuilderEvent + * install created. * * This is kept apart from the other 6.0 migrations on purpose: SERIAL columns work the same for * inserts, so leaving this one out keeps them as they are. @@ -50,8 +51,24 @@ public function up(Schema $schema): void { $this->abortIfUnsupportedPlatform(SqlPlatform::MYSQL, SqlPlatform::MARIADB, SqlPlatform::POSTGRESQL, SqlPlatform::SQLITE); - if ($this->isPostgreSQL()) { - $this->addSqlFile(__DIR__ . '/sql/convert-serial-columns-to-identity-postgresql.sql'); + if (!$this->isPostgreSQL()) { + return; } + + if ($this->areColumnsConverted($schema)) { + return; + } + + $this->addSqlFile(__DIR__ . '/sql/convert-serial-columns-to-identity-postgresql.sql'); + } + + /** + * Doctrine DBAL 4 reports only identity columns as autoincrement; a SERIAL column comes back + * with its nextval() default instead. PostgreSQL runs the migration in one transaction, so the + * columns are either all converted or none is, and the first one tells which. + */ + private function areColumnsConverted(Schema $schema): bool + { + return $schema->getTable('ibexa_user_invitation')->getColumn('id')->getAutoincrement(); } } diff --git a/src/bundle/Migration/sql/convert-serial-columns-to-identity-postgresql.sql b/src/bundle/Migration/sql/convert-serial-columns-to-identity-postgresql.sql index 39359f0..c226134 100644 --- a/src/bundle/Migration/sql/convert-serial-columns-to-identity-postgresql.sql +++ b/src/bundle/Migration/sql/convert-serial-columns-to-identity-postgresql.sql @@ -1,28 +1,20 @@ -DO $$ -DECLARE - serial_column record; - sequence_name text; - old_last_value bigint; - old_is_called boolean; -BEGIN - FOR serial_column IN SELECT * FROM (VALUES - ('ibexa_user_invitation', 'id'), - ('ibexa_user_invitation_assignment', 'id') - ) AS c (table_name, column_name) - LOOP - -- A SERIAL column defaults to nextval(''::regclass); an identity column has no default. - SELECT substring(column_default FROM 'nextval\(''([^'']+)''::regclass\)') INTO sequence_name - FROM information_schema.columns - WHERE table_schema = current_schema() - AND table_name = serial_column.table_name - AND column_name = serial_column.column_name; - CONTINUE WHEN sequence_name IS NULL; - - EXECUTE format('SELECT last_value, is_called FROM %s', sequence_name) INTO old_last_value, old_is_called; - EXECUTE format('ALTER TABLE %I ALTER %I DROP DEFAULT', serial_column.table_name, serial_column.column_name); - EXECUTE format('DROP SEQUENCE %s', sequence_name); - EXECUTE format('ALTER TABLE %I ALTER %I ADD GENERATED BY DEFAULT AS IDENTITY', serial_column.table_name, serial_column.column_name); - PERFORM setval(pg_get_serial_sequence(serial_column.table_name, serial_column.column_name), old_last_value, old_is_called); - END LOOP; -END -$$; +ALTER TABLE ibexa_user_invitation ALTER id DROP DEFAULT; +-- ibexa:sql-statement-separator +ALTER SEQUENCE ibexa_user_invitation_id_seq RENAME TO ibexa_user_invitation_id_seq_old; +-- ibexa:sql-statement-separator +ALTER TABLE ibexa_user_invitation ALTER id ADD GENERATED BY DEFAULT AS IDENTITY; +-- ibexa:sql-statement-separator +SELECT setval('ibexa_user_invitation_id_seq', last_value, is_called) FROM ibexa_user_invitation_id_seq_old; +-- ibexa:sql-statement-separator +DROP SEQUENCE ibexa_user_invitation_id_seq_old; +-- ibexa:sql-statement-separator +ALTER TABLE ibexa_user_invitation_assignment ALTER id DROP DEFAULT; +-- ibexa:sql-statement-separator +ALTER SEQUENCE ibexa_user_invitation_assignment_id_seq RENAME TO ibexa_user_invitation_assignment_id_seq_old; +-- ibexa:sql-statement-separator +ALTER TABLE ibexa_user_invitation_assignment ALTER id ADD GENERATED BY DEFAULT AS IDENTITY; +-- ibexa:sql-statement-separator +SELECT setval('ibexa_user_invitation_assignment_id_seq', last_value, is_called) + FROM ibexa_user_invitation_assignment_id_seq_old; +-- ibexa:sql-statement-separator +DROP SEQUENCE ibexa_user_invitation_assignment_id_seq_old;