Skip to content

v0.18.1: fix upgrade failures for pre-alembic databases#140

Merged
dannymcc merged 1 commit intomainfrom
dev
Apr 21, 2026
Merged

v0.18.1: fix upgrade failures for pre-alembic databases#140
dannymcc merged 1 commit intomainfrom
dev

Conversation

@dannymcc
Copy link
Copy Markdown
Owner

Summary

Resolves issues #132 and #136 — containers crashing after upgrade with sqlite3.OperationalError: no such column: users.default_vehicle_id.

Root cause

Databases created on v0.15.0 or earlier predate Flask-Migrate, so their alembic_version table is empty. The entrypoint's flask db upgrade then tried to replay every migration from the beginning and died on a duplicate column: odometer_unit error. The failure was silenced by 2>/dev/null || true, so the a1b2c3d4e5f6 migration that adds users.default_vehicle_id never ran, and create_app() then crashed in User.query.count().

Changes

  • Idempotent migrations998cdb1497c6, 613be8af4376, and a1b2c3d4e5f6 now no-op when their target column/nullability is already in place, so flask db upgrade succeeds even on a DB that was bootstrapped without Flask-Migrate.
  • Alembic bootstrap — new _bootstrap_alembic_version stamps the correct prior revision when the database has tables but no alembic row, so future upgrades only apply genuinely pending migrations.
  • Safety net_run_schema_migrations now also adds users.default_vehicle_id, guaranteeing the app can boot even if alembic state can't be recovered.
  • Visible errorsdocker-entrypoint.sh no longer swallows flask db upgrade failures; any remaining error surfaces in container logs.
  • Bumps APP_VERSION to 0.18.1.

Test plan

  • Full test suite passes (535 passed, 6 xfailed)
  • Simulated upgrade from a v0.15-style pre-alembic DB: flask db upgrade completes cleanly, alembic_version lands at head (b2c3d4e5f6a7), users.default_vehicle_id is present, app boots, User.query.count() returns without error
  • Deploy to staging and confirm a real upgrade path works

Containers upgrading from v0.15.0/v0.16.0 failed to start because the
a1b2c3d4e5f6 migration never ran: alembic_version was empty on those DBs,
so flask db upgrade replayed migrations from scratch and died on a
duplicate odometer_unit column, leaving users.default_vehicle_id missing
and create_app() crashing in User.query.count().

- Make the three pre-trip-templates migrations idempotent so they no-op
  when the column/nullability they add already exists.
- Add _bootstrap_alembic_version to stamp the correct revision when the
  DB has tables but no alembic row, so subsequent upgrades only apply
  pending migrations.
- Extend _run_schema_migrations with default_vehicle_id as a safety net
  so the app boots even if alembic state cannot be recovered.
- Stop silencing flask db upgrade failures in the entrypoint — a real
  upgrade error should be visible in container logs.

Bumps version to 0.18.1.
@dannymcc dannymcc merged commit fa441f1 into main Apr 21, 2026
1 check passed
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.

1 participant