Skip to content

Commit

Permalink
Ignore duplicate inferred migrations having the same timestamp (#369)
Browse files Browse the repository at this point in the history
Remove all but the last inferred migration in a group of inferred
migrations that occur at the same timestamp with the same migration SQL.

Such inferred migrations are typically part of a batched transaction for
which we want to ignore all but the effect of the last statement in the
batch.
  • Loading branch information
andrew-farries committed Jul 10, 2024
1 parent c4e4ee3 commit aa0c6a4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ BEGIN
RETURN;
END IF;
-- Remove any duplicate inferred migrations with the same timestamp for this
-- schema. We assume such migrations are multi-statement batched migrations
-- and we are only interested in the last one in the batch.
DELETE FROM %[1]s.migrations
WHERE schema = schemaname
AND created_at = current_timestamp
AND migration_type = 'inferred'
AND migration->'operations'->0->'sql'->>'up' = current_query();
-- Someone did a schema change without pgroll, include it in the history
SELECT INTO migration_id pg_catalog.format('sql_%%s',pg_catalog.substr(pg_catalog.md5(pg_catalog.random()::text), 0, 15));
Expand Down Expand Up @@ -399,7 +408,7 @@ func (s *State) Init(ctx context.Context) error {
}

// Perform pgroll state initialization
_, err = tx.ExecContext(ctx, fmt.Sprintf(sqlInit, pq.QuoteIdentifier(s.schema)))
_, err = tx.ExecContext(ctx, fmt.Sprintf(sqlInit, pq.QuoteIdentifier(s.schema), pq.QuoteLiteral(s.schema)))
if err != nil {
return err
}
Expand Down

0 comments on commit aa0c6a4

Please sign in to comment.