OpenShield manages its PostgreSQL schema with Alembic. Application queries remain raw SQL; migration files define schema operations explicitly and do not introduce ORM models or SQLAlchemy metadata.
All commands below run from the repository root and require DATABASE_URL in the
environment:
export DATABASE_URL=postgresql://openshield:openshield@localhost:5432/openshieldCreate the complete current schema by upgrading to the latest revision:
alembic upgrade head
alembic currentProduction startup runs alembic upgrade head once, before starting the worker
and Gunicorn. The Flask application does not create or migrate tables.
For a brand-new database:
- Set
DATABASE_URL. - Deploy normally;
startup.shrunsalembic upgrade head. - Confirm
alembic currentreports the head revision.
For an existing production database that already has OpenShield tables:
- Back up the database.
- Verify the live schema matches the baseline revision in
alembic/versions/. - Run
alembic stamp headonce before the first Alembic-enabled deployment. - Deploy normally;
startup.shmust continue to run onlyalembic upgrade head.
Do not automate alembic stamp head in application code, startup scripts,
containers, or deployment configuration.
The baseline migration represents the schema that OpenShield already created in
production before Alembic was adopted. It must not be run against an unversioned
database that already contains the scans and findings tables.
Before deploying this Alembic-enabled release to an existing production database:
-
Back up the database.
-
Verify that
scans,findings, their columns, constraints, and indexes match the baseline revision inalembic/versions/. -
Record the existing schema at the baseline revision without executing DDL:
alembic stamp head alembic current
This is a one-time operational step for existing databases. Never add automatic stamping to application code, startup scripts, containers, or deployment configuration. After stamping, normal deployments use only:
alembic upgrade headCreate an empty revision with a short, descriptive, imperative message:
alembic revision -m "add finding source"Because OpenShield has no ORM metadata, do not use --autogenerate. Implement
both upgrade() and downgrade() explicitly with Alembic operations, then review
the generated file before applying it.
Migration files use Alembic's standard <revision>_<slug>.py naming. Keep the
message lowercase and specific to one schema change, for example
add_scan_region or index_findings_detected_at. Each revision must point to the
current head through down_revision; do not create parallel heads.
- Start PostgreSQL and export
DATABASE_URL. - Update to the current schema with
alembic upgrade head. - Create and implement the new revision.
- Test the upgrade from the previous revision.
- Test
alembic downgrade -1and thenalembic upgrade headon disposable data. - Test the full migration chain against a fresh empty database.
- Run the application test suite.
Useful inspection commands:
alembic current
alembic history --verbose
alembic heads
alembic upgrade head --sqlTreat downgrades as destructive operations and run them only against a database whose data can be restored.