Skip to content

Commit cabb623

Browse files
authored
New DBAction: ValidateConstraintAction (#838)
This PR adds a new `DBAction` to validate constraints on tables. The new action is used in: * `op_add_column` * `op_set_fk` * `op_set_not_null` * `op_set_check` * `rename`
1 parent 7241a55 commit cabb623

File tree

6 files changed

+28
-22
lines changed

6 files changed

+28
-22
lines changed

pkg/migrations/dbactions.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,25 @@ func (a *DropTableAction) Execute(ctx context.Context) error {
372372
pq.QuoteIdentifier(a.table)))
373373
return err
374374
}
375+
376+
// validateConstraintAction is a DBAction that validates a constraint in a table.
377+
type validateConstraintAction struct {
378+
conn db.DB
379+
table string
380+
constraint string
381+
}
382+
383+
func NewValidateConstraintAction(conn db.DB, table, constraint string) *validateConstraintAction {
384+
return &validateConstraintAction{
385+
conn: conn,
386+
table: table,
387+
constraint: constraint,
388+
}
389+
}
390+
391+
func (a *validateConstraintAction) Execute(ctx context.Context) error {
392+
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s VALIDATE CONSTRAINT %s",
393+
pq.QuoteIdentifier(a.table),
394+
pq.QuoteIdentifier(a.constraint)))
395+
return err
396+
}

pkg/migrations/op_add_column.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ func (o *OpAddColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *sch
152152
}
153153

154154
if o.Column.Check != nil {
155-
_, err = conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s VALIDATE CONSTRAINT %s",
156-
pq.QuoteIdentifier(o.Table),
157-
pq.QuoteIdentifier(o.Column.Check.Name)))
155+
err = NewValidateConstraintAction(conn, o.Table, o.Column.Check.Name).Execute(ctx)
158156
if err != nil {
159157
return err
160158
}
@@ -348,9 +346,7 @@ func addColumn(ctx context.Context, conn db.DB, o OpAddColumn, t *schema.Table,
348346
// constraint to a NOT NULL column attribute. The constraint is removed after
349347
// the column attribute is added.
350348
func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB, tableName, columnName string) error {
351-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s VALIDATE CONSTRAINT %s",
352-
pq.QuoteIdentifier(tableName),
353-
pq.QuoteIdentifier(NotNullConstraintName(columnName))))
349+
err := NewValidateConstraintAction(conn, tableName, NotNullConstraintName(columnName)).Execute(ctx)
354350
if err != nil {
355351
return err
356352
}

pkg/migrations/op_set_check.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ func (o *OpSetCheckConstraint) Complete(ctx context.Context, l Logger, conn db.D
4343
l.LogOperationComplete(o)
4444

4545
// Validate the check constraint
46-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s VALIDATE CONSTRAINT %s",
47-
pq.QuoteIdentifier(o.Table),
48-
pq.QuoteIdentifier(o.Check.Name)))
46+
err := NewValidateConstraintAction(conn, o.Table, o.Check.Name).Execute(ctx)
4947
if err != nil {
5048
return err
5149
}

pkg/migrations/op_set_fk.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ func (o *OpSetForeignKey) Complete(ctx context.Context, l Logger, conn db.DB, s
3939
l.LogOperationComplete(o)
4040

4141
// Validate the foreign key constraint
42-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s VALIDATE CONSTRAINT %s",
43-
pq.QuoteIdentifier(o.Table),
44-
pq.QuoteIdentifier(o.References.Name)))
42+
err := NewValidateConstraintAction(conn, o.Table, o.References.Name).Execute(ctx)
4543
if err != nil {
4644
return err
4745
}

pkg/migrations/op_set_notnull.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ func (o *OpSetNotNull) Complete(ctx context.Context, l Logger, conn db.DB, s *sc
4848
// The constraint must be valid because:
4949
// * Existing NULL values in the old column were rewritten using the `up` SQL during backfill.
5050
// * New NULL values written to the old column during the migration period were also rewritten using `up` SQL.
51-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s VALIDATE CONSTRAINT %s",
52-
pq.QuoteIdentifier(o.Table),
53-
pq.QuoteIdentifier(NotNullConstraintName(o.Column))))
51+
err := NewValidateConstraintAction(conn, o.Table, NotNullConstraintName(o.Column)).Execute(ctx)
5452
if err != nil {
5553
return err
5654
}

pkg/migrations/rename.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
// * Validates and renames any temporary `CHECK` constraints on the duplicated column.
2020
func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table, column *schema.Column) error {
2121
const (
22-
cValidateConstraintSQL = `ALTER TABLE IF EXISTS %s VALIDATE CONSTRAINT %s`
2322
cSetNotNullSQL = `ALTER TABLE IF EXISTS %s ALTER COLUMN %s SET NOT NULL`
2423
cDropConstraintSQL = `ALTER TABLE IF EXISTS %s DROP CONSTRAINT IF EXISTS %s`
2524
cCreateUniqueConstraintSQL = `ALTER TABLE IF EXISTS %s ADD CONSTRAINT %s UNIQUE USING INDEX %s`
@@ -55,12 +54,7 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table
5554
}
5655

5756
if slices.Contains(cc.Columns, TemporaryName(column.Name)) {
58-
validateConstraintSQL := fmt.Sprintf(cValidateConstraintSQL,
59-
pq.QuoteIdentifier(table.Name),
60-
pq.QuoteIdentifier(cc.Name),
61-
)
62-
63-
_, err = conn.ExecContext(ctx, validateConstraintSQL)
57+
err := NewValidateConstraintAction(conn, table.Name, cc.Name).Execute(ctx)
6458
if err != nil {
6559
return fmt.Errorf("failed to validate check constraint %q: %w", cc.Name, err)
6660
}

0 commit comments

Comments
 (0)