Skip to content

Add support for creating CHECK constraints with create_constraint #464

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
Nov 18, 2024

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Nov 14, 2024

This PR introduces a new constraint type to create_constraint operation called check. Now it is possible to create check constraints on multiple columns.

Example

{
  "name": "45_add_table_check_constraint",
  "operations": [
    {
      "create_constraint": {
        "type": "check",
        "table": "tickets",
        "name": "check_zip_name",
        "columns": [
          "sellers_name",
          "sellers_zip"
        ],
        "check": "sellers_name ~ 'Alice' AND sellers_zip IS NOT NULL",
        "up": {
          "sellers_name": "Alice",
          "sellers_zip": "(SELECT CASE WHEN sellers_zip IS NOT NULL THEN sellers_zip ELSE '00000' END)"
        },
        "down": {
          "sellers_name": "sellers_name",
          "sellers_zip": "sellers_zip"
        }
      }
    }
  ]
}

@kvch
Copy link
Contributor Author

kvch commented Nov 14, 2024

I've found a bug in column duplication for unique indexes with multiple columns, so the example is failing. I am adding a fix in a separate PR.

@kvch
Copy link
Contributor Author

kvch commented Nov 14, 2024

Bugfix: #466

@kvch
Copy link
Contributor Author

kvch commented Nov 14, 2024

It is highly likely we need to adjust the check constraint migrations as well. I will address those issues in a follow-up PR.

@kvch kvch requested a review from andrew-farries November 14, 2024 14:55
kvch and others added 6 commits November 14, 2024 16:32
Co-authored-by: Andrew Farries <andyrb@gmail.com>
Co-authored-by: Andrew Farries <andyrb@gmail.com>
Co-authored-by: Andrew Farries <andyrb@gmail.com>
Co-authored-by: Andrew Farries <andyrb@gmail.com>
Co-authored-by: Andrew Farries <andyrb@gmail.com>
@andrew-farries
Copy link
Collaborator

I guess this just needs to be rebased once #466 goes in?

kvch added a commit that referenced this pull request Nov 18, 2024
Previously, unique and check constraints with multiple columns were not
duplicated correctly.

We had two issues:
1. Pgroll tried to create the same duplicated index for each renamed
column. From now on pgroll creates the index once with the new column
names.
2. Pgroll tried to convert the unique index to a constraint multiple
times. From now on, the index is only converted once.

Required by #464

---------

Co-authored-by: Andrew Farries <andyrb@gmail.com>
@kvch
Copy link
Contributor Author

kvch commented Nov 18, 2024

Rebased and will merge once the tests pass.

@kvch kvch merged commit d231ee0 into xataio:main Nov 18, 2024
27 checks passed
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