Migration to Alembic¶
Overview¶
The ibutsu-server backend has been refactored to use Alembic for database schema management, replacing the previous custom upgrade system.
Changes Made¶
Removed Old Upgrade System¶
Deleted:
ibutsu_server/db/upgrades.py- contained upgrade functions 1-9
Removed from ``ibutsu_server/__init__.py``:
Import of
upgradesmoduleImport of
upgrade_dbfunctionCall to
db.create_all()(dangerous in production)Call to
upgrade_db(db.session, upgrades)
Removed from ``ibutsu_server/db/models.py``:
upgrade_db()function definition
Updated tests:
Removed mocks for
db.create_all()andupgrade_db()fromtests/test_init.py
Added Alembic Configuration¶
Created the following files in backend/:
backend/
├── alembic.ini # Alembic configuration
└── alembic/
├── env.py # Flask-SQLAlchemy integration
├── script.py.mako # Migration template
├── README.md # Documentation
└── versions/ # Migration scripts directory
└── .gitkeep
Next Steps: Creating the Initial Migration¶
After the local development pod is started with:
./scripts/ibutsu-pod.sh -d -v -p -a
Step 1: Access the Pod¶
podman exec -it ibutsu-postgres bash
Step 3: Generate Initial Baseline Migration¶
This will create a migration that captures the current database schema:
alembic revision --autogenerate -m "Initial baseline"
This command will:
Inspect all SQLAlchemy models in
ibutsu_server/db/models.pyCompare with the current database state
Generate a migration file in
alembic/versions/
Step 4: Review the Generated Migration¶
Open the generated file in alembic/versions/ and review:
Ensure all tables are included
Check for any unexpected changes
Verify index and constraint definitions
Step 5: Apply the Migration¶
alembic upgrade head
Step 6: Verify Migration¶
alembic current
Should show the revision ID of your initial baseline.
Important Notes¶
Database Schema Management¶
Never use ``db.create_all()`` - It bypasses migrations and is dangerous in production
Always use Alembic for schema changes
Test migrations in development before applying to production
The Meta Table¶
The Meta model (table: meta) is still present in the codebase but is no longer used for version tracking. Alembic uses its own alembic_version table for tracking applied migrations.
Old Upgrade History¶
The previous upgrade system had 9 upgrade functions that performed:
Added dashboard_id to widget_configs
Added GIN indices for tags and requirements
Made result_id nullable in artifacts, added run_id
Added is_superadmin, is_active, activation_code to users
Added default_dashboard_id to projects
Fixed owner_id type in projects table
Added created column to imports table
Dropped reports and report_files tables
Migrated widget config parameters
These changes should already be reflected in your database schema. The initial baseline migration will capture the current state.
Future Workflow¶
Making Schema Changes¶
Modify SQLAlchemy models in
ibutsu_server/db/models.pyGenerate migration:
alembic revision --autogenerate -m "Description"Review and edit the generated migration if needed
Test the migration:
alembic upgrade headCommit the migration file to git
Applying Migrations in Production¶
Deploy new code with migration files
Run
alembic upgrade headbefore starting the applicationStart the application
Troubleshooting¶
“Can’t locate revision”¶
The alembic_version table may be missing or out of sync. This is expected on first setup.
Autogenerate detects unwanted changes¶
Review the migration and remove any changes that are:
Type variations (VARCHAR vs TEXT)
Index naming differences
Unchanged server defaults
Need to rollback¶
alembic downgrade -1 # Go back one revision
References¶
Backend README:
ibutsu-server/backend/README.mdAlembic README:
alembic/README.md