Database Migrations
Schema changes to the firmware MySQL database are managed with Alembic. Migration scripts are generated automatically from the SQLAlchemy ORM models in src/firmware_orm/ and stored in alembic/versions/.
Applying migrations (automatic)
You do not need to run alembic upgrade head manually to bring a database up to date. The db_migrations service in docker_compose_dir/docker-compose.yaml is a one-shot container that runs alembic upgrade head and then exits. Every application service depends_on its successful completion (service_completed_successfully), so on each docker compose up the database is migrated to the latest revision before the app starts:
docker compose -f docker_compose_dir/docker-compose.yaml up -d
This applies to both the production stack and the development stack (compose.dev.yaml extends the same base compose file, so db_migrations runs there too).
The manual setup and commands below are only needed when authoring migrations during development — generating a new revision, reviewing it, and testing upgrade/downgrade against a local database.
Prerequisites (development only)
- The MySQL container must be running:
mise run db:up
# Alternatively:
docker compose -f docker_compose_dir/docker-compose.yaml up -d mysql_db db_migrationscautionStarting only
mysql_dbskips thedb_migrationscontainer, so the schema is not brought up to date automatically. You then have to either apply migrations yourself withalembic upgrade headormise run db:migrate(see below) or also startdb_migrations(e.g.mise run db:upordocker compose -f docker_compose_dir/docker-compose.yaml up -d mysql_db db_migrations). Bringing up the full stack runsdb_migrationsfor you. - The Python environment must be active and dependencies installed:
Python 3.11 and the
.venvvirtual environment are managed automatically by mise (see Toolchain). Run the install task to ensure dependencies are installed:mise run install:python - All
alembiccommands must be run from the project root (wherepyproject.tomllives).
Environment Variables
Alembic reads the database connection from environment variables.
When using mise run db, the variables from docker_compose_dir/.env are loaded automatically:
mise run db -- <alembic-command>
If running alembic directly outside of mise, export them manually before running any command:
set -a && source docker_compose_dir/.env && set +a
| Variable | Default | Description |
|---|---|---|
DATABASE_HOST | localhost | MySQL host |
DATABASE_PORT | 3306 | MySQL port |
DATABASE_USER | scraper | MySQL user |
DATABASE_PASSWORD | password | MySQL password |
DATABASE_SCHEMA | firmware | Database / schema name |
Forgetting to export these variables is the most common source of Access denied errors when running Alembic locally. The .env file is only loaded automatically by Docker Compose and mise run db — not by your shell.
Common Commands
These are run locally during development. Remember that deployments apply migrations automatically through the db_migrations container (see Applying migrations).
Apply all pending migrations against a local database
mise run db:migrate
# Or via alembic:
alembic upgrade head
Roll back the last migration
mise run db -- downgrade -1
# Or via alembic:
alembic downgrade -1
Show the current revision of the live database
mise run db -- current
# Or via alembic:
alembic current
Show the full migration history
mise run db -- history --verbose
# Or via alembic:
alembic history --verbose
Adding a Migration
Follow these steps whenever a schema change is needed:
-
Update the ORM model in
src/firmware_orm/. -
Generate the migration script. Alembic compares the models against the live database and writes the diff:
mise run db:revision "short description of change"
# Or via alembic directly:
alembic revision --autogenerate -m "short description of change" -
Review the generated file in
alembic/versions/. Autogenerate is not perfect — always check that theupgrade()anddowngrade()functions are correct and complete before committing. -
Apply the migration locally to test it:
mise run db:migrate
# Or:
alembic upgrade head -
Commit the changed ORM model and the new migration script together in the same commit. Once merged, the
db_migrationscontainer applies it automatically on the nextdocker compose up— no manual upgrade step is needed in deployment.
How It Works
alembic/env.py builds the database URL from the environment variables listed above and passes Base.metadata to Alembic as target_metadata. All ORM models are imported at the top of env.py so their tables are visible to the autogenerate comparison.
The [tool.alembic] section in pyproject.toml points script_location to the alembic/ directory, which is why no path needs to be set in alembic.ini.
On deployment, the db_migrations container (src/Dockerfile_migrations) runs migrate_db() from src/firmware_orm/init_db.py, which is the programmatic equivalent of alembic upgrade head. It resolves the alembic/ script location relative to the package, so it works regardless of the working directory. Because the container reads DB_CONTAINER/MYSQL_*/DB_SCHEMA/DB_PORT from the compose environment, no shell export step is needed there — that is only required when running Alembic by hand on the host.
Per-tool analysis history migration
Revision 6f8f1f2a4c91 adds analysis_executions. The table records one
completed tool run with foreign keys to jobs, images and tools, plus
execution number, status, optional status message, optional finding count,
tool-output path and completion time.
The migration backfills existing tasks for tools selected in the job and their
currently supported direct dependencies. Because legacy tasks only stored one
combined success flag and output.txt, their per-tool status is inferred from
the task, their output path points to that combined file, and their finding
count remains NULL. New executor returns populate all per-tool fields
directly.
Merging the dashboard and boot-analysis migration branches
Revision b7c42a90e615 joins 6f8f1f2a4c91 (analysis history) and
9561599ff4a3 (boot-result primary key and longer paths). It is an empty
Alembic merge revision: both existing migrations remain unchanged and are
applied as needed, regardless of which branch a deployment previously used.
There is one migration head, so the normal db_migrations service can still
run upgrade head. Do not stamp past either migration or modify a live schema
manually.
Result deduplication uses the existing tables and transaction locks on images;
it does not add another table or require a schema migration. Existing historical
duplicates are not deleted automatically.
Initial Schema
The first migration (alembic/versions/83812b476ccf_initial_schema.py) captures the baseline schema. On a fresh database the MySQL container's entrypoint runs docker_compose_dir/sql_scripts/init.sql to create the tables, and Alembic's migration history starts from there. Going forward, migrations are the authoritative source of truth for all schema changes — init.sql is only used for bootstrapping a brand-new data volume.