| name | database-migrations |
| description | Use when creating alembic migrations, applying migrations to remote environments, or recovering from schema drift. Triggers on changes to models.py, "run migration", "schema drift", "alembic", "database error in batch jobs". |
Instructions
Do not write out alembic migrations yourself. Use the alembic tool to generate and apply migrations.
You do not need to give alembic the path to alembic.ini.
Do not manually drop any tables or columns in the DB. Always use alembic migrations to make schema changes.
Key Paths
- Models:
hawk/core/db/models.py
- Migrations:
hawk/core/db/alembic/versions/
- Alembic config:
hawk/core/db/alembic/
- Tests:
tests/core/db/test_alembic_migrations.py
Creating Migrations
alembic revision --autogenerate -m "description of changes"
ruff check --fix && ruff format
You may need to ensure the DB is up to date before generating a new migration. Run alembic upgrade head.
If you need to regenerate a migration (after making model changes since the last migration):
- Run
alembic downgrade -1 to revert the last migration.
- Delete the migration file from the versions/ directory.
- Run
alembic upgrade head to ensure the DB is up to date.
- Run the revision command again to generate a new migration file.
Running Migrations Against Remote Environments
Alembic depends on having a valid DATABASE_URL set. The username should be inspect_admin. The password is automatically generated via RDS IAM.
The URL depends on the environment. Use tofu output to get the correct URL:
tofu -chdir=terraform output -var-file="terraform.tfvars" -raw warehouse_database_url_admin
Note: These commands may require AWS credentials for the target account (e.g., set AWS_PROFILE if needed for RDS IAM auth and tofu commands).
DATABASE_URL=$(tofu -chdir=terraform output \
-var-file="terraform.tfvars" -raw warehouse_database_url_admin) \
alembic upgrade head
Other useful commands:
DATABASE_URL=$(tofu -chdir=terraform output \
-var-file="terraform.tfvars" -raw warehouse_database_url_admin) \
alembic current
DATABASE_URL=$(tofu -chdir=terraform output \
-var-file="terraform.tfvars" -raw warehouse_database_url_admin) \
alembic upgrade head --sql
Note: Dev environments each have their own warehouse DB, even though they share staging S3 and EventBridge. Make sure terraform.tfvars points to the correct environment.
Schema Drift Recovery
Schema drift means the database schema doesn't match what the code/migrations expect. This can happen when:
- A migration was applied to the DB but later removed from the codebase
- Someone modified the DB schema directly
- A migration was partially applied
How to Detect
- In CI:
test_migrations_are_up_to_date_with_models fails
- In production/dev: Batch jobs fail with errors like
NotNullViolationError: null value in column "X" (column exists in DB but code doesn't set it)
- Manually: Run
alembic upgrade head and check for errors, or compare alembic current against expected head
How to Fix
Create a migration that brings code in sync with DB reality:
def upgrade() -> None:
op.execute("ALTER TABLE my_table ADD COLUMN IF NOT EXISTS my_column text")
op.execute("UPDATE my_table SET my_column = 'default' WHERE my_column IS NULL")
op.execute("ALTER TABLE my_table ALTER COLUMN my_column SET NOT NULL")
Nuclear Option (Dev Environments Only)
If a dev warehouse is too drifted to migrate cleanly, tear it down and recreate:
tofu -chdir=terraform destroy \
-var-file="terraform.tfvars" -target=module.warehouse
tofu -chdir=terraform apply \
-var-file="terraform.tfvars" -target=module.warehouse
DATABASE_URL=$(tofu -chdir=terraform output \
-var-file="terraform.tfvars" -raw warehouse_database_url_admin) \
alembic upgrade head
Never do this in staging or production. Only for dev environments where data loss is acceptable.
When Migrations Fail
- Check current state:
alembic current
- Revert:
alembic downgrade -1
- Fix the migration file
- Test locally:
pytest tests/core/db/test_alembic_migrations.py -vv
- Retry:
alembic upgrade head
If downgrade also fails, use the nuclear option above (dev only) or fix forward by creating a new corrective migration.
Verification
Run before committing migration changes:
pytest tests/core/db/test_alembic_migrations.py -vv
This test suite:
- Applies all migrations from scratch to a fresh database
- Tests downgrade/upgrade cycle for reversibility
- Compares actual schema to SQLAlchemy models (catches drift)
- Verifies no multiple heads (branched history)
Related Skills
deploy-dev skill: For deploying migration changes to dev environments
smoke-tests skill: For verifying the deployment works after migration