Skip to content

Commit 711b4d4

Browse files
authored
New DBAction to run raw SQL statements (#898)
Related to #742
1 parent b533a14 commit 711b4d4

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

pkg/migrations/dbactions.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,3 +673,20 @@ func (a *dropDefaultAction) Execute(ctx context.Context) error {
673673
pq.QuoteIdentifier(a.column)))
674674
return err
675675
}
676+
677+
type rawSQLAction struct {
678+
conn db.DB
679+
sql string
680+
}
681+
682+
func NewRawSQLAction(conn db.DB, sql string) *rawSQLAction {
683+
return &rawSQLAction{
684+
conn: conn,
685+
sql: sql,
686+
}
687+
}
688+
689+
func (a *rawSQLAction) Execute(ctx context.Context) error {
690+
_, err := a.conn.ExecContext(ctx, a.sql)
691+
return err
692+
}

pkg/migrations/op_raw_sql.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ func (o *OpRawSQL) Start(ctx context.Context, l Logger, conn db.DB, latestSchema
2121
return nil, nil
2222
}
2323

24-
_, err := conn.ExecContext(ctx, o.Up)
25-
return nil, err
24+
return nil, NewRawSQLAction(conn, o.Up).Execute(ctx)
2625
}
2726

2827
func (o *OpRawSQL) Complete(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
@@ -32,8 +31,7 @@ func (o *OpRawSQL) Complete(ctx context.Context, l Logger, conn db.DB, s *schema
3231
return nil
3332
}
3433

35-
_, err := conn.ExecContext(ctx, o.Up)
36-
return err
34+
return NewRawSQLAction(conn, o.Up).Execute(ctx)
3735
}
3836

3937
func (o *OpRawSQL) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema.Schema) error {
@@ -43,8 +41,7 @@ func (o *OpRawSQL) Rollback(ctx context.Context, l Logger, conn db.DB, s *schema
4341
return nil
4442
}
4543

46-
_, err := conn.ExecContext(ctx, o.Down)
47-
return err
44+
return NewRawSQLAction(conn, o.Down).Execute(ctx)
4845
}
4946

5047
func (o *OpRawSQL) Validate(ctx context.Context, s *schema.Schema) error {

0 commit comments

Comments
 (0)