Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pkg/backfill/trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: Apache-2.0

package backfill

import (
"github.com/xataio/pgroll/pkg/schema"
)

type TriggerDirection string

const (
TriggerDirectionUp TriggerDirection = "up"
TriggerDirectionDown TriggerDirection = "down"
)

type TriggerConfig struct {
Name string
Direction TriggerDirection
Columns map[string]*schema.Column
SchemaName string
TableName string
PhysicalColumn string
LatestSchema string
SQL string
NeedsBackfillColumn string
}

// TriggerFunctionName returns the name of the trigger function
// for a given table and column.
func TriggerFunctionName(tableName, columnName string) string {
return "_pgroll_trigger_" + tableName + "_" + columnName
}

// TriggerName returns the name of the trigger for a given table and column.
func TriggerName(tableName, columnName string) string {
return TriggerFunctionName(tableName, columnName)
}
10 changes: 5 additions & 5 deletions pkg/migrations/op_add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func (o *OpAddColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSch
var tableToBackfill *schema.Table
if o.Up != "" {
err := NewCreateTriggerAction(conn,
triggerConfig{
Name: TriggerName(o.Table, o.Column.Name),
Direction: TriggerDirectionUp,
backfill.TriggerConfig{
Name: backfill.TriggerName(o.Table, o.Column.Name),
Direction: backfill.TriggerDirectionUp,
Columns: table.Columns,
SchemaName: s.Name,
LatestSchema: latestSchema,
Expand Down Expand Up @@ -149,7 +149,7 @@ func (o *OpAddColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *sch
return err
}

err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func (o *OpAddColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *sch
return err
}

err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column.Name)).Execute(ctx)
if err != nil {
return err
}
Expand Down
41 changes: 21 additions & 20 deletions pkg/migrations/op_add_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/xataio/pgroll/internal/testutils"
"github.com/xataio/pgroll/pkg/backfill"
"github.com/xataio/pgroll/pkg/migrations"
)

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

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

// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

// The trigger has been dropped.
triggerName := migrations.TriggerName("products", "description")
triggerName := backfill.TriggerName("products", "description")
TriggerMustNotExist(t, db, schema, "products", triggerName)
},
},
Expand Down Expand Up @@ -940,11 +941,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

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

// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

// The trigger has been dropped.
triggerName := migrations.TriggerName("products", "description")
triggerName := backfill.TriggerName("products", "description")
TriggerMustNotExist(t, db, schema, "products", triggerName)
},
},
Expand Down Expand Up @@ -1022,11 +1023,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

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

// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

// The trigger has been dropped.
triggerName := migrations.TriggerName("products", "description")
triggerName := backfill.TriggerName("products", "description")
TriggerMustNotExist(t, db, schema, "products", triggerName)
},
},
Expand Down Expand Up @@ -1106,11 +1107,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

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

// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

// The trigger has been dropped.
triggerName := migrations.TriggerName("products", "description")
triggerName := backfill.TriggerName("products", "description")
TriggerMustNotExist(t, db, schema, "products", triggerName)
},
},
Expand Down Expand Up @@ -1197,11 +1198,11 @@ func TestAddColumnWithUpSql(t *testing.T) {
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

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

// The trigger function has been dropped.
triggerFnName := migrations.TriggerFunctionName("products", "description")
triggerFnName := backfill.TriggerFunctionName("products", "description")
FunctionMustNotExist(t, db, schema, triggerFnName)

// The trigger has been dropped.
triggerName := migrations.TriggerName("products", "description")
triggerName := backfill.TriggerName("products", "description")
TriggerMustNotExist(t, db, schema, "products", triggerName)
},
},
Expand Down
18 changes: 9 additions & 9 deletions pkg/migrations/op_alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func (o *OpAlterColumn) Start(ctx context.Context, l Logger, conn db.DB, latestS

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

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

// Remove the up and down function and trigger
err = NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column), TriggerFunctionName(o.Table, TemporaryName(o.Column))).Execute(ctx)
err = NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column), backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column))).Execute(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -170,8 +170,8 @@ func (o *OpAlterColumn) Rollback(ctx context.Context, l Logger, conn db.DB, s *s
// Remove the up and down functions and triggers
if err := NewDropFunctionAction(
conn,
TriggerFunctionName(o.Table, o.Column),
TriggerFunctionName(o.Table, TemporaryName(o.Column)),
backfill.TriggerFunctionName(o.Table, o.Column),
backfill.TriggerFunctionName(o.Table, TemporaryName(o.Column)),
).Execute(ctx); err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/migrations/op_change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/xataio/pgroll/pkg/backfill"
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/roll"
)
Expand Down Expand Up @@ -131,14 +132,14 @@ func TestChangeColumnType(t *testing.T) {
}, rows)

// The up function no longer exists.
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName("reviews", "rating"))
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName("reviews", "rating"))
// The down function no longer exists.
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName("reviews", migrations.TemporaryName("rating")))
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName("reviews", migrations.TemporaryName("rating")))

// The up trigger no longer exists.
TriggerMustNotExist(t, db, schema, "reviews", migrations.TriggerName("reviews", "rating"))
TriggerMustNotExist(t, db, schema, "reviews", backfill.TriggerName("reviews", "rating"))
// The down trigger no longer exists.
TriggerMustNotExist(t, db, schema, "reviews", migrations.TriggerName("reviews", migrations.TemporaryName("rating")))
TriggerMustNotExist(t, db, schema, "reviews", backfill.TriggerName("reviews", migrations.TemporaryName("rating")))
},
},
{
Expand Down
8 changes: 4 additions & 4 deletions pkg/migrations/op_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,14 +981,14 @@ func TableMustBeCleanedUp(t *testing.T, db *sql.DB, schema, table string, column
ColumnMustNotExist(t, db, schema, table, backfill.CNeedsBackfillColumn)

// The up function for the column no longer exists.
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName(table, column))
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName(table, column))
// The down function for the column no longer exists.
FunctionMustNotExist(t, db, schema, migrations.TriggerFunctionName(table, migrations.TemporaryName(column)))
FunctionMustNotExist(t, db, schema, backfill.TriggerFunctionName(table, migrations.TemporaryName(column)))

// The up trigger for the column no longer exists.
TriggerMustNotExist(t, db, schema, table, migrations.TriggerName(table, column))
TriggerMustNotExist(t, db, schema, table, backfill.TriggerName(table, column))
// The down trigger for the column no longer exists.
TriggerMustNotExist(t, db, schema, table, migrations.TriggerName(table, migrations.TemporaryName(column)))
TriggerMustNotExist(t, db, schema, table, backfill.TriggerName(table, migrations.TemporaryName(column)))
}
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/migrations/op_create_constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, la
for _, colName := range o.Columns {
upSQL := o.Up[colName]
err := NewCreateTriggerAction(conn,
triggerConfig{
Name: TriggerName(o.Table, colName),
Direction: TriggerDirectionUp,
backfill.TriggerConfig{
Name: backfill.TriggerName(o.Table, colName),
Direction: backfill.TriggerDirectionUp,
Columns: table.Columns,
SchemaName: s.Name,
LatestSchema: latestSchema,
Expand All @@ -71,9 +71,9 @@ func (o *OpCreateConstraint) Start(ctx context.Context, l Logger, conn db.DB, la

downSQL := o.Down[colName]
err = NewCreateTriggerAction(conn,
triggerConfig{
Name: TriggerName(o.Table, TemporaryName(colName)),
Direction: TriggerDirectionDown,
backfill.TriggerConfig{
Name: backfill.TriggerName(o.Table, TemporaryName(colName)),
Direction: backfill.TriggerDirectionDown,
Columns: table.Columns,
LatestSchema: latestSchema,
SchemaName: s.Name,
Expand Down Expand Up @@ -205,8 +205,8 @@ func (o *OpCreateConstraint) Rollback(ctx context.Context, l Logger, conn db.DB,
func (o *OpCreateConstraint) removeTriggers(ctx context.Context, conn db.DB) error {
dropFuncs := make([]string, 0, len(o.Columns)*2)
for _, column := range o.Columns {
dropFuncs = append(dropFuncs, TriggerFunctionName(o.Table, column))
dropFuncs = append(dropFuncs, TriggerFunctionName(o.Table, TemporaryName(column)))
dropFuncs = append(dropFuncs, backfill.TriggerFunctionName(o.Table, column))
dropFuncs = append(dropFuncs, backfill.TriggerFunctionName(o.Table, TemporaryName(column)))
}
return NewDropFunctionAction(conn, dropFuncs...).Execute(ctx)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/migrations/op_drop_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func (o *OpDropColumn) Start(ctx context.Context, l Logger, conn db.DB, latestSc

if o.Down != "" {
err := NewCreateTriggerAction(conn,
triggerConfig{
Name: TriggerName(o.Table, o.Column),
Direction: TriggerDirectionDown,
backfill.TriggerConfig{
Name: backfill.TriggerName(o.Table, o.Column),
Direction: backfill.TriggerDirectionDown,
Columns: s.GetTable(o.Table).Columns,
SchemaName: s.Name,
LatestSchema: latestSchema,
Expand Down Expand Up @@ -58,7 +58,7 @@ func (o *OpDropColumn) Complete(ctx context.Context, l Logger, conn db.DB, s *sc
return err
}

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

table := s.GetTable(o.Table)

err := NewDropFunctionAction(conn, TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
err := NewDropFunctionAction(conn, backfill.TriggerFunctionName(o.Table, o.Column)).Execute(ctx)
if err != nil {
return err
}
Expand Down
Loading
Loading