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

Add support for exclusion constraints in create_table operation #624

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

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Jan 24, 2025

This PR adds a new type of constraint to create_table operation called exclude to support exclusion constraints.

New options:

  • exclude.index_method: name of the index method (e.g. btree, gist, etc.)
  • exclude.elements: exlusion elements
  • exclude.predicate: WHERE clause for partial indices

Example constraint forbidding overlap in room reservations:

{
  "name": "52_create_table_with_table_exclusion_constraint",
  "operations": [
    {
      "create_table": {
        "name": "room_reservations",
        "columns": [
          {
            "name": "id",
            "type": "serial"
          },
          {
            "name": "start",
            "type": "timestamp"
          },
          {
            "name": "end",
            "type": "timestamp"
          },
          {
            "name": "canceled",
            "type": "boolean",
            "default": "false"
          }
        ],
        "constraints": [
          {
            "name": "forbid_double_booking",
            "type": "exclude",
            "exclude": {
              "index_method": "gist",
              "elements": "id WITH =, tsrange(\"start\", \"end\") WITH &&",
              "predicate": "NOT canceled"
            }
          }
        ]
      }
    }
  ]
}

The tests I added require the extension btree_gist. So I added it for make examples and running go test.

go.mod Outdated Show resolved Hide resolved
@kvch kvch marked this pull request as ready for review January 24, 2025 12:42
@kvch kvch requested a review from andrew-farries January 24, 2025 12:42
@kvch kvch enabled auto-merge (squash) January 24, 2025 12:42
Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

I wonder whether we can do without the need for the btree_gist extension though.

@@ -220,6 +220,7 @@ jobs:
if [ "$PGROLL_SCHEMA" != "public" ]; then
psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U postgres -c "CREATE SCHEMA $PGROLL_SCHEMA;"
fi
psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U postgres -c "CREATE EXTENSION IF NOT EXISTS btree_gist SCHEMA $PGROLL_SCHEMA;"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make a similar change to the local docker compose setup too:

docker compose up
make examples

now fails because the btree_gist extension isn't installed.

@@ -0,0 +1,56 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to give an example migration that doesn't require the btree_gist extension?

All the example migrations so far run against a vanilla Postgres - this is the first one that requires an extension.

@@ -1020,6 +1020,191 @@ func TestCreateTable(t *testing.T) {
}, rows)
},
},
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could potentially just keep the first of these tests, which wouldn't require the extension.

Do you think that still provides enough coverage?

@kvch
Copy link
Contributor Author

kvch commented Jan 27, 2025

I think the simple test provides enough coverage already. I am happy to remove the more complex tests. \o/

@kvch kvch requested a review from andrew-farries January 27, 2025 12:12
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