Skip to content

Commit

Permalink
Export OpAlterColumn.IsRenameOnly method (#357)
Browse files Browse the repository at this point in the history
Export `OpAlterColumn.IsRenameOnly` method and add a test for it.

Applications using `pgroll` as a module may need to test if an alter
column operation is only a column rename operation without having to
duplicate the logic of checking the fields of the `OpAlterColumn`
struct.
  • Loading branch information
andrew-farries authored May 22, 2024
1 parent a87fa36 commit fca5abd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pkg/migrations/op_alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (o *OpAlterColumn) Start(ctx context.Context, conn db.DB, stateSchema strin
ops := o.subOperations()

// Duplicate the column on the underlying table.
if !o.isRenameOnly() {
if !o.IsRenameOnly() {
d := duplicatorForOperations(ops, conn, table, column)
if err := d.Duplicate(ctx); err != nil {
return nil, fmt.Errorf("failed to duplicate column: %w", err)
Expand All @@ -36,7 +36,7 @@ func (o *OpAlterColumn) Start(ctx context.Context, conn db.DB, stateSchema strin

// Add a trigger to copy values from the old column to the new, rewriting values using the `up` SQL.
// Rename column operations do not require this trigger.
if !o.isRenameOnly() {
if !o.IsRenameOnly() {
err := createTrigger(ctx, conn, tr, triggerConfig{
Name: TriggerName(o.Table, o.Column),
Direction: TriggerDirectionUp,
Expand Down Expand Up @@ -89,7 +89,7 @@ func (o *OpAlterColumn) Start(ctx context.Context, conn db.DB, stateSchema strin
table.RenameColumn(o.Column, *o.Name)
}

if o.isRenameOnly() {
if o.IsRenameOnly() {
return nil, nil
}
return table, nil
Expand All @@ -105,7 +105,7 @@ func (o *OpAlterColumn) Complete(ctx context.Context, conn db.DB, tr SQLTransfor
}
}

if !o.isRenameOnly() {
if !o.IsRenameOnly() {
// Drop the old column
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s DROP COLUMN IF EXISTS %s",
pq.QuoteIdentifier(o.Table),
Expand Down Expand Up @@ -160,7 +160,7 @@ func (o *OpAlterColumn) Rollback(ctx context.Context, conn db.DB, tr SQLTransfor
}
}

if !o.isRenameOnly() {
if !o.IsRenameOnly() {
// Drop the new column
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s DROP COLUMN IF EXISTS %s",
pq.QuoteIdentifier(o.Table),
Expand Down Expand Up @@ -202,7 +202,7 @@ func (o *OpAlterColumn) Validate(ctx context.Context, s *schema.Schema) error {

// 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 !o.IsRenameOnly() {
if err := checkBackfill(table); err != nil {
return err
}
Expand All @@ -224,7 +224,7 @@ func (o *OpAlterColumn) Validate(ctx context.Context, s *schema.Schema) error {
}

// Rename-only operations are not allowed to have `up` or `down` SQL
if o.isRenameOnly() {
if o.IsRenameOnly() {
if o.Up != "" {
return NoUpSQLAllowedError{}
}
Expand Down Expand Up @@ -374,7 +374,7 @@ func (o *OpAlterColumn) upSQLForOperations(ops []Operation) string {
return ""
}

// isRenameOnly returns true if the operation is a rename column operation only.
func (o *OpAlterColumn) isRenameOnly() bool {
// IsRenameOnly returns true if the operation is a rename column operation only.
func (o *OpAlterColumn) IsRenameOnly() bool {
return len(o.subOperations()) == 0 && o.Name != nil
}
45 changes: 45 additions & 0 deletions pkg/migrations/op_alter_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,51 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) {
})
}

func TestIsRenameOnly(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
op migrations.OpAlterColumn
expected bool
}{
{
name: "rename-only operation",
op: migrations.OpAlterColumn{
Table: "events",
Column: "name",
Name: ptr("event_name"),
},
expected: true,
},
{
name: "rename operation with other sub-operations",
op: migrations.OpAlterColumn{
Table: "events",
Column: "name",
Name: ptr("event_name"),
Nullable: ptr(false),
},
expected: false,
},
{
name: "alter column with no rename",
op: migrations.OpAlterColumn{
Table: "events",
Column: "name",
Nullable: ptr(false),
},
expected: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, tc.op.IsRenameOnly())
})
}
}

func TestAlterColumnValidation(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit fca5abd

Please sign in to comment.