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

Implement Start for adding columns with NOT NULL and no DEFAULT #37

Merged
merged 6 commits into from
Jul 21, 2023

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Jul 20, 2023

Implement Start for add column operations that add a NOT NULL column without a DEFAULT.

To add such a column without forcing a exclusive lock while a full table scan is performed, these steps need to be followed:

On Start:

  1. Add the new column
  2. Add a CHECK IS NOT NULL constraint to the new column, but with NOT VALID, to avoid the scan.
  3. Backfill the new column with the provided up SQL.

On Complete

  1. Validate the constraint (with ALTER TABLE VALIDATE CONSTRAINT).
  2. Add the NOT NULL attribute to the column. The presence of a valid NOT NULL constraint on the column means that adding NOT NULL to the column does not perform a full table scan.

See this post for a summary of these steps.

pkg/migrations/op_add_column_test.go Show resolved Hide resolved
pkg/migrations/op_add_column_test.go Show resolved Hide resolved
Remove the validation restriction that `NOT NULL` columns without
`DEFAULT` are not supported.
Demonstrate adding a `NOT NULL` column without a `DEFAULT`.
Assert that the `INSERT` statement was not successful.
`addCheckConstraint` -> `addNotNullConstraint`
`CheckConstraintName` -> `NotNullConstraintName`

to be more specific and not cause confusion with any future constraints.
@andrew-farries andrew-farries force-pushed the start-not-null-col-with-no-default branch from a1aaed3 to 1715aff Compare July 21, 2023 06:45
@andrew-farries andrew-farries merged commit 9a08b6c into main Jul 21, 2023
2 checks passed
@andrew-farries andrew-farries deleted the start-not-null-col-with-no-default branch July 21, 2023 06:47
andrew-farries added a commit that referenced this pull request Jul 21, 2023
…LT` (#38)

Implement `Rollback` for **add column** operations that add a `NOT NULL`
column without a `DEFAULT`.

Support for starting such an operation was added in
#37. See that PR for a description
of the steps involved.

In order to roll back, there is nothing to be done as the rollback for
the **add column** operation already removes the new column, which also
removes any constraints defined on the column.

This PR adds an `afterRollback` hook to the tests to verify that the
constraint has been dropped.
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