Skip to content

Commit

Permalink
Allow rename-only 'alter-column' operations on unbackfillable columns (
Browse files Browse the repository at this point in the history
…#341)

Allow rename-only 'alter column' operations on unbackfillable columns.
Rename operations don't require backfills so there is no reason to
impose such a restriction.

Fixes #340
  • Loading branch information
andrew-farries authored Apr 24, 2024
1 parent a4222d8 commit 7d8460f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
10 changes: 6 additions & 4 deletions pkg/migrations/op_alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ func (o *OpAlterColumn) Validate(ctx context.Context, s *schema.Schema) error {
return ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
}

// Ensure that the column has a primary key defined on exactly one column.
err := checkBackfill(table)
if err != nil {
return err
// If the operation requires backfills (ie it isn't a rename-only operation),
// ensure that the column meets the requirements for backfilling.
if !o.isRenameOnly() {
if err := checkBackfill(table); err != nil {
return err
}
}

// If the column is being renamed, ensure that the target column name does
Expand Down
29 changes: 27 additions & 2 deletions pkg/migrations/op_alter_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func TestAlterColumnValidation(t *testing.T) {
wantStartErr: migrations.AlterColumnNoChangesError{Table: "posts", Column: "title"},
},
{
name: "table must have a primary key on exactly one column",
name: "if a backfill is required, the table must have a primary key on exactly one column",
migrations: []migrations.Migration{
{
Name: "01_add_table",
Expand All @@ -518,6 +518,31 @@ func TestAlterColumnValidation(t *testing.T) {
},
},
},
{
Name: "02_alter_column",
Operations: migrations.Operations{
&migrations.OpAlterColumn{
Table: "orders",
Column: "quantity",
Nullable: ptr(false),
},
},
},
},
wantStartErr: migrations.BackfillNotPossibleError{Table: "orders"},
},
{
name: "rename-only operations don't have primary key requirements",
migrations: []migrations.Migration{
{
Name: "01_add_table",
Operations: migrations.Operations{
&migrations.OpRawSQL{
Up: "CREATE TABLE orders(id integer, order_id integer, quantity integer)",
Down: "DROP TABLE orders",
},
},
},
{
Name: "02_alter_column",
Operations: migrations.Operations{
Expand All @@ -529,7 +554,7 @@ func TestAlterColumnValidation(t *testing.T) {
},
},
},
wantStartErr: migrations.BackfillNotPossibleError{Table: "orders"},
wantStartErr: nil,
},
})
}

0 comments on commit 7d8460f

Please sign in to comment.