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

Altering a column type attempts to duplicate incompatible defaults and constraints #348

Closed
andrew-farries opened this issue May 2, 2024 · 0 comments · Fixed by #349
Closed
Labels
bug Something isn't working
Milestone

Comments

@andrew-farries
Copy link
Collaborator

Columns that are duplicated as part of an 'alter column' migration that changes the column type should not attempt to duplicate any DEFAULTs or CHECK constraints that are not compatible with the new column type.

Given this pair of migrations:

{
  "name": "01_create_table",
  "operations": [
    {
      "create_table": {
        "name": "products",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "age",
            "type": "text",
            "check": {
              "name": "length_check",
              "constraint": "LENGTH(\"age\") <= 3"
            }
          }
        ]
      }
    }
  ]
}
{
  "name": "02_alter_column",
  "operations": [
    {
      "alter_column": {
        "table": "products",
        "column": "age",
        "type": "bigint",
        "default": "0",
        "up": "CAST(age AS integer)",
        "down": "CAST(age AS text)"
      }
    }
  ]
}

will fail because the length_check constraint can't be applied to the new column type. A similar problem occurs if the column has a string default and the column type is changed to an integer.

As a first pass, it could be acceptable to simply drop defaults and constraints from columns that have their type changed; users can re-set them in the same 'alter column' operation that changes the type if they are intended to be preserved.

Ideally the default/constraints should be dropped if they are not compatible with the new column type and preserved otherwise.

@andrew-farries andrew-farries added the bug Something isn't working label May 2, 2024
@andrew-farries andrew-farries added this to the v1 milestone May 2, 2024
andrew-farries added a commit that referenced this issue May 8, 2024
…n type (#349)

When a column is duplicated with a new type, check constraints and
defaults defined on the column *may* be incompatible with the new type.

In this case column duplication should not fail, but rather the
incompatible constraints and defaults should be ignored and not be
duplicated to the new column.

This PR changes column duplication to ignore errors on `DEFAULT` and
`CHECK` constraint duplication that look as though they are caused by a
change of column type.

Fixes #348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant