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 foo ADD CONSTRAINT bar UNIQUE (a) SQL to pgroll operation #507

Merged
merged 4 commits into from
Dec 4, 2024

Conversation

andrew-farries
Copy link
Collaborator

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

Convert SQL DDL of the form:

"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a)"

To the equivalent pgroll operation:

[
  {
    "create_constraint": {
      "type": "unique",
      "table": "foo",
      "name": "bar",
      "columns": ["a"],
      "up": {
        "a": "...",
      },
      "down": {
        "a": "..."
      }
    }
  }
]

We need to be conservative when converting SQL statements to pgroll operations to ensure that information present in the SQL is not lost during the conversion.

There are several options possible as part of ADD CONSTRAINT ... UNIQUE statements that aren't currently representable by the OpCreateConstraint operation, for example:

ALTER TABLE foo ADD CONSTRAINT bar UNIQUE NULLS NOT DISTINCT (a)
ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a) INCLUDE (b)

In these cases we must resort to converting to an OpRawSQL.

Tests are added to cover these unrepresentable cases.

Part of #504

Convert SQL DDL of the form:

```sql
"ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a)"
```

To the equivalent `pgroll` operation:

```json
[
  {
    "create_constraint": {
      "type": "unique",
      "table": "foo",
      "name": "bar",
      "columns": ["a"],
      "up": {
        "a": "...",
      },
      "down": {
        "a": "..."
      }
    }
  }
]
```
We need to be conservative when converting SQL statements to `pgroll`
operations to ensure that information present in the SQL is not lost
during the conversion.

There are several options possible as part of `ADD CONSTRAINT ...
UNIQUE` statements that aren't representable by the `OpCreateConstraint`
operation; in these cases we must resort to converting to an `OpRawSQL`.
@andrew-farries andrew-farries marked this pull request as ready for review December 4, 2024 10:14
@andrew-farries andrew-farries merged commit 06bfebd into main Dec 4, 2024
27 checks passed
@andrew-farries andrew-farries deleted the sql2pgroll-alter-column-add-unique-constraint branch December 4, 2024 11:07
@andrew-farries andrew-farries added the sql2pgroll Issues relating to the sql2pgroll package label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql2pgroll Issues relating to the sql2pgroll package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants