Skip to content

Commit aa0c6a4

Browse files
Ignore duplicate inferred migrations having the same timestamp (#369)
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.
1 parent c4e4ee3 commit aa0c6a4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

pkg/state/state.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ BEGIN
311311
RETURN;
312312
END IF;
313313
314+
-- Remove any duplicate inferred migrations with the same timestamp for this
315+
-- schema. We assume such migrations are multi-statement batched migrations
316+
-- and we are only interested in the last one in the batch.
317+
DELETE FROM %[1]s.migrations
318+
WHERE schema = schemaname
319+
AND created_at = current_timestamp
320+
AND migration_type = 'inferred'
321+
AND migration->'operations'->0->'sql'->>'up' = current_query();
322+
314323
-- Someone did a schema change without pgroll, include it in the history
315324
SELECT INTO migration_id pg_catalog.format('sql_%%s',pg_catalog.substr(pg_catalog.md5(pg_catalog.random()::text), 0, 15));
316325
@@ -399,7 +408,7 @@ func (s *State) Init(ctx context.Context) error {
399408
}
400409

401410
// Perform pgroll state initialization
402-
_, err = tx.ExecContext(ctx, fmt.Sprintf(sqlInit, pq.QuoteIdentifier(s.schema)))
411+
_, err = tx.ExecContext(ctx, fmt.Sprintf(sqlInit, pq.QuoteIdentifier(s.schema), pq.QuoteLiteral(s.schema)))
403412
if err != nil {
404413
return err
405414
}

0 commit comments

Comments
 (0)