Skip to content

Commit d784d1b

Browse files
committed
Move TriggerConfig to backfill package
1 parent 899645a commit d784d1b

File tree

8 files changed

+87
-81
lines changed

8 files changed

+87
-81
lines changed

pkg/backfill/trigger.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package backfill
4+
5+
import (
6+
"github.com/xataio/pgroll/pkg/schema"
7+
)
8+
9+
type TriggerDirection string
10+
11+
const (
12+
TriggerDirectionUp TriggerDirection = "up"
13+
TriggerDirectionDown TriggerDirection = "down"
14+
)
15+
16+
type TriggerConfig struct {
17+
Name string
18+
Direction TriggerDirection
19+
Columns map[string]*schema.Column
20+
SchemaName string
21+
TableName string
22+
PhysicalColumn string
23+
LatestSchema string
24+
SQL string
25+
NeedsBackfillColumn string
26+
}
27+
28+
// TriggerFunctionName returns the name of the trigger function
29+
// for a given table and column.
30+
func TriggerFunctionName(tableName, columnName string) string {
31+
return "_pgroll_trigger_" + tableName + "_" + columnName
32+
}
33+
34+
// TriggerName returns the name of the trigger for a given table and column.
35+
func TriggerName(tableName, columnName string) string {
36+
return TriggerFunctionName(tableName, columnName)
37+
}

pkg/migrations/op_add_column.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSch
103103
var tableToBackfill *schema.Table
104104
if o.Up != "" {
105105
err := NewCreateTriggerAction(conn,
106-
triggerConfig{
107-
Name: TriggerName(o.Table, o.Column.Name),
108-
Direction: TriggerDirectionUp,
106+
backfill.TriggerConfig{
107+
Name: backfill.TriggerName(o.Table, o.Column.Name),
108+
Direction: backfill.TriggerDirectionUp,
109109
Columns: table.Columns,
110110
SchemaName: s.Name,
111111
LatestSchema: latestSchema,
@@ -149,7 +149,7 @@ func (o *OpAddColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *sch
149149
return err
150150
}
151151

152-
err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
152+
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
153153
if err != nil {
154154
return err
155155
}
@@ -224,7 +224,7 @@ func (o *OpAddColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *sch
224224
return err
225225
}
226226

227-
err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
227+
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
228228
if err != nil {
229229
return err
230230
}

pkg/migrations/op_alter_column.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, latestS
4040

4141
// Add a trigger to copy values from the old column to the new, rewriting values using the `up` SQL.
4242
err := NewCreateTriggerAction(conn,
43-
triggerConfig{
44-
Name: TriggerName(o.Table, o.Column),
45-
Direction: TriggerDirectionUp,
43+
backfill.TriggerConfig{
44+
Name: backfill.TriggerName(o.Table, o.Column),
45+
Direction: backfill.TriggerDirectionUp,
4646
Columns: table.Columns,
4747
SchemaName: s.Name,
4848
LatestSchema: latestSchema,
@@ -66,9 +66,9 @@ func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, latestS
6666

6767
// Add a trigger to copy values from the new column to the old.
6868
err = NewCreateTriggerAction(conn,
69-
triggerConfig{
70-
Name: TriggerName(o.Table, TemporaryName(o.Column)),
71-
Direction: TriggerDirectionDown,
69+
backfill.TriggerConfig{
70+
Name: backfill.TriggerName(o.Table, TemporaryName(o.Column)),
71+
Direction: backfill.TriggerDirectionDown,
7272
Columns: table.Columns,
7373
LatestSchema: latestSchema,
7474
SchemaName: s.Name,
@@ -114,7 +114,7 @@ func (o *OpAlterColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *s
114114
}
115115

116116
// Remove the up and down function and trigger
117-
err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column), TriggerFunctionName(o.Table, TemporaryName(o.Column))).Execute(ctx)
117+
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column), backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column))).Execute(ctx)
118118
if err != nil {
119119
return err
120120
}
@@ -170,8 +170,8 @@ func (o *OpAlterColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *s
170170
// Remove the up and down functions and triggers
171171
if err := NewDropFunctionAction(
172172
conn,
173-
TriggerFunctionName(o.Table, o.Column),
174-
TriggerFunctionName(o.Table, TemporaryName(o.Column)),
173+
backfill.TriggerFunctionName(o.Table, o.Column),
174+
backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column)),
175175
).Execute(ctx); err != nil {
176176
return err
177177
}

pkg/migrations/op_create_constraint.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, la
4545
for _, colName := range o.Columns {
4646
upSQL := o.Up[colName]
4747
err := NewCreateTriggerAction(conn,
48-
triggerConfig{
49-
Name: TriggerName(o.Table, colName),
50-
Direction: TriggerDirectionUp,
48+
backfill.TriggerConfig{
49+
Name: backfill.TriggerName(o.Table, colName),
50+
Direction: backfill.TriggerDirectionUp,
5151
Columns: table.Columns,
5252
SchemaName: s.Name,
5353
LatestSchema: latestSchema,
@@ -71,9 +71,9 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, la
7171

7272
downSQL := o.Down[colName]
7373
err = NewCreateTriggerAction(conn,
74-
triggerConfig{
75-
Name: TriggerName(o.Table, TemporaryName(colName)),
76-
Direction: TriggerDirectionDown,
74+
backfill.TriggerConfig{
75+
Name: backfill.TriggerName(o.Table, TemporaryName(colName)),
76+
Direction: backfill.TriggerDirectionDown,
7777
Columns: table.Columns,
7878
LatestSchema: latestSchema,
7979
SchemaName: s.Name,
@@ -205,8 +205,8 @@ func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB,
205205
func (o *OpCreateConstraint) removeTriggers(ctx context.Context, conn db.DB) error {
206206
dropFuncs := make([]string, 0, len(o.Columns)*2)
207207
for _, column := range o.Columns {
208-
dropFuncs = append(dropFuncs, TriggerFunctionName(o.Table, column))
209-
dropFuncs = append(dropFuncs, TriggerFunctionName(o.Table, TemporaryName(column)))
208+
dropFuncs = append(dropFuncs, backfill.TriggerFunctionName(o.Table, column))
209+
dropFuncs = append(dropFuncs, backfill.TriggerFunctionName(o.Table, TemporaryName(column)))
210210
}
211211
return NewDropFunctionAction(conn, dropFuncs...).Execute(ctx)
212212
}

pkg/migrations/op_drop_column.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ func (o *OpDropColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSc
2020

2121
if o.Down != "" {
2222
err := NewCreateTriggerAction(conn,
23-
triggerConfig{
24-
Name: TriggerName(o.Table, o.Column),
25-
Direction: TriggerDirectionDown,
23+
backfill.TriggerConfig{
24+
Name: backfill.TriggerName(o.Table, o.Column),
25+
Direction: backfill.TriggerDirectionDown,
2626
Columns: s.GetTable(o.Table).Columns,
2727
SchemaName: s.Name,
2828
LatestSchema: latestSchema,
@@ -58,7 +58,7 @@ func (o *OpDropColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *sc
5858
return err
5959
}
6060

61-
err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
61+
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
6262
if err != nil {
6363
return err
6464
}
@@ -77,7 +77,7 @@ func (o *OpDropColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *sc
7777

7878
table := s.GetTable(o.Table)
7979

80-
err := NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
80+
err := NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
8181
if err != nil {
8282
return err
8383
}

pkg/migrations/op_drop_constraint.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ func (o *OpDropConstraint) Start(ctx context.Context, l Logger, conn db.DB, late
3838

3939
// Add a trigger to copy values from the old column to the new, rewriting values using the `up` SQL.
4040
err := NewCreateTriggerAction(conn,
41-
triggerConfig{
42-
Name: TriggerName(o.Table, column.Name),
43-
Direction: TriggerDirectionUp,
41+
backfill.TriggerConfig{
42+
Name: backfill.TriggerName(o.Table, column.Name),
43+
Direction: backfill.TriggerDirectionUp,
4444
Columns: table.Columns,
4545
SchemaName: s.Name,
4646
LatestSchema: latestSchema,
@@ -62,9 +62,9 @@ func (o *OpDropConstraint) Start(ctx context.Context, l Logger, conn db.DB, late
6262

6363
// Add a trigger to copy values from the new column to the old, rewriting values using the `down` SQL.
6464
err = NewCreateTriggerAction(conn,
65-
triggerConfig{
66-
Name: TriggerName(o.Table, TemporaryName(column.Name)),
67-
Direction: TriggerDirectionDown,
65+
backfill.TriggerConfig{
66+
Name: backfill.TriggerName(o.Table, TemporaryName(column.Name)),
67+
Direction: backfill.TriggerDirectionDown,
6868
Columns: table.Columns,
6969
SchemaName: s.Name,
7070
LatestSchema: latestSchema,
@@ -87,7 +87,7 @@ func (o *OpDropConstraint) Complete(ctx context.Context, l Logger, conn db.DB, s
8787
column := table.GetColumn(table.GetConstraintColumns(o.Name)[0])
8888

8989
// Remove the up and down function and trigger
90-
err := NewDropFunctionAction(conn, TriggerFunctionName(o.Table, column.Name), TriggerFunctionName(o.Table, TemporaryName(column.Name))).Execute(ctx)
90+
err := NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, column.Name), backfill.TriggerFunctionName(o.Table, TemporaryName(column.Name))).Execute(ctx)
9191
if err != nil {
9292
return err
9393
}
@@ -134,8 +134,8 @@ func (o *OpDropConstraint) Rollback(ctx context.Context, l Logger, conn db.DB, s
134134
// Remove the up and down functions and triggers
135135
if err := NewDropFunctionAction(
136136
conn,
137-
TriggerFunctionName(o.Table, columnName),
138-
TriggerFunctionName(o.Table, TemporaryName(columnName)),
137+
backfill.TriggerFunctionName(o.Table, columnName),
138+
backfill.TriggerFunctionName(o.Table, TemporaryName(columnName)),
139139
).Execute(ctx); err != nil {
140140
return err
141141
}

pkg/migrations/op_drop_multicolumn_constraint.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-License-Identifier: Apache-2.0
1+
// SPDX-icense-Identifier: Apache-2.0
22

33
package migrations
44

@@ -52,9 +52,9 @@ func (o *OpDropMultiColumnConstraint) Start(ctx context.Context, l Logger, conn
5252
for _, columnName := range table.GetConstraintColumns(o.Name) {
5353
// Add a trigger to copy values from the old column to the new, rewriting values using the `up` SQL.
5454
err := NewCreateTriggerAction(conn,
55-
triggerConfig{
56-
Name: TriggerName(o.Table, columnName),
57-
Direction: TriggerDirectionUp,
55+
backfill.TriggerConfig{
56+
Name: backfill.TriggerName(o.Table, columnName),
57+
Direction: backfill.TriggerDirectionUp,
5858
Columns: table.Columns,
5959
SchemaName: s.Name,
6060
LatestSchema: latestSchema,
@@ -78,9 +78,9 @@ func (o *OpDropMultiColumnConstraint) Start(ctx context.Context, l Logger, conn
7878

7979
// Add a trigger to copy values from the new column to the old, rewriting values using the `down` SQL.
8080
err = NewCreateTriggerAction(conn,
81-
triggerConfig{
82-
Name: TriggerName(o.Table, TemporaryName(columnName)),
83-
Direction: TriggerDirectionDown,
81+
backfill.TriggerConfig{
82+
Name: backfill.TriggerName(o.Table, TemporaryName(columnName)),
83+
Direction: backfill.TriggerDirectionDown,
8484
Columns: table.Columns,
8585
SchemaName: s.Name,
8686
LatestSchema: latestSchema,
@@ -104,7 +104,7 @@ func (o *OpDropMultiColumnConstraint) Complete(ctx context.Context, l Logger, co
104104

105105
for _, columnName := range table.GetConstraintColumns(o.Name) {
106106
// Remove the up and down function and trigger
107-
err := NewDropFunctionAction(conn, TriggerFunctionName(o.Table, columnName), TriggerFunctionName(o.Table, TemporaryName(columnName))).Execute(ctx)
107+
err := NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, columnName), backfill.TriggerFunctionName(o.Table, TemporaryName(columnName))).Execute(ctx)
108108
if err != nil {
109109
return err
110110
}
@@ -148,7 +148,7 @@ func (o *OpDropMultiColumnConstraint) Rollback(ctx context.Context, l Logger, co
148148
}
149149

150150
// Remove the up and down function and trigger
151-
err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, columnName), TriggerFunctionName(o.Table, TemporaryName(columnName))).Execute(ctx)
151+
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, columnName), backfill.TriggerFunctionName(o.Table, TemporaryName(columnName))).Execute(ctx)
152152
if err != nil {
153153
return err
154154
}

pkg/migrations/trigger.go

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,14 @@ import (
1414
"github.com/xataio/pgroll/pkg/backfill"
1515
"github.com/xataio/pgroll/pkg/db"
1616
"github.com/xataio/pgroll/pkg/migrations/templates"
17-
"github.com/xataio/pgroll/pkg/schema"
1817
)
1918

20-
type TriggerDirection string
21-
22-
const (
23-
TriggerDirectionUp TriggerDirection = "up"
24-
TriggerDirectionDown TriggerDirection = "down"
25-
)
26-
27-
type triggerConfig struct {
28-
Name string
29-
Direction TriggerDirection
30-
Columns map[string]*schema.Column
31-
SchemaName string
32-
TableName string
33-
PhysicalColumn string
34-
LatestSchema string
35-
SQL string
36-
NeedsBackfillColumn string
37-
}
38-
3919
type createTriggerAction struct {
4020
conn db.DB
41-
cfg triggerConfig
21+
cfg backfill.TriggerConfig
4222
}
4323

44-
func NewCreateTriggerAction(conn db.DB, cfg triggerConfig) DBAction {
24+
func NewCreateTriggerAction(conn db.DB, cfg backfill.TriggerConfig) DBAction {
4525
return &createTriggerAction{
4626
conn: conn,
4727
cfg: cfg,
@@ -85,26 +65,15 @@ func (a *createTriggerAction) Execute(ctx context.Context) error {
8565
})
8666
}
8767

88-
func buildFunction(cfg triggerConfig) (string, error) {
68+
func buildFunction(cfg backfill.TriggerConfig) (string, error) {
8969
return executeTemplate("function", templates.Function, cfg)
9070
}
9171

92-
func buildTrigger(cfg triggerConfig) (string, error) {
72+
func buildTrigger(cfg backfill.TriggerConfig) (string, error) {
9373
return executeTemplate("trigger", templates.Trigger, cfg)
9474
}
9575

96-
// TriggerFunctionName returns the name of the trigger function
97-
// for a given table and column.
98-
func TriggerFunctionName(tableName, columnName string) string {
99-
return "_pgroll_trigger_" + tableName + "_" + columnName
100-
}
101-
102-
// TriggerName returns the name of the trigger for a given table and column.
103-
func TriggerName(tableName, columnName string) string {
104-
return TriggerFunctionName(tableName, columnName)
105-
}
106-
107-
func executeTemplate(name, content string, cfg triggerConfig) (string, error) {
76+
func executeTemplate(name, content string, cfg backfill.TriggerConfig) (string, error) {
10877
tmpl := template.Must(template.
10978
New(name).
11079
Funcs(template.FuncMap{

0 commit comments

Comments
 (0)