Skip to content

Commit 44b15a5

Browse files
committed
Use existing dbactions in op_alter_column
1 parent 2150959 commit 44b15a5

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
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", TemporaryName(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+
rewriteCheckExpression(o.Column.Check.Constraint, o.Column.Name),
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
}
@@ -363,24 +381,6 @@ func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB,
363381
return err
364382
}
365383

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

0 commit comments

Comments
 (0)