Skip to content

Commit 08c7e01

Browse files
authored
Pass all column attributes to new temporary column when adding a new one to the table (#788)
The following migration was failing due to several reasons: ```json { "operations": [ { "create_table": { "name": "users", "columns": [ { "name": "id", "type": "uuid", "pk": true } ] } }, { "add_column": { "table": "users", "column": { "name": "name", "type": "text", "nullable": true } } }, { "create_constraint": { "table": "users", "columns": [ "name" ], "type": "unique", "name": "my_custom_name", "up": { "name": "name" }, "down": { "name": "name" } } } ] } ``` This PR fixes the first problem with the migration. The first problem is when `pgroll` adds the new column with the temporary name to the table schema it lacks all column attributes. Thus, if a later operation tries to duplicate this duplicated column it fails because we do not know the type of the column, the default value, etc. The SQL statement generated has a syntax error because of the lack of information: ``` Failed to start migration: unable to execute start operation: failed to duplicate column: pq: syntax error at or near "," ```
1 parent 95d8794 commit 08c7e01

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

pkg/migrations/op_add_column.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,27 @@ func (o *OpAddColumn) Start(ctx context.Context, conn db.DB, latestSchema string
9999
tableToBackfill = table
100100
}
101101

102-
table.AddColumn(o.Column.Name, &schema.Column{
103-
Name: TemporaryName(o.Column.Name),
104-
})
102+
tmpColumn := toSchemaColumn(o.Column)
103+
tmpColumn.Name = TemporaryName(o.Column.Name)
104+
table.AddColumn(o.Column.Name, tmpColumn)
105105

106106
return tableToBackfill, nil
107107
}
108108

109+
func toSchemaColumn(c Column) *schema.Column {
110+
tmpColumn := &schema.Column{
111+
Name: c.Name,
112+
Type: c.Type,
113+
Default: c.Default,
114+
Nullable: c.Nullable,
115+
Unique: c.Unique,
116+
}
117+
if c.Comment != nil {
118+
tmpColumn.Comment = *c.Comment
119+
}
120+
return tmpColumn
121+
}
122+
109123
func (o *OpAddColumn) Complete(ctx context.Context, conn db.DB, s *schema.Schema) error {
110124
err := NewRenameColumnAction(conn, o.Table, TemporaryName(o.Column.Name), o.Column.Name).Execute(ctx)
111125
if err != nil {

0 commit comments

Comments
 (0)