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

Convert ALTER TABLE ... ADD CONSTRAINT ... CHECK SQL to pgroll operation #538

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Dec 16, 2024

Convert SQL statements like:

ALTER TABLE foo ADD CONSTRAINT bar CHECK (age > 0)

to the equivalent pgroll operation:

[
  {
    "create_constraint": {
      "check": "age > 0",
      "columns": [
        "placeholder"
      ],
      "down": {
        "placeholder": "TODO: Implement SQL data migration"
      },
      "name": "bar",
      "table": "foo",
      "type": "check",
      "up": {
        "placeholder": "TODO: Implement SQL data migration"
      }
    }
  }
]

As we don't currently have a reliable way of extracting the columns covered by the constraint from the the CHECK SQL expression, the converted pgroll operation uses placeholders for the columns, up and down fields.

Some forms of the statement are not currently representable by the create_constraint operation. For these a raw SQL migration is generated:

"ALTER TABLE foo ADD CONSTRAINT bar CHECK (age > 0) NO INHERIT",
"ALTER TABLE foo ADD CONSTRAINT bar CHECK (age > 0) NOT VALID",

@andrew-farries andrew-farries force-pushed the convert-add-check-constraint branch from 29f2539 to 3de40ab Compare December 16, 2024 16:12
@andrew-farries andrew-farries marked this pull request as ready for review December 16, 2024 16:31
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