Skip to content

Replay plain-numbered migrations in number order - #64

Open
chiragjangiir wants to merge 1 commit into
mj-hahn:mainfrom
chiragjangiir:fix/natural-migration-order
Open

chiragjangiir wants to merge 1 commit into
mj-hahn:mainfrom
chiragjangiir:fix/natural-migration-order

Conversation

@chiragjangiir

Copy link
Copy Markdown

Closes #57.

What happens today

_migration_files returns the files in source.list_files order, which is plain lexicographic, so a plain-numbered folder replays 10_drop_legacy.sql before 2_add_legacy.sql. The drop then hits a column that does not exist yet (counted in unknown), and the later ADD COLUMN puts it back, so the extracted model keeps a column the real database no longer has.

Fix

migration_sort_key orders a file by the numbers in its base name — the split on digit runs keeps text parts compared as lowercased text — so 1_, 2_, 10_ replay in number order while timestamped names (Supabase, dbmate) keep the order they already had.

Tests

  • Ordering.test_plain_numbered_migrations_run_in_number_order: 1_init.sql creates users(id), 2_add_legacy.sql adds legacy, 10_drop_legacy.sql drops it; asserts the model has no legacy column.
  • Ordering.test_timestamped_names_keep_their_order: timestamped names still create then drop.

Verified with python3.12 -m unittest tests.test_postgres: with core/extractors/postgres.py stashed (the old sorting) the first test fails with 'legacy' unexpectedly found in {...} and the second passes; with the change, all 32 tests in the module pass.

I did not run the full suite or the live PostgreSQL check (PGBIN) — the change is confined to file ordering. The changelog has an entry under [Unreleased] → ### Fixed.

Lexicographic order ran 10_drop_legacy.sql before 2_add_legacy.sql, keeping the
column the later migration drops. Sort by the leading number, then the rest;
timestamped names keep their order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrations sort as text, so 10_ runs before 2_

1 participant