From 4d3faebffdadcca89a51dca97a573f8164a020ea Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Thu, 16 May 2024 11:57:53 +0100 Subject: [PATCH] Make `down` SQL in rename column operations use the new name of the column (#354) Ensure that 'alter column' operations that rename a column and also specify `down` SQL (such as those that alter some other column attribute at the time of the rename) must use the new name of the column in the `down` SQL. Without this change, the `down` SQL would require the use of the old column name. Fixes #350 --- examples/35_alter_column_multiple.json | 2 +- pkg/migrations/op_alter_column.go | 13 ++++++++++++- pkg/migrations/op_alter_column_test.go | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/examples/35_alter_column_multiple.json b/examples/35_alter_column_multiple.json index 8294dce..2fae4e1 100644 --- a/examples/35_alter_column_multiple.json +++ b/examples/35_alter_column_multiple.json @@ -18,7 +18,7 @@ "constraint": "length(name) > 3" }, "up": "(SELECT CASE WHEN name IS NULL OR LENGTH(name) <= 3 THEN 'placeholder' ELSE name END)", - "down": "name" + "down": "event_name" } } ] diff --git a/pkg/migrations/op_alter_column.go b/pkg/migrations/op_alter_column.go index 95ae8c3..77b22e8 100644 --- a/pkg/migrations/op_alter_column.go +++ b/pkg/migrations/op_alter_column.go @@ -5,6 +5,7 @@ package migrations import ( "context" "fmt" + "maps" "github.com/lib/pq" "github.com/xataio/pgroll/pkg/db" @@ -57,11 +58,21 @@ func (o *OpAlterColumn) Start(ctx context.Context, conn db.DB, stateSchema strin Name: TemporaryName(o.Column), }) + // If the column has been renamed, temporarily update the column name in + // the internal schema representation to ensure that the variable name in + // the down trigger corresponds to the new name of column. + cols := table.Columns + if o.Name != nil { + cols = maps.Clone(table.Columns) + cols[*o.Name] = cols[o.Column] + delete(cols, o.Column) + } + // Add a trigger to copy values from the new column to the old. err = createTrigger(ctx, conn, tr, triggerConfig{ Name: TriggerName(o.Table, TemporaryName(o.Column)), Direction: TriggerDirectionDown, - Columns: table.Columns, + Columns: cols, SchemaName: s.Name, TableName: o.Table, PhysicalColumn: o.Column, diff --git a/pkg/migrations/op_alter_column_test.go b/pkg/migrations/op_alter_column_test.go index 6d28634..52a86de 100644 --- a/pkg/migrations/op_alter_column_test.go +++ b/pkg/migrations/op_alter_column_test.go @@ -46,7 +46,7 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) { Table: "events", Column: "name", Up: "(SELECT CASE WHEN name IS NULL OR LENGTH(name) <= 3 THEN 'placeholder' ELSE name END)", - Down: "name", + Down: "event_name", Name: ptr("event_name"), Type: ptr("text"), Comment: nullable.NewNullableWithValue("the name of the event"), @@ -319,7 +319,7 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) { Table: "events", Column: "name", Up: "name || '-' || random()*999::int", - Down: "name", + Down: "event_name", Name: ptr("event_name"), Unique: &migrations.UniqueConstraint{ Name: "events_event_name_unique",