Skip to content

Conversation

@conbrad
Copy link
Collaborator

@conbrad conbrad commented Dec 31, 2025

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 tables
  • 6157a8d08f28_seed_application_data.py - Loads 18 application data tables from compressed SQL dump (40 MB gzipped)

Auto-Stamping for Production:

  • Modified alembic/env.py to automatically detect production databases at old revision d276ba9eed1f
  • Stamps to new head 6157a8d08f28 without running migrations when ENVIRONMENT=production
  • Prevents re-creating existing schema and data on production deployments

Metadata Registry:

  • Defines a new application registry table for marking tables as application data — data needed for applications to run at a baseline. These were dumped into a seed file with:
pg_dump --data-only --inserts --no-owner --no-privileges \
                                         -t advisory_fuel_types \
                                         -t advisory_hfi_classification_threshold \
                                         -t advisory_shape_fuels \
                                         -t advisory_shape_types \
                                         -t advisory_shapes \
                                         -t advisory_zone_status \
                                         -t combustible_area \
                                         -t fire_centres \
                                         -t fuel_type_raster \
                                         -t fuel_types \
                                         -t hfi_fire_centre_fire_start_range \
                                         -t hfi_fire_start_lookup \
                                         -t hfi_fire_start_range \
                                         -t planning_areas \
                                         -t planning_weather_stations \
                                         -t prediction_models \
                                         -t prescription_status \
                                         -t sfms_fuel_types \
                                         -t tpi_fuel_area \
                                         wps > /Users/cbrady/projects/wps/backend/packages/wps-api/alembic/data/application_seed_data.sql
  • Includes descriptions and seed order for each application table

Testing locally

Prerequisites:

  • Local database wps (copy of production) at revision d276ba9eed1f
  • Virtual environment activated

Test Steps:

  1. Verify starting state:
    psql -d wps -c "SELECT version_num FROM alembic_version;"
    # Should show: d276ba9eed1f

  2. Run auto-stamping:
    source <root-path>/wps/backend/.venv/bin/activate
    ENVIRONMENT=production alembic upgrade head

  3. Expected output:
    ✓ Production database stamped from d276ba9eed1f to 6157a8d08f28
    INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
    INFO [alembic.runtime.migration] Will assume transactional DDL.

  4. Verify stamped revision:
    psql -d wps -c "SELECT version_num FROM alembic_version;"
    # Should show: 6157a8d08f28

  5. Verify 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:

  1. Create fresh test database:
    createdb wps_test

  2. Run migrations without production flag:
    # Update alembic.ini or env variable to point to wps_test
    alembic upgrade head

  3. Expected behavior:
    - Runs 9bb0dc8ed7fb_initial_schema (creates all tables)
    - Runs 6157a8d08f28_seed_application_data (loads data)
    - No auto-stamping occurs

  4. Verify:
    psql -d wps_test -c "SELECT version_num FROM alembic_version;"
    # Should show: 6157a8d08f28

psql -d wps_test -c "SELECT COUNT(*) FROM application_data_tables;"
# Should show: 18

Testing Edge Cases

Test already at head:
# Run again on already-stamped database
ENVIRONMENT=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 message

Reset for retesting:
# Reset to old revision to test again
psql -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

- Archived 125 old migrations to alembic/versions/archive/
- Created 9bb0dc8ed7fb_initial_schema.py (schema + partitions)
- Created 6157a8d08f28_seed_application_data.py (18 application tables)
- Added application_data_tables metadata registry
- Compressed seed data to .sql.gz (40 MB) to stay under GitHub limit
- Auto-stamps production databases at d276ba9eed1f to avoid re-running migrations
- Uses shapely to validate geometry data during seed loading
@codecov
Copy link

codecov bot commented Dec 31, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.86%. Comparing base (75f7ea7) to head (07e3470).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4984   +/-   ##
=======================================
  Coverage   74.86%   74.86%           
=======================================
  Files         587      587           
  Lines       27610    27610           
  Branches     2045     2045           
=======================================
  Hits        20671    20671           
  Misses       6360     6360           
  Partials      579      579           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sonarqubecloud
Copy link

@conbrad conbrad requested review from brettedw and dgboss December 31, 2025 19:08
@conbrad conbrad changed the title Compress alembic migrations Flatten alembic migrations Dec 31, 2025
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.

2 participants