Flatten alembic migrations #4984
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.



Flattens 125 Alembic migrations into 2 migrations to improve maintainability and deployment speed. Archived migrations are preserved in
alembic/versions/archive/for historical reference. Note: no production migrations will run, this only applies to databases that bootstrap from scratch (e.g. dev or local). New migrations that run on production will run the same way.The dev deployment runs in ~3 mins now, e.g. https://github.com/bcgov/wps/actions/runs/20625237422/job/59234759443?pr=4984
New Migrations:
9bb0dc8ed7fb_initial_schema.py- Creates complete database schema including partitioned tables6157a8d08f28_seed_application_data.py- Loads 18 application data tables from compressed SQL dump (40 MB gzipped)Auto-Stamping for Production:
alembic/env.pyto automatically detect production databases at old revisiond276ba9eed1f6157a8d08f28without running migrations whenENVIRONMENT=productionMetadata Registry:
Testing locally
Prerequisites:
d276ba9eed1fTest Steps:
Verify starting state:
psql -d wps -c "SELECT version_num FROM alembic_version;"# Should show: d276ba9eed1fRun auto-stamping:
source <root-path>/wps/backend/.venv/bin/activateENVIRONMENT=production alembic upgrade headExpected output:
✓ Production database stamped from d276ba9eed1f to 6157a8d08f28INFO [alembic.runtime.migration] Context impl PostgresqlImpl.INFO [alembic.runtime.migration] Will assume transactional DDL.Verify stamped revision:
psql -d wps -c "SELECT version_num FROM alembic_version;"# Should show: 6157a8d08f28Verify data preserved:
psql -d wps -c "SELECT COUNT(*) FROM advisory_shapes;"# Should show existing data intact (40 rows)Testing Normal Migration Flow (Fresh Database)
Test that migrations still work for new databases:
Create fresh test database:
createdb wps_testRun migrations without production flag:
# Update alembic.ini or env variable to point to wps_testalembic upgrade headExpected behavior:
- Runs 9bb0dc8ed7fb_initial_schema (creates all tables)
- Runs 6157a8d08f28_seed_application_data (loads data)
- No auto-stamping occurs
Verify:
psql -d wps_test -c "SELECT version_num FROM alembic_version;"# Should show: 6157a8d08f28psql -d wps_test -c "SELECT COUNT(*) FROM application_data_tables;"# Should show: 18Testing Edge Cases
Test already at head:
# Run again on already-stamped databaseENVIRONMENT=production alembic upgrade head# Should output: (no stamping message, already at head)Test wrong head revision (assertion):
# If someone adds a new migration after 6157a8d08f28# The assertion will fail with clear error messageReset for retesting:
# Reset to old revision to test againpsql -d wps -c "UPDATE alembic_version SET version_num = 'd276ba9eed1f';"Test Links:
Landing Page
MoreCast
Percentile Calculator
C-Haines
FireCalc
FireCalc bookmark
Auto Spatial Advisory (ASA)
HFI Calculator
SFMS Insights
Fire Watch