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

Support table and column rename operations preceding create_constraint operations #674

Merged
merged 6 commits into from
Feb 11, 2025

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Feb 11, 2025

Add support for allowing the table and column on which a create_constraint operation acts to be renamed by preceding operations in the same migration. For example, ensure that a migration like this works as expected:

{
  "name": "16_multiple_ops",
  "operations": [
    {
      "rename_table": {
        "from": "items",
        "to": "products"
      }
    },
    {
      "rename_column": {
        "table": "products",
        "from": "name",
        "to": "item_name"
      }
    },
    {
      "create_constraint": {
        "table": "products",
        "type": "check",
        "name": "check_item_name",
        "columns": [ "item_name" ],
        "check": "length(item_name) > 3",
        "up": {
          "item_name": "item_name || '-from-up'"
        },
        "down": {
          "item_name": "item_name || '-from-down'"
        }
      }
    }
  ]
}

Here, a three operation migration first renames the items table to products, renames the name field to item_name then adds a CHECK constraint to the item_name field on the products table.

This PR covers the case of adding CHECK constraints only; further (smaller) PRs may be needed for the other constraint types supported by create_constraint.

Part of #239

Ensure that the triggers created on operation start use the correct name
of the table being modified. This ensures that trigger creation is aware
of any table renaming that may have been done by operations earlier in
the same migration.
When adding a check constraint, the physical table name should be used,
not the one specified in the operation, so that table renames are taken
into account.
Use the physical table name when rolling back a constraint creation
instead of the logical table name. A previous rename table operation in
the same migration may have renamed the table, but the physical name of
the table remains unchanged until migration completion.
When duplicating columns for a new constraint, duplicate the columns
using their final names after migration completion.

This ensures that the `Complete` step is able to find the duplicated
columns.
@andrew-farries andrew-farries force-pushed the support-multi-op-create-constraint branch from ad6af4b to caf15c9 Compare February 11, 2025 11:57
@andrew-farries andrew-farries merged commit 1558ef7 into main Feb 11, 2025
28 checks passed
@andrew-farries andrew-farries deleted the support-multi-op-create-constraint branch February 11, 2025 13:28
andrew-farries added a commit that referenced this pull request Feb 11, 2025
…nt` `UNIQUE` operations (#676)

Follow up to #674 which ensured
that `create_constraint` `CHECK` operations could be preceded by rename
table and rename column operations.

This PR ensures that `create_constraint` `UNIQUE` operations can be
preceded by rename table and rename column operations as in the
following example:

```json
{
  "name": "19_multiple_ops",
  "operations": [
    {
      "rename_table": {
        "from": "items",
        "to": "products"
      }
    },
    {
      "rename_column": {
        "table": "products",
        "from": "name",
        "to": "item_name"
      }
    },
    {
      "create_constraint": {
        "table": "products",
        "type": "unique",
        "name": "unique_item_name",
        "columns": ["item_name"],
        "up": {
          "item_name": "item_name || '-from-up'"
        },
        "down": {
          "item_name": "item_name || '-from-down'"
        }
      }
    }
  ]
}
```
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