Skip to content

Commit 41e72a7

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

14 files changed

+135
-125
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_add_column_test.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111

1212
"github.com/xataio/pgroll/internal/testutils"
13+
"github.com/xataio/pgroll/pkg/backfill"
1314
"github.com/xataio/pgroll/pkg/migrations"
1415
)
1516

@@ -858,11 +859,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
858859
},
859860
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
860861
// The trigger function has been dropped.
861-
triggerFnName := migrations.TriggerFunctionName("products", "description")
862+
triggerFnName := backfill.TriggerFunctionName("products", "description")
862863
FunctionMustNotExist(t, db, schema, triggerFnName)
863864

864865
// The trigger has been dropped.
865-
triggerName := migrations.TriggerName("products", "description")
866+
triggerName := backfill.TriggerName("products", "description")
866867
TriggerMustNotExist(t, db, schema, "products", triggerName)
867868
},
868869
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
@@ -874,11 +875,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
874875
}, res)
875876

876877
// The trigger function has been dropped.
877-
triggerFnName := migrations.TriggerFunctionName("products", "description")
878+
triggerFnName := backfill.TriggerFunctionName("products", "description")
878879
FunctionMustNotExist(t, db, schema, triggerFnName)
879880

880881
// The trigger has been dropped.
881-
triggerName := migrations.TriggerName("products", "description")
882+
triggerName := backfill.TriggerName("products", "description")
882883
TriggerMustNotExist(t, db, schema, "products", triggerName)
883884
},
884885
},
@@ -940,11 +941,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
940941
},
941942
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
942943
// The trigger function has been dropped.
943-
triggerFnName := migrations.TriggerFunctionName("products", "description")
944+
triggerFnName := backfill.TriggerFunctionName("products", "description")
944945
FunctionMustNotExist(t, db, schema, triggerFnName)
945946

946947
// The trigger has been dropped.
947-
triggerName := migrations.TriggerName("products", "description")
948+
triggerName := backfill.TriggerName("products", "description")
948949
TriggerMustNotExist(t, db, schema, "products", triggerName)
949950
},
950951
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
@@ -956,11 +957,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
956957
}, res)
957958

958959
// The trigger function has been dropped.
959-
triggerFnName := migrations.TriggerFunctionName("products", "description")
960+
triggerFnName := backfill.TriggerFunctionName("products", "description")
960961
FunctionMustNotExist(t, db, schema, triggerFnName)
961962

962963
// The trigger has been dropped.
963-
triggerName := migrations.TriggerName("products", "description")
964+
triggerName := backfill.TriggerName("products", "description")
964965
TriggerMustNotExist(t, db, schema, "products", triggerName)
965966
},
966967
},
@@ -1022,11 +1023,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
10221023
},
10231024
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
10241025
// The trigger function has been dropped.
1025-
triggerFnName := migrations.TriggerFunctionName("products", "description")
1026+
triggerFnName := backfill.TriggerFunctionName("products", "description")
10261027
FunctionMustNotExist(t, db, schema, triggerFnName)
10271028

10281029
// The trigger has been dropped.
1029-
triggerName := migrations.TriggerName("products", "description")
1030+
triggerName := backfill.TriggerName("products", "description")
10301031
TriggerMustNotExist(t, db, schema, "products", triggerName)
10311032
},
10321033
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
@@ -1038,11 +1039,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
10381039
}, res)
10391040

10401041
// The trigger function has been dropped.
1041-
triggerFnName := migrations.TriggerFunctionName("products", "description")
1042+
triggerFnName := backfill.TriggerFunctionName("products", "description")
10421043
FunctionMustNotExist(t, db, schema, triggerFnName)
10431044

10441045
// The trigger has been dropped.
1045-
triggerName := migrations.TriggerName("products", "description")
1046+
triggerName := backfill.TriggerName("products", "description")
10461047
TriggerMustNotExist(t, db, schema, "products", triggerName)
10471048
},
10481049
},
@@ -1106,11 +1107,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
11061107
},
11071108
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
11081109
// The trigger function has been dropped.
1109-
triggerFnName := migrations.TriggerFunctionName("products", "description")
1110+
triggerFnName := backfill.TriggerFunctionName("products", "description")
11101111
FunctionMustNotExist(t, db, schema, triggerFnName)
11111112

11121113
// The trigger has been dropped.
1113-
triggerName := migrations.TriggerName("products", "description")
1114+
triggerName := backfill.TriggerName("products", "description")
11141115
TriggerMustNotExist(t, db, schema, "products", triggerName)
11151116
},
11161117
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
@@ -1122,11 +1123,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
11221123
}, res)
11231124

