Migrations should not be changed if they have already been merged in main branch ;)
Don't change migration files by hands If you don't really understand how the kmigrator works!
Drop database and reapply all migrations:
docker-compose down &&
docker-compose up -d postgresdb redis &&
docker-compose run app yarn workspace @app/condo makemigrations &&
docker-compose run app yarn workspace @app/condo migrate
To reflect changes, made on Keystone schemas, with database schema, create migration file, that will be added into apps/condo/migrations:
docker-compose run app yarn workspace @app/condo makemigrations
Migrate current database to new schema, this will run all migrations, that was not applied:
docker-compose run app yarn workspace @app/condo migrate
Rollback last applied migration:
docker-compose run app yarn workspace @app/condo kmigrator down
When some migration is removing a constraint, it may not be reversible, because the constraint may be violated by records, inserted after migration was applied.
For example, we have a migration, that is removing a constraint to require a value for a field. After applying the migration, records without value for the field may be inserted. When the migration is reversed ("down"), it may not be able to bring the constraint back because some rows may violate it.
Suppose, You changed structure of a schema, you are working with. You need changes to be reflected in a corresponding migration file. Don't change migration file by hands. Instead, recreate it completely.
- Roll it back:
docker-compose run app yarn workspace @app/condo kmigrator down
-
Delete the migration file
-
Recreate migrations:
docker-compose run app yarn workspace @app/condo makemigrations
If Your work results to several migration files, that You need to merge into one, don't do it by hands. It's not enough just to merge SQL-contents of migration files, because a special system comment "KMIGRATOR…" will not be conformed with the file anymore.
To merge several sequential migration files into one:
- Roll them back, one by one using
# roll one step back
docker-compose run app yarn workspace @app/condo kmigrator down
# roll one more step back
docker-compose run app yarn workspace @app/condo kmigrator down
# etc.
- Delete files
- Generate new migration file using:
docker-compose run app yarn workspace @app/condo makemigrations
If You are ready to ship Your work, make a rebase and look at migrations, created by others. Maybe, your migration number is not ahead of others anymore.
- Roll back your migration
docker-compose run app yarn workspace @app/condo kmigrator down
-
Delete your migration file
-
Generate new migration
docker-compose run app yarn workspace @app/condo makemigrations
It's better to roll back your migration and recreate it. But sometimes you should do:
docker-compose run app python apps/condo/.kmigrator/manage.py makemigrations --merge
If the util asks You to provide a default value, it expects it in Python 3 syntax.
By running makemigrations util, it can crash with following error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte
The reason can be a .DS_Store file, added by macOS ;)
➜ condo git:(main) ls -la apps/condo/migrations
total 752
drwxr-xr-x 31 antonal staff 992 16 июл 10:15 .
drwxr-xr-x 29 antonal staff 928 16 июл 10:15 ..
-rw-r--r--@ 1 antonal staff 6148 15 июл 13:22 .DS_StoreRemove it:
rm apps/condo/migrations .DS_Store