Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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('<sequence>'::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;
Loading