11241125
// The trigger function has been dropped.
1125-
triggerFnName := migrations.TriggerFunctionName("products", "description")
1126+
triggerFnName := backfill.TriggerFunctionName("products", "description")
11261127
FunctionMustNotExist(t, db, schema, triggerFnName)
11271128

11281129
// The trigger has been dropped.
1129-
triggerName := migrations.TriggerName("products", "description")
1130+
triggerName := backfill.TriggerName("products", "description")
11301131
TriggerMustNotExist(t, db, schema, "products", triggerName)
11311132
},
11321133
},
@@ -1197,11 +1198,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
11971198
},
11981199
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
11991200
// The trigger function has been dropped.
1200-
triggerFnName := migrations.TriggerFunctionName("products", "description")
1201+
triggerFnName := backfill.TriggerFunctionName("products", "description")
12011202
FunctionMustNotExist(t, db, schema, triggerFnName)
12021203

12031204
// The trigger has been dropped.
1204-
triggerName := migrations.TriggerName("products", "description")
1205+
triggerName := backfill.TriggerName("products", "description")
12051206
TriggerMustNotExist(t, db, schema, "products", triggerName)
12061207
},
12071208
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
@@ -1214,11 +1215,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
12141215
}, res)
12151216

12161217
// The trigger function has been dropped.
1217-
triggerFnName := migrations.TriggerFunctionName("products", "description")
1218+
triggerFnName := backfill.TriggerFunctionName("products", "description")
12181219
FunctionMustNotExist(t, db, schema, triggerFnName)
12191220

12201221
// The trigger has been dropped.
1221-
triggerName := migrations.TriggerName("products", "description")
1222+
triggerName := backfill.TriggerName("products", "description")
12221223
TriggerMustNotExist(t, db, schema, "products", triggerName)
12231224
},
12241225
},

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_change_type_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/stretchr/testify/assert"
1212

13+
"github.com/xataio/pgroll/pkg/backfill"
1314
"github.com/xataio/pgroll/pkg/migrations"
1415
"github.com/xataio/pgroll/pkg/roll"
1516
)
@@ -131,14 +132,14 @@ func TestChangeColumnType(t *testing.T) {
131132
}, rows)
132133

133134
// The up function no longer exists.
134-
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName("reviews", "rating"))
135+
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName("reviews", "rating"))
135136
// The down function no longer exists.
136-
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName("reviews", migrations.TemporaryName("rating")))
137+
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName("reviews", migrations.TemporaryName("rating")))
137138

138139
// The up trigger no longer exists.
139-
TriggerMustNotExist(t, db, schema, "reviews", migrations.TriggerName("reviews", "rating"))
140+
TriggerMustNotExist(t, db, schema, "reviews", backfill.TriggerName("reviews", "rating"))
140141
// The down trigger no longer exists.
141-
TriggerMustNotExist(t, db, schema, "reviews", migrations.TriggerName("reviews", migrations.TemporaryName("rating")))
142+
TriggerMustNotExist(t, db, schema, "reviews", backfill.TriggerName("reviews", migrations.TemporaryName("rating")))
142143
},
143144
},
144145
{

pkg/migrations/op_common_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -981,14 +981,14 @@ func TableMustBeCleanedUp(t *testing.T, db *sql.DB, schema, table string, column
981981
ColumnMustNotExist(t, db, schema, table, backfill.CNeedsBackfillColumn)
982982

983983
// The up function for the column no longer exists.
984-
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName(table, column))
984+
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName(table, column))
985985
// The down function for the column no longer exists.
986-
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName(table, migrations.TemporaryName(column)))
986+
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName(table, migrations.TemporaryName(column)))
987987

988988
// The up trigger for the column no longer exists.
989-
TriggerMustNotExist(t, db, schema, table, migrations.TriggerName(table, column))
989+
TriggerMustNotExist(t, db, schema, table, backfill.TriggerName(table, column))
990990
// The down trigger for the column no longer exists.
991-
TriggerMustNotExist(t, db, schema, table, migrations.TriggerName(table, migrations.TemporaryName(column)))
991+
TriggerMustNotExist(t, db, schema, table, backfill.TriggerName(table, migrations.TemporaryName(column)))
992992
}
993993
}
994994

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
}

0 commit comments

Comments
 (0)