Golden Codes - armanexplorer planet

Practical code snippets for Django, Python, Bash, Git and All!

View on GitHub

squashmigrations

Docs

# created combined migration
python manage.py squashmigrations myapp 0001 0005

# apply (commit) migration
python manage.py migrate

# After successfully applying the squashed migration, you can optionally remove the old migration files that were squashed.
# WARNING: DO THIS ONLY IF ALL USERS HAVE BEEN COMMITTED THIS
# python manage.py migrate --prune <app_name>

NOTE: Maybe it is better to create new migration and then delete the old ones, because it leads to updating the migrations table and you can see it by running showmigrations <app_name>

Once you’ve squashed your migration, you should then commit it alongside the migrations it replaces and distribute this change to all running instances of your application, making sure that they run migrate to store the change in their database.

You must then transition the squashed migration to a normal migration by:

NOTE: Once you’ve squashed a migration, you should not then re-squash that squashed migration until you have fully transitioned it to a normal migration.

NOTE: If it is likely that you may reuse the name of a deleted migration in the future, you should remove references to it from Django’s migrations table with the migrate --prune option.

data migrations

Link

revert migration

Practical Doc

Full Doc

You can reverse a migration using the migrate command with the number of the previous migration.

Let’s say you have an app called recipes.

You can see a list of all the migrations in the migrations folder or by running the following command:

python manage.py showmigrations recipes

You may have the following migrations in your recipes app:

If you want to undo the 0003_recipeindex migration, you can do it like so:

python manage.py migrate recipes 0002_recipepage

# or shorter
python manage.py migrate recipes 0002

Make sure that everything is working the way it should, then you can delete 0003_recipeindex.py.

If you want to reverse all migrations for your app, you can use the name zero, like so:

python manage.py migrate recipes zero

Keep in mind that certain irreversible operations can make a migration irreversible. If you try to reverse an irreversible migration, you will get an IrreversibleError.