Skip to content

Commit

Permalink
Support multiple 'alter column' sub operations (#338)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrew-farries committed Apr 22, 2024
1 parent a444723 commit a4222d8
Show file tree
Hide file tree
Showing 15 changed files with 583 additions and 201 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ Example **add column** migrations:

An alter column operation alters the properties of a column. The operation supports several sub-operations, described below.

An alter column operation may contain multiple sub-operations. For example, a single alter column operation may rename a column, change its type, and add a check constraint.

#### Rename column

A rename column operation renames a column.
Expand Down
22 changes: 22 additions & 0 deletions examples/34_create_events_table.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "34_create_events_table",
"operations": [
{
"create_table": {
"name": "events",
"columns": [
{
"name": "id",
"type": "serial",
"pk": true
},
{
"name": "name",
"type": "varchar(255)",
"nullable": true
}
]
}
}
]
}
23 changes: 23 additions & 0 deletions examples/35_alter_column_multiple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"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 OR LENGTH(name) <= 3 THEN 'placeholder' ELSE name END)",
"down": "name"
}
}
]
}
2 changes: 1 addition & 1 deletion pkg/jsonschema/testdata/alter-column-3.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets `name` but also sets `up` and `down`.
It sets only `name` but also sets `up` and `down`.

-- alter_column.json --
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/jsonschema/testdata/alter-column-4.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets `name` but also sets `up`.
It sets only `name` but also sets `up`.

-- alter_column.json --
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/jsonschema/testdata/alter-column-5.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets `name` but also sets `down`.
It sets only `name` but also sets `down`.

-- alter_column.json --
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/jsonschema/testdata/alter-column-6.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is an invalid 'alter_column' migration.
This is a valid 'alter_column' migration.
It sets both `name` and `nullable`.

-- alter_column.json --
Expand All @@ -19,4 +19,4 @@ It sets both `name` and `nullable`.
}

-- valid --
false
true
4 changes: 2 additions & 2 deletions pkg/jsonschema/testdata/alter-column-7.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is an invalid 'alter_column' migration.
It sets both `name` and `nullable`.
This is a invalid 'alter_column' migration.
It sets both `name` and `nullable` but does not set `up` or `down`.

-- alter_column.json --
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/jsonschema/testdata/alter-column-8.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is an invalid 'alter_column' migration.
This is a valid 'alter_column' migration.
It sets both `type` and `nullable`.

-- alter_column.json --
Expand All @@ -19,4 +19,4 @@ It sets both `type` and `nullable`.
}

-- valid --
false
true
18 changes: 18 additions & 0 deletions pkg/jsonschema/testdata/alter-column-9.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This is an invalid 'alter_column' migration.
It specifies a table and column but no changes.

-- alter_column.json --
{
"name": "migration_name",
"operations": [
{
"alter_column": {
"table": "reviews",
"column": "review"
}
}
]
}

-- valid --
false
17 changes: 9 additions & 8 deletions pkg/migrations/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ func (e NoDownSQLAllowedError) Error() string {
return "down SQL is not allowed for this operation"
}

type MultipleAlterColumnChangesError struct {
Changes int
}

func (e MultipleAlterColumnChangesError) Error() string {
return fmt.Sprintf("alter column operations require exactly one change, found %d", e.Changes)
}

type BackfillNotPossibleError struct {
Table string
}
Expand Down Expand Up @@ -199,3 +191,12 @@ func (e InvalidOnDeleteSettingError) Error() string {
e.Setting,
)
}

type AlterColumnNoChangesError struct {
Table string
Column string
}

func (e AlterColumnNoChangesError) Error() string {
return fmt.Sprintf("alter column %q on table %q requires at least one change", e.Column, e.Table)
}
Loading

0 comments on commit a4222d8

Please sign in to comment.