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

Merged
merged 71 commits into from
Jan 28, 2025

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.

.github/workflows/build.yml Outdated Show resolved Hide resolved
pkg/migrations/op_create_table_test.go Show resolved Hide resolved
@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
@kvch kvch merged commit 05ab88c into xataio:main Jan 28, 2025
28 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