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

Update schema during create table validation #455

Merged
merged 3 commits into from
Nov 8, 2024

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Nov 7, 2024

Update the schema during create_table operation validation so that validation of subsequent operations in the same migration can see the new table.

This means that migrations that add a new table and then perform some other operation, like adding a column, on that table can be validated because the new table is visible to the add_column operation during it's validation phase.

This means that the following migration is now able to validate:

{
  "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": true
        }
      }
    }
  ]
}

Previously, the new table would not have been visible to the add_column operation and its validation would have failed.

In order to make this work the schema needs to be re-read from the target database in between validation and migration start, as the previous assumption that validation would not make changes to the in-memory schema representation no longer holds.

Part of #239

Now that validation can mutate the schema object in memory, we need to
reread the schema object from the database before starting DDL
operations.
Add the new table to the schema during validation so that subsequent
operations in the same migration can assume its existence during their
own validation step.
This commit enforces the validation in the test that was previously
skipped.

This is possible now that the validation of the create table
operation updates the schema with the new table so that the validation
of the subsequent add column operation can be performed.
@andrew-farries andrew-farries marked this pull request as ready for review November 7, 2024 12:39
@andrew-farries andrew-farries merged commit d656387 into main Nov 8, 2024
27 checks passed
@andrew-farries andrew-farries deleted the update-schema-in-create-table-validation branch November 8, 2024 14:26
andrew-farries added a commit that referenced this pull request Nov 8, 2024
Update the schema during `add_column` operation validation so that
validation of subsequent operations in the same migration can see the
new column.

This means that migrations that add a new column and then perform some
other operation, like adding an index, on that column can be validated
because the new column is visible to the `create_index` operation during
it's validation phase.

This means that the following migration is now able to validate:

```json
{
  "name": "43_multiple_ops",
  "operations": [
    {
      "add_column": {
        "table": "players",
        "column": {
          "name": "rating",
          "type": "integer",
          "comment": "hello world",
          "check": {
            "name": "rating_check",
            "constraint": "rating > 0 AND rating < 100"
          },
          "nullable": false
        }
      }
    },
    {
      "create_index": {
        "name": "idx_player_rating",
        "table": "players",
        "columns": [
          "rating"
        ]
      }
    }
  ]
}
```

Previously, the new column would not have been visible to the
`create_index` operation and its validation would have failed.

This PR does for the `add_column` operation what #455 did for the
`create_table` operation.

Part of #239
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