Skip to content

Commit b533a14

Browse files
authored
Add new DBActions for setting and dropping default values (#892)
Related to #742
1 parent fe34c2c commit b533a14

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

pkg/migrations/dbactions.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,48 @@ func (a *setNotNullAction) Execute(ctx context.Context) error {
628628
pq.QuoteIdentifier(a.column)))
629629
return err
630630
}
631+
632+
type setDefaultAction struct {
633+
conn db.DB
634+
table string
635+
column string
636+
defaultValue string
637+
}
638+
639+
func NewSetDefaultValueAction(conn db.DB, table, column, defaultValue string) *setDefaultAction {
640+
return &setDefaultAction{
641+
conn: conn,
642+
table: table,
643+
column: column,
644+
defaultValue: defaultValue,
645+
}
646+
}
647+
648+
func (a *setDefaultAction) Execute(ctx context.Context) error {
649+
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s ALTER COLUMN %s SET DEFAULT %s",
650+
pq.QuoteIdentifier(a.table),
651+
pq.QuoteIdentifier(a.column),
652+
a.defaultValue))
653+
return err
654+
}
655+
656+
type dropDefaultAction struct {
657+
conn db.DB
658+
table string
659+
column string
660+
}
661+
662+
func NewDropDefaultValueAction(conn db.DB, table, column string) *dropDefaultAction {
663+
return &dropDefaultAction{
664+
conn: conn,
665+
table: table,
666+
column: column,
667+
}
668+
}
669+
670+
func (a *dropDefaultAction) Execute(ctx context.Context) error {
671+
_, err := a.conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s ALTER COLUMN %s DROP DEFAULT",
672+
pq.QuoteIdentifier(a.table),
673+
pq.QuoteIdentifier(a.column)))
674+
return err
675+
}

pkg/migrations/op_add_column.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ func (o *OpAddColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *sch
191191
// optimization, set it here.
192192
column := s.GetTable(o.Table).GetColumn(TemporaryName(o.Column.Name))
193193
if o.Column.HasDefault() && column.Default == nil {
194-
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE IF EXISTS %s ALTER COLUMN %s SET DEFAULT %s",
195-
pq.QuoteIdentifier(o.Table),
196-
pq.QuoteIdentifier(o.Column.Name),
197-
*o.Column.Default,
198-
))
194+
err := NewSetDefaultValueAction(conn, o.Table, o.Column.Name, *o.Column.Default).Execute(ctx)
199195
if err != nil {
200196
return err
201197
}

pkg/migrations/op_set_default.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ package migrations
44

55
import (
66
"context"
7-
"fmt"
8-
9-
"github.com/lib/pq"
107

118
"github.com/xataio/pgroll/pkg/db"
129
"github.com/xataio/pgroll/pkg/schema"
@@ -36,17 +33,10 @@ func (o *OpSetDefault) Start(ctx context.Context, l Logger, conn db.DB, latestSc
3633

3734
var err error
3835
if o.Default == nil {
39-
_, err = conn.ExecContext(ctx, fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT`,
40-
pq.QuoteIdentifier(table.Name),
41-
pq.QuoteIdentifier(column.Name)))
42-
36+
err = NewDropDefaultValueAction(conn, table.Name, column.Name).Execute(ctx)
4337
column.Default = nil
4438
} else {
45-
_, err = conn.ExecContext(ctx, fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s`,
46-
pq.QuoteIdentifier(table.Name),
47-
pq.QuoteIdentifier(column.Name),
48-
*o.Default))
49-
39+
err = NewSetDefaultValueAction(conn, table.Name, column.Name, *o.Default).Execute(ctx)
5040
column.Default = o.Default
5141
}
5242
if err != nil {

0 commit comments

Comments
 (0)