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 drop NOT NULL operation to remove NOT NULL from a column #258

Merged
merged 11 commits into from
Feb 1, 2024

Conversation

andrew-farries
Copy link
Collaborator

@andrew-farries andrew-farries commented Jan 31, 2024

Add a new sub-operation to the 'alter column' operation to drop NOT NULL constraints from columns.

Currently, it is only possible to set NOT NULL constraints on columns but not remove them. The syntax for doing this is:

{
  "name": "16_set_nullable",
  "operations": [
    {
      "alter_column": {
        "table": "reviews",
        "column": "review",
        "nullable": false,
        "up": "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)",
        "down": "review"
      }
    }
  ]
}

Setting nullable: true in the above migration would result in a validation error saying that removing NOT NULL constraints was not supported. This PR removes this restriction and allows nullable: true to remove an existing NOT NULL constraint.

A migration to remove a NOT NULL constraint looks like:

{
  "name": "31_unset_not_null",
  "operations": [
    {
      "alter_column": {
        "table": "posts",
        "column": "title",
        "nullable": true,
        "up": "title",
        "down": "(SELECT CASE WHEN title IS NULL THEN 'placeholder title' ELSE title END)"
      }
    }
  ]
}

The differences between adding and removing a NOT NULL constraint are:

  • nullable: true vs nullable: false
  • The roles of up and down SQL are reversed; down now needs to rewrite any NULLs to meet the NOT NULL constraint on the old column. up defaults to a simple copy of the data from the old column to the new.

Fixes #223

@andrew-farries andrew-farries changed the title Add 'drop NOT NULL operation to remove NOT NULL from a column Add drop NOT NULL operation to remove NOT NULL from a column Jan 31, 2024
To make it clear that they are setting a column `NOT NULL`, rather than
removing `NOT NULL`.
And run `make generate`.
Allow for duplication of a column ignoring any `NOT NULL` constraints
present on the column.
Ensure that duplicated columns that were intentionally duplicated
without a `NOT NULL` constraint don't have `NOT NULL` reinstated when
renaming the duplicated column back to the original name.
@andrew-farries andrew-farries enabled auto-merge (squash) February 1, 2024 07:07
@andrew-farries andrew-farries merged commit b9e7819 into main Feb 1, 2024
26 checks passed
@andrew-farries andrew-farries deleted the add-drop-not-null-operation branch February 1, 2024 07:08
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.

Support removal of not null constraint
2 participants