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

fix: EXPOSED-696 [PostgreSQL] Drop of auto-increment sequence fails after column modified without dropping default #2369

Merged
merged 1 commit into from
Jan 28, 2025

Conversation

bog-walk
Copy link
Member

Description

Summary of the change:
Add ALTER COLUMN ... DROP DEFAULT string to modified columns that lose their auto-increment status.

Detailed description:

  • Why:

When a column is modified to no longer be auto-incrementing, MigrationUtils.statementsRequiredForDatabaseMigration() currently generates statements to ALTER the column type and DROP the associated sequence. If these statements are actually executed using PostgreSQL, they will fail with an exception like:

ERROR: cannot drop sequence test_table_id_seq because other objects depend on it
  Detail: default value for column id of table test_table depends on sequence test_table_id_seq
  Hint: Use DROP ... CASCADE to drop the dependent objects too.. Statement(s): DROP SEQUENCE IF EXISTS test_table_id_seq 

Before dropping the sequence, the default value of the no-longer-auto-incrementing column must also be dropped to ensure that the column no longer depends on it.

Note about Defaults:

Technically, metadata results correctly return a mismatch, with the existing column in the database having a default of something like nextval('test_table_id_seq'::regclass). But this line purposefully ignores the value, resulting in a technically incorrect ColumnDiff.defauts == false, so the necessary string isn't currently appended.

That line currently cannot be changed as it leads to failures in many other parts of the source code that depend on it.


Type of Change

Please mark the relevant options with an "X":

  • Bug fix

Affected databases:

  • Postgres

Checklist

  • Unit tests are in place
  • The build is green (including the Detekt check)
  • All public methods affected by my PR has up to date API docs
  • Documentation for my change is up to date

Related Issues

EXPOSED-696
EXPOSED-691

Comment on lines +411 to +415
} else if (columnDiff.autoInc && column.autoIncColumnType == null) {
// based on logic in SchemaUtils.isIncorrectAutoInc this should only be possible if the existing
// column in database is auto-incrementing while defined table is not
append("ALTER COLUMN $colName TYPE ${column.columnType.sqlType()}")
append(", ALTER COLUMN $colName DROP DEFAULT")
Copy link
Member Author

Choose a reason for hiding this comment

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

Link to SchemaUtils.isIncorrectAutoInc that determines value of columnDiff.autoInc.

Also, I didn't keep the test for it, but I did confirm that appending the new string here does not affect migration to a new default, as logic further down would append ALTER COLUMN ... SET DEFAULT ... if needed. Meaning migrating from Column.autoIncrement() --> Column.default() would still succeed.

@bog-walk bog-walk requested review from e5l and obabichevjb January 22, 2025 23:47
…fter column modified without dropping default

DDL generated by MigrationUtils when a column is modified to lose its auto-increment
status was not dropping the column's default. This meant that attemtping to drop
the auto-increment status after would result in an exception because the sequence
was still being used by the altered column.

This ensures that the column is altered to drop its default if the existing database
column is auto-incrementing while the defined column isn't.
@bog-walk bog-walk force-pushed the bog-walk/fix-drop-autoincrement-sequence branch from 67fcf06 to 3c456e6 Compare January 28, 2025 20:34
@bog-walk bog-walk merged commit e622c7c into main Jan 28, 2025
5 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-drop-autoincrement-sequence branch January 28, 2025 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants