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

New operation: create_constraint and support unique constraints with multiple columns #459

Merged
merged 69 commits into from
Nov 13, 2024

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Nov 11, 2024

This PR adds a new operation named create_constraint. Previously, we only supported adding constraints to columns. This operation lets us define table level constraints including multiple columns.

The operation supports unique constraints for now. In a follow-up PR I am adding foreign_key and check constraints as well.

Unique

{
  "name": "44_add_table_unique_constraint",
  "operations": [
    {
      "create_constraint": {
        "type": "unique",
        "table": "tickets",
        "name": "unique_zip_name",
        "columns": [
          "sellers_name",
          "sellers_zip"
        ],
        "up": {
          "sellers_name": "sellers_name",
          "sellers_zip": "sellers_zip"
        },
        "down": {
          "sellers_name": "sellers_name",
          "sellers_zip": "sellers_zip"
        }
      }
    }
  ]
}

Related

Created from #411

kvch and others added 30 commits October 16, 2024 16:15
…o#449)

Allow the `add_column` operation to add a column to a table that was
created by an operation earlier in the same migration.

The following migration would previously have failed to start:

```json
{
  "name": "43_multiple_ops",
  "operations": [
    {
      "create_table": {
        "name": "players",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "name",
            "type": "varchar(255)",
            "check": {
              "name": "name_length_check",
              "constraint": "length(name) > 2"
            }
          }
        ]
      }
    },
    {
      "add_column": {
        "table": "players",
        "column": {
          "name": "rating",
          "type": "integer",
          "comment": "hello world",
          "check": {
            "name": "rating_check",
            "constraint": "rating > 0 AND rating < 100"
          },
          "nullable": false
        }
      }
    }
  ]
}
```

As of this PR, the migration can be started.

The above migration does not validate yet, but it can be started
successfully with the `--skip-validation` flag to the `start` command.

Part of xataio#239
…o#451)

Allow the `create_index` operation to add an index to a table that was
created by an operation earlier in the same migration.

The following migration would previously have failed to start:

```json
{
  "name": "43_multiple_ops",
  "operations": [
    {
      "create_table": {
        "name": "players",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "name",
            "type": "varchar(255)",
            "check": {
              "name": "name_length_check",
              "constraint": "length(name) > 2"
            }
          }
        ]
      }
    },
    {
      "create_index": {
        "name": "idx_player_name",
        "table": "players",
        "columns": [
          "name"
        ]
      }
    }
  ]
}
```

As of this PR the migration can be started.

The above migration does not validate yet, but it can be started
successfully with the --skip-validation flag to the start command.

Part of xataio#239
…io#452)

Add a healthcheck for the `db` service so that scripts can wait for
Postgres to be ready to accept connections, eg:

```sh
#! /bin/sh

docker compose up -d --wait
# ... other commands now that Postgres is ready
```
@kvch
Copy link
Contributor Author

kvch commented Nov 12, 2024

@andrew-farries I have updated the PR with backfilling. The example migration file has changed, let me know what you think of this format. (I am not really happy with it, but this was my best idea :D)

@kvch kvch requested a review from andrew-farries November 12, 2024 11:26
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.

This is looking good. Left some comments.

examples/44_add_table_unique_constraint.json Outdated Show resolved Hide resolved
examples/44_add_table_unique_constraint.json Outdated Show resolved Hide resolved
docs/README.md Outdated Show resolved Hide resolved
pkg/migrations/op_create_constraint.go Outdated Show resolved Hide resolved
pkg/migrations/op_create_constraint.go Outdated Show resolved Hide resolved
examples/44_add_table_unique_constraint.json Show resolved Hide resolved
pkg/migrations/op_create_constraint.go Outdated Show resolved Hide resolved
pkg/migrations/op_create_constraint.go Outdated Show resolved Hide resolved
pkg/migrations/op_create_constraint.go Outdated Show resolved Hide resolved
pkg/migrations/op_create_constraint_test.go Outdated Show resolved Hide resolved
kvch and others added 6 commits November 12, 2024 14: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>
@kvch kvch requested a review from andrew-farries November 12, 2024 14:55
schema.json Outdated Show resolved Hide resolved
schema.json Outdated Show resolved Hide resolved
schema.json Outdated Show resolved Hide resolved
pkg/migrations/op_create_constraint.go Outdated Show resolved Hide resolved
docs/README.md Outdated Show resolved Hide resolved
kvch and others added 4 commits November 13, 2024 10:21
Co-authored-by: Andrew Farries <andyrb@gmail.com>
Co-authored-by: Andrew Farries <andyrb@gmail.com>
@kvch kvch requested a review from andrew-farries November 13, 2024 10:23
@kvch kvch requested a review from andrew-farries November 13, 2024 13:08
@kvch kvch merged commit a5fb72e into xataio:main Nov 13, 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.

3 participants