Skip to content

Commit fcda88a

Browse files
authored
Use existing dbactions in op_alter_column (#879)
Related #742
1 parent 44e4e00 commit fcda88a

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

pkg/migrations/op_add_column.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,32 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSch
5555
// the column as no DEFAULT or because the default value cannot be set using
5656
// the fast path optimization), add a NOT NULL constraint to the column which
5757
// will be validated on migration completion.
58+
skipInherit := false
59+
skipValidate := true
5860
if !o.Column.IsNullable() && (o.Column.Default == nil || !fastPathDefault) {
59-
if err := addNotNullConstraint(ctx, conn, table.Name, o.Column.Name, TemporaryName(o.Column.Name)); err != nil {
61+
if err := NewCreateCheckConstraintAction(
62+
conn,
63+
table.Name,
64+
NotNullConstraintName(o.Column.Name),
65+
fmt.Sprintf("%s IS NOT NULL", o.Column.Name),
66+
[]string{o.Column.Name},
67+
skipInherit,
68+
skipValidate,
69+
).Execute(ctx); err != nil {
6070
return nil, fmt.Errorf("failed to add not null constraint: %w", err)
6171
}
6272
}
6373

6474
if o.Column.Check != nil {
65-
if err := o.addCheckConstraint(ctx, table.Name, conn); err != nil {
75+
if err := NewCreateCheckConstraintAction(
76+
conn,
77+
table.Name,
78+
o.Column.Check.Name,
79+
o.Column.Check.Constraint,
80+
[]string{o.Column.Name},
81+
skipInherit,
82+
skipValidate,
83+
).Execute(ctx); err != nil {
6684
return nil, fmt.Errorf("failed to add check constraint: %w", err)
6785
}
6886
}
@@ -360,24 +378,6 @@ func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB,
360378
return err
361379
}
362380

363-
func addNotNullConstraint(ctx context.Context, conn db.DB, table, column, physicalColumn string) error {
364-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s IS NOT NULL) NOT VALID",
365-
pq.QuoteIdentifier(table),
366-
pq.QuoteIdentifier(NotNullConstraintName(column)),
367-
pq.QuoteIdentifier(physicalColumn),
368-
))
369-
return err
370-
}
371-
372-
func (o *OpAddColumn) addCheckConstraint(ctx context.Context, tableName string, conn db.DB) error {
373-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s) NOT VALID",
374-
pq.QuoteIdentifier(tableName),
375-
pq.QuoteIdentifier(o.Column.Check.Name),
376-
rewriteCheckExpression(o.Column.Check.Constraint, o.Column.Name),
377-
))
378-
return err
379-
}
380-
381381
// UniqueIndexName returns the name of the unique index for the given column
382382
func UniqueIndexName(columnName string) string {
383383
return "_pgroll_uniq_" + columnName

pkg/migrations/op_set_notnull.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ func (o *OpSetNotNull) Start(ctx context.Context, l Logger, conn db.DB, latestSc
3434
}
3535

3636
// Add an unchecked NOT NULL constraint to the new column.
37-
if err := addNotNullConstraint(ctx, conn, table.Name, o.Column, column.Name); err != nil {
37+
skipInherit := false
38+
skipValidate := true // We will validate the constraint later in the Complete step.
39+
if err := NewCreateCheckConstraintAction(
40+
conn,
41+
table.Name,
42+
NotNullConstraintName(o.Column),
43+
fmt.Sprintf("%s IS NOT NULL", o.Column),
44+
[]string{o.Column},
45+
skipInherit,
46+
skipValidate,
47+
).Execute(ctx); err != nil {
3848
return nil, fmt.Errorf("failed to add not null constraint: %w", err)
3949
}
4050

0 commit comments

Comments
 (0)