Overview
Condo uses kmigrator, a Django-based migration tool, to manage database schema changes. Migrations are stored per-app underapps/condo/migrations/ and are applied automatically during setup.
Prerequisites
Before running migrations, ensure:- The
DATABASE_URLenvironment variable is set: - The database is accessible (running via Docker or directly on the host).
- Python dependencies are installed:
Common Commands
Run all pending migrations
Applies all migration files that have not yet been run against the database:Create migrations after schema changes
After modifying anyschema.ts file in a domain, generate a new migration file:
apps/condo/migrations/. Commit this file alongside your schema changes.
Revert the last migration
Unlock the migrations table
If a migration process was interrupted and the migrations table is locked:When to Create Migrations
Create a new migration any time you modify aschema.ts file in any domain. This includes:
- Adding or removing a field
- Changing a field type or constraint
- Adding or removing a model
- Changing a relationship
Migrations are required even for optional or nullable field additions. The database schema must stay in sync with the Keystone schema definitions.
Workflow Cases
Changed a schema during work-in-progress — recreate the migration
Changed a schema during work-in-progress — recreate the migration
If you change a schema file after already generating a migration for it, do not edit the migration file directly. Instead:
- Roll back the existing migration:
- Delete the migration file from
apps/condo/migrations/. - Regenerate:
Merging multiple migration files into one
Merging multiple migration files into one
If your branch contains several migration files that you want to consolidate, do not manually merge their SQL content. The
KMIGRATOR system comment in each file must match the file exactly.Instead:- Roll back each migration, one at a time:
- Delete the migration files.
- Regenerate a single file:
Staying in sync after a rebase
Staying in sync after a rebase
If you rebase onto
main and see that other migrations were merged ahead of yours, your migration number may no longer be sequential.- Roll back your migration:
- Delete your migration file.
- Regenerate:
Drop and Recreate (Local Only)
To completely reset your local database and reapply all migrations:Reversible vs. Non-Reversible Migrations
Some migrations cannot be safely reversed. For example, if a migration removes aNOT NULL constraint and records without that field are subsequently inserted, rolling back the migration will fail because those records would violate the restored constraint.
Be aware of this when using migrate:down in non-trivial cases.
Common Errors
CommandError: Conflicting migrations detected
CommandError: Conflicting migrations detected
This happens when two branches generated migrations at the same sequence number.The preferred fix is to roll back and recreate your migration. In complex cases, you can also run:If prompted for a default value, provide it in Python 3 syntax.
UnicodeDecodeError when running makemigrations
UnicodeDecodeError when running makemigrations
This can be caused by a
.DS_Store file added by macOS inside the migrations directory: