Skip to content

Add support for alter_table set NOT NULL operations with rename_column operations #607

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

Merged
merged 8 commits into from
Jan 20, 2025

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Jan 17, 2025

Ensure that multi-operation migrations combining alter_column SET NOT NULL and rename_column operations work as expected.

{
  "name": "06_multi_operation",
  "operations": [
    {
      "rename_column": {
        "table": "items",
        "from": "name",
        "to": "item_name"
      }
    },
    {
      "alter_column": {
        "table": "items",
        "column": "item_name",
        "nullable": false,
        "up": "SELECT CASE WHEN item_name IS NULL THEN 'anonymous' ELSE item_name END",
        "down": "item_name || '_from_down_trigger'"
      }
    }
  ]
}

This migration renames a column and then sets the renamed column's nullability.

Previously the migration would fail as the alter_column operation was unaware of the changes made by the preceding operation.

Part of #239

Base automatically changed from support-rename-table-set-not-null to main January 20, 2025 11:01
Use the physical column name when dropping the temporary column during
rollback. This ensures that the correct column is dropped, even if the
column was renamed by a previous operation.
Use the physical column name instead of a name derived from the name
given by the operation when adding a `NOT NULL` constraint.

This ensures that the correct column name is used even if the column has
been renamed by a previous rename operation in the same migration.
Allow specifying the name of the duplicated column, overriding the
default name.
When duplicating the column, use the name of the column taken from the
operation instead of the name of the column in the schema.

This ensures that the `Complete` method can find the duplicated column
under the name that it expects.
Update the in memory schema on completion so that the virtual schema
reflects the column rename.

This allows subsequent Complete steps in other operations to see the
column rename.
Add the new column to the in-memory schema so that Complete steps in
subsequent operations can see the new column.
@andrew-farries andrew-farries force-pushed the support-rename-column-set-not-null branch from 508a839 to 60e201c Compare January 20, 2025 11:04
@andrew-farries andrew-farries merged commit af0eccd into main Jan 20, 2025
28 checks passed
@andrew-farries andrew-farries deleted the support-rename-column-set-not-null branch January 20, 2025 11:10
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.

2 participants