Skip to content

Commit 9a3eaed

Browse files
authored
Error out if table/column is missing (#681)
Graceful error for the segfault scenario described in #641
1 parent cd19f8a commit 9a3eaed

17 files changed

+123
-0
lines changed

pkg/migrations/op_add_column.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var _ Operation = (*OpAddColumn)(nil)
1919

2020
func (o *OpAddColumn) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
2121
table := s.GetTable(o.Table)
22+
if table == nil {
23+
return nil, TableDoesNotExistError{Name: o.Table}
24+
}
2225

2326
if err := addColumn(ctx, conn, *o, table, tr); err != nil {
2427
return nil, fmt.Errorf("failed to start add column operation: %w", err)
@@ -120,6 +123,9 @@ func (o *OpAddColumn) Complete(ctx context.Context, conn db.DB, tr SQLTransforme
120123
// Add the column to the in-memory schema so that Complete steps in subsequent
121124
// operations can see the new column.
122125
table := s.GetTable(o.Table)
126+
if table == nil {
127+
return TableDoesNotExistError{Name: o.Table}
128+
}
123129
table.AddColumn(o.Column.Name, &schema.Column{
124130
Name: o.Column.Name,
125131
})
@@ -129,7 +135,13 @@ func (o *OpAddColumn) Complete(ctx context.Context, conn db.DB, tr SQLTransforme
129135

130136
func (o *OpAddColumn) Rollback(ctx context.Context, conn db.DB, tr SQLTransformer, s *schema.Schema) error {
131137
table := s.GetTable(o.Table)
138+
if table == nil {
139+
return TableDoesNotExistError{Name: o.Table}
140+
}
132141
column := table.GetColumn(o.Column.Name)
142+
if column == nil {
143+
return ColumnDoesNotExistError{Table: o.Table, Name: o.Column.Name}
144+
}
133145

134146
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s DROP COLUMN IF EXISTS %s",
135147
pq.QuoteIdentifier(table.Name),

pkg/migrations/op_alter_column.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ var _ Operation = (*OpAlterColumn)(nil)
1717

1818
func (o *OpAlterColumn) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
1919
table := s.GetTable(o.Table)
20+
if table == nil {
21+
return nil, TableDoesNotExistError{Name: o.Table}
22+
}
2023
column := table.GetColumn(o.Column)
24+
if column == nil {
25+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
26+
}
2127
ops := o.subOperations()
2228

2329
// Duplicate the column on the underlying table.
@@ -114,7 +120,13 @@ func (o *OpAlterColumn) Complete(ctx context.Context, conn db.DB, tr SQLTransfor
114120

115121
// Rename the new column to the old column name
116122
table := s.GetTable(o.Table)
123+
if table == nil {
124+
return TableDoesNotExistError{Name: o.Table}
125+
}
117126
column := table.GetColumn(o.Column)
127+
if column == nil {
128+
return ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
129+
}
118130
if err := RenameDuplicatedColumn(ctx, conn, table, column); err != nil {
119131
return err
120132
}
@@ -124,7 +136,13 @@ func (o *OpAlterColumn) Complete(ctx context.Context, conn db.DB, tr SQLTransfor
124136

125137
func (o *OpAlterColumn) Rollback(ctx context.Context, conn db.DB, tr SQLTransformer, s *schema.Schema) error {
126138
table := s.GetTable(o.Table)
139+
if table == nil {
140+
return TableDoesNotExistError{Name: o.Table}
141+
}
127142
column := table.GetColumn(o.Column)
143+
if column == nil {
144+
return ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
145+
}
128146

129147
// Perform any operation specific rollback steps
130148
ops := o.subOperations()

pkg/migrations/op_change_type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var _ Operation = (*OpChangeType)(nil)
2121

2222
func (o *OpChangeType) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
2323
table := s.GetTable(o.Table)
24+
if table == nil {
25+
return nil, TableDoesNotExistError{Name: o.Table}
26+
}
2427

2528
return table, nil
2629
}

pkg/migrations/op_create_constraint.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ var _ Operation = (*OpCreateConstraint)(nil)
1717

1818
func (o *OpCreateConstraint) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
1919
table := s.GetTable(o.Table)
20+
if table == nil {
21+
return nil, TableDoesNotExistError{Name: o.Table}
22+
}
23+
2024
columns := make([]*schema.Column, len(o.Columns))
2125
for i, colName := range o.Columns {
2226
columns[i] = table.GetColumn(colName)
27+
if columns[i] == nil {
28+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: colName}
29+
}
2330
}
2431

2532
// Duplicate each column using its final name after migration completion
@@ -137,8 +144,14 @@ func (o *OpCreateConstraint) Complete(ctx context.Context, conn db.DB, tr SQLTra
137144

138145
// rename new columns to old name
139146
table := s.GetTable(o.Table)
147+
if table == nil {
148+
return TableDoesNotExistError{Name: o.Table}
149+
}
140150
for _, col := range o.Columns {
141151
column := table.GetColumn(col)
152+
if column == nil {
153+
return ColumnDoesNotExistError{Table: o.Table, Name: col}
154+
}
142155
if err := RenameDuplicatedColumn(ctx, conn, table, column); err != nil {
143156
return err
144157
}
@@ -149,6 +162,9 @@ func (o *OpCreateConstraint) Complete(ctx context.Context, conn db.DB, tr SQLTra
149162

150163
func (o *OpCreateConstraint) Rollback(ctx context.Context, conn db.DB, tr SQLTransformer, s *schema.Schema) error {
151164
table := s.GetTable(o.Table)
165+
if table == nil {
166+
return TableDoesNotExistError{Name: o.Table}
167+
}
152168

153169
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s %s",
154170
pq.QuoteIdentifier(table.Name),

pkg/migrations/op_create_index.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ var _ Operation = (*OpCreateIndex)(nil)
1717

1818
func (o *OpCreateIndex) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
1919
table := s.GetTable(o.Table)
20+
if table == nil {
21+
return nil, TableDoesNotExistError{Name: o.Table}
22+
}
2023

2124
// create index concurrently
2225
stmtFmt := "CREATE INDEX CONCURRENTLY %s ON %s"

pkg/migrations/op_drop_column.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ func (o *OpDropColumn) Start(ctx context.Context, conn db.DB, latestSchema strin
3030
}
3131
}
3232

33+
table := s.GetTable(o.Table)
34+
if table == nil {
35+
return nil, TableDoesNotExistError{Name: o.Table}
36+
}
37+
column := table.GetColumn(o.Column)
38+
if column == nil {
39+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: o.Column}
40+
}
41+
3342
s.GetTable(o.Table).RemoveColumn(o.Column)
3443
return nil, nil
3544
}

pkg/migrations/op_drop_constraint.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ var _ Operation = (*OpDropConstraint)(nil)
1616

1717
func (o *OpDropConstraint) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
1818
table := s.GetTable(o.Table)
19+
if table == nil {
20+
return nil, TableDoesNotExistError{Name: o.Table}
21+
}
1922

2023
// By this point Validate() should have run which ensures the constraint exists and that we only have
2124
// one column associated with it.
2225
column := table.GetColumn(table.GetConstraintColumns(o.Name)[0])
26+
if column == nil {
27+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: table.GetConstraintColumns(o.Name)[0]}
28+
}
2329

2430
// Create a copy of the column on the underlying table.
2531
d := NewColumnDuplicator(conn, table, column).WithoutConstraint(o.Name)

pkg/migrations/op_drop_multicolumn_constraint.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ var _ Operation = (*OpDropMultiColumnConstraint)(nil)
1616

1717
func (o *OpDropMultiColumnConstraint) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
1818
table := s.GetTable(o.Table)
19+
if table == nil {
20+
return nil, TableDoesNotExistError{Name: o.Table}
21+
}
1922

2023
// Get all columns covered by the constraint to be dropped
2124
constraintColumns := table.GetConstraintColumns(o.Name)
2225
columns := make([]*schema.Column, len(constraintColumns))
2326
for i, c := range constraintColumns {
2427
columns[i] = table.GetColumn(c)
28+
if columns[i] == nil {
29+
return nil, ColumnDoesNotExistError{Table: o.Table, Name: c}
30+
}
2531
}
2632

2733
// Duplicate each of the columns covered by the constraint to be dropped

pkg/migrations/op_drop_not_null.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var _ Operation = (*OpDropNotNull)(nil)
2121

2222
func (o *OpDropNotNull) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
2323
table := s.GetTable(o.Table)
24+
if table == nil {
25+
return nil, TableDoesNotExistError{Name: o.Table}
26+
}
2427

2528
return table, nil
2629
}

pkg/migrations/op_drop_table.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ var _ Operation = (*OpDropTable)(nil)
1515

1616
func (o *OpDropTable) Start(ctx context.Context, conn db.DB, latestSchema string, tr SQLTransformer, s *schema.Schema) (*schema.Table, error) {
1717
table := s.GetTable(o.Name)
18+
if table == nil {
19+
return nil, TableDoesNotExistError{Name: o.Name}
20+
}
1821

1922
// Soft-delete the table in order that a create table operation in the same
2023
// migration can create a table with the same name

0 commit comments

Comments
 (0)