Skip to content

Commit df51a33

Browse files
authored
New DBAction: DropConstraintAction (#849)
Related to #742
1 parent 3160548 commit df51a33

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

pkg/migrations/dbactions.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,24 @@ func getSequenceNameForColumn(ctx context.Context, conn db.DB, tableName, column
535535

536536
return sequenceName
537537
}
538+
539+
type dropConstraintAction struct {
540+
conn db.DB
541+
table string
542+
constraint string
543+
}
544+
545+
func NewDropConstraintAction(conn db.DB, table, constraint string) *dropConstraintAction {
546+
return &dropConstraintAction{
547+
conn: conn,
548+
table: table,
549+
constraint: constraint,
550+
}
551+
}
552+
553+
func (a *dropConstraintAction) Execute(ctx context.Context) error {
554+
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s DROP CONSTRAINT IF EXISTS %s",
555+
pq.QuoteIdentifier(a.table),
556+
pq.QuoteIdentifier(a.constraint)))
557+
return err
558+
}

pkg/migrations/op_add_column.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,7 @@ func upgradeNotNullConstraintToNotNullAttribute(ctx context.Context, conn db.DB,
358358
return err
359359
}
360360

361-
_, err = conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s DROP CONSTRAINT IF EXISTS %s",
362-
pq.QuoteIdentifier(tableName),
363-
pq.QuoteIdentifier(NotNullConstraintName(columnName))))
361+
err = NewDropConstraintAction(conn, tableName, NotNullConstraintName(columnName)).Execute(ctx)
364362

365363
return err
366364
}

pkg/migrations/op_set_notnull.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ func (o *OpSetNotNull) Complete(ctx context.Context, l Logger, conn db.DB, s *sc
6262
}
6363

6464
// Drop the NOT NULL constraint
65-
_, err = conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s DROP CONSTRAINT IF EXISTS %s",
66-
pq.QuoteIdentifier(o.Table),
67-
pq.QuoteIdentifier(NotNullConstraintName(o.Column))))
65+
err = NewDropConstraintAction(conn, o.Table, NotNullConstraintName(o.Column)).Execute(ctx)
6866
if err != nil {
6967
return err
7068
}

pkg/migrations/rename.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table
8181
}
8282

8383
// Drop the constraint
84-
dropConstraintSQL := fmt.Sprintf(cDropConstraintSQL,
85-
pq.QuoteIdentifier(table.Name),
86-
pq.QuoteIdentifier(NotNullConstraintName(column.Name)),
87-
)
88-
89-
_, err = conn.ExecContext(ctx, dropConstraintSQL)
84+
err = NewDropConstraintAction(conn, table.Name, NotNullConstraintName(column.Name)).Execute(ctx)
9085
if err != nil {
9186
return fmt.Errorf("failed to drop not null constraint: %w", err)
9287
}

0 commit comments

Comments
 (0)