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

Support multiple sub-operations in one 'alter column' migration #336

Closed
andrew-farries opened this issue Apr 10, 2024 · 0 comments · Fixed by #338
Closed

Support multiple sub-operations in one 'alter column' migration #336

andrew-farries opened this issue Apr 10, 2024 · 0 comments · Fixed by #338
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@andrew-farries
Copy link
Collaborator

Alter column operations currently support only one 'sub-operation' at a time. So, for example, changing a column's name and setting it NOT NULL within the same operation is not supported:

{
  "name": "35_alter_column_multiple",
  "operations": [
    {
      "alter_column": {
        "table": "events",
        "column": "name",
        "name": "username",
        "nullable": false,
        "up": "(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)"
      }
    }
  ]
}

This migration will fail with:

alter column operations require exactly one change, found 2

Alter column operations should change to allow more than one sub-operation at at time.

@andrew-farries andrew-farries added the enhancement New feature or request label Apr 10, 2024
@andrew-farries andrew-farries added this to the v1 milestone Apr 10, 2024
@andrew-farries andrew-farries self-assigned this Apr 10, 2024
andrew-farries added a commit that referenced this issue Apr 10, 2024
Remove duplication between 'alter column' sub-operations. Pull:

* column duplication and trigger creation on migration start
* column rename and trigger removal on complete
* trigger and column drop on rollback

up to the parent 'alter column' operation. This removes a lot of
duplicated code from the sub-operations and will make it easier to
support multiple sub-operations in one 'alter column' operation.

Part of #336
andrew-farries added a commit that referenced this issue Apr 22, 2024
Allow 'alter column' operations to include multiple sub-operations. This
means migrations like this one are now possible:

```json
{
  "name": "35_alter_column_multiple",
  "operations": [
    {
      "alter_column": {
        "table": "events",
        "column": "name",
        "name": "event_name",
        "type": "text",
        "nullable": false,
        "unique": {
          "name": "events_event_name_unique"
        },
        "check": {
          "name": "event_name_length",
          "constraint": "length(name) > 3"
        },
        "up": "(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)",
        "down": "name"
      }
    }
  ]
}
```

This 'alter column' operation:
* Renames a column
* Changes its type
* Sets it `NOT NULL`
* Adds a unique constraint
* Adds a check constraint

Previously, this would have required 5 different operations.

Builds on #337. Fixes
#336
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant