Skip to content

Commit

Permalink
Move call sites over to new createTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Aug 29, 2023
1 parent 3806e5d commit 8123af4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
9 changes: 5 additions & 4 deletions pkg/migrations/op_add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ func (o *OpAddColumn) Start(ctx context.Context, conn *sql.DB, schemaName, state
}

if o.Up != nil {
err := createTrigger(ctx, conn, s, triggerConfig{
err := createTrigger(ctx, conn, triggerConfig{
Name: TriggerName(o.Table, o.Column.Name),
Direction: TriggerDirectionUp,
Columns: s.GetTable(o.Table).Columns,
SchemaName: schemaName,
StateSchema: stateSchema,
Table: o.Table,
Column: o.Column.Name,
TableName: o.Table,
PhysicalColumn: TemporaryName(o.Column.Name),
StateSchema: stateSchema,
SQL: *o.Up,
})
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/migrations/op_drop_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ var _ Operation = (*OpDropColumn)(nil)

func (o *OpDropColumn) Start(ctx context.Context, conn *sql.DB, schemaName string, stateSchema string, s *schema.Schema) error {
if o.Down != nil {
err := createTrigger(ctx, conn, s, triggerConfig{
err := createTrigger(ctx, conn, triggerConfig{
Name: TriggerName(o.Table, o.Column),
Direction: TriggerDirectionDown,
Columns: s.GetTable(o.Table).Columns,
SchemaName: schemaName,
StateSchema: stateSchema,
Table: o.Table,
Column: o.Column,
TableName: o.Table,
PhysicalColumn: o.Column,
StateSchema: stateSchema,
SQL: *o.Down,
})
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions pkg/migrations/op_set_notnull.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ func (o *OpSetNotNull) Start(ctx context.Context, conn *sql.DB, schemaName strin
}

// Add a trigger to copy values from the old column to the new, rewriting NULL values using the `up` SQL.
err := createTrigger(ctx, conn, s, triggerConfig{
err := createTrigger(ctx, conn, triggerConfig{
Name: TriggerName(o.Table, o.Column),
Direction: TriggerDirectionUp,
Columns: table.Columns,
SchemaName: schemaName,
StateSchema: stateSchema,
Table: o.Table,
Column: o.Column,
TableName: o.Table,
PhysicalColumn: TemporaryName(o.Column),
StateSchema: stateSchema,
SQL: *o.Up,
TestExpr: fmt.Sprintf("NEW.%s IS NULL", pq.QuoteIdentifier(o.Column)),
ElseExpr: fmt.Sprintf("NEW.%s = NEW.%s;",
ElseExpr: fmt.Sprintf("NEW.%s = NEW.%s",
pq.QuoteIdentifier(TemporaryName(o.Column)),
pq.QuoteIdentifier(o.Column)),
})
Expand All @@ -55,13 +56,14 @@ func (o *OpSetNotNull) Start(ctx context.Context, conn *sql.DB, schemaName strin
}

// Add a trigger to copy values from the new column to the old.
err = createTrigger(ctx, conn, s, triggerConfig{
err = createTrigger(ctx, conn, triggerConfig{
Name: TriggerName(o.Table, TemporaryName(o.Column)),
Direction: TriggerDirectionDown,
Columns: table.Columns,
SchemaName: schemaName,
StateSchema: stateSchema,
Table: o.Table,
Column: TemporaryName(o.Column),
TableName: o.Table,
PhysicalColumn: o.Column,
StateSchema: stateSchema,
SQL: fmt.Sprintf("NEW.%s", pq.QuoteIdentifier(TemporaryName(o.Column))),
})
if err != nil {
Expand Down

0 comments on commit 8123af4

Please sign in to comment.