Skip to content

Commit

Permalink
Add support for alter_table set NOT NULL operations with `create_…
Browse files Browse the repository at this point in the history
…table` operations (#608)

Ensure that multi-operation migrations combining `alter_column` `SET NOT
NULL` and `create_table` operations work as expected.

```json
{
  "name": "06_multi_operation",
  "operations": [
    {
      "create_table": {
        "name": "items",
        "columns": [
          {
            "name": "id",
            "type": "serial",
            "pk": true
          },
          {
            "name": "name",
            "type": "text",
            "nullable": true
          }
        ]
      }
    },
    {
      "alter_column": {
        "table": "items",
        "column": "name",
        "nullable": false,
        "up": "SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END",
        "down": "name || '_from_down_trigger'"
      }
    }
  ]
}
```

This migration creates a table and then sets a column's `nullability`.

Previously the migration would fail as the `alter_column` operation was
unaware of the changes made by the preceding operation.

Part of #239
  • Loading branch information
andrew-farries authored Jan 20, 2025
1 parent af0eccd commit cec0d80
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/migrations/op_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ func (o *OpCreateTable) updateSchema(s *schema.Schema) *schema.Schema {
Name: col.Name,
Unique: col.Unique,
Nullable: col.Nullable,
Type: col.Type,
}
if col.Pk {
primaryKeys = append(primaryKeys, col.Name)
}
}

uniqueConstraints := make(map[string]*schema.UniqueConstraint, 0)
checkConstraints := make(map[string]*schema.CheckConstraint, 0)
for _, c := range o.Constraints {
Expand Down
67 changes: 67 additions & 0 deletions pkg/migrations/op_set_notnull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,73 @@ func TestSetNotNullInMultiOperationMigrations(t *testing.T) {
TableMustBeCleanedUp(t, db, schema, "products", "item_name")
},
},
{
name: "create table, set not null",
migrations: []migrations.Migration{
{
Name: "01_multi_operation",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "items",
Columns: []migrations.Column{
{
Name: "id",
Type: "int",
Pk: true,
},
{
Name: "name",
Type: "varchar(255)",
Nullable: true,
},
},
},
&migrations.OpAlterColumn{
Table: "items",
Column: "name",
Nullable: ptr(false),
Up: "SELECT CASE WHEN name IS NULL THEN 'unknown' ELSE name END",
Down: "name",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// Can insert a row into the new (only) schema that meets the constraint
MustInsert(t, db, schema, "01_multi_operation", "items", map[string]string{
"id": "1",
"name": "apple",
})

// Can't insert a row into the new (only) schema that violates the constraint
MustNotInsert(t, db, schema, "01_multi_operation", "items", map[string]string{
"id": "2",
}, testutils.CheckViolationErrorCode)

// The new view has the expected rows
rows := MustSelect(t, db, schema, "01_multi_operation", "items")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "apple"},
}, rows)
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// Tht table has been dropped
TableMustNotExist(t, db, schema, "items")
},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// Can insert a row into the new (only) schema that meets the constraint
MustInsert(t, db, schema, "01_multi_operation", "items", map[string]string{
"id": "1",
"name": "banana",
})

// The new view has the expected rows
rows := MustSelect(t, db, schema, "01_multi_operation", "items")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "banana"},
}, rows)
},
},
})
}

Expand Down

0 comments on commit cec0d80

Please sign in to comment.