While developing a Python application with SQL
Squashing Some Migrations🔗
If you only want to squash some of your migration history, but not all of it, follow the instructions outlined in this StackOverflow answer.
Squashing All Migrations🔗
This may be obvious already, but I'll share hoping it saves someone a few minutes of hassle. If you want to squash all of your migrations into one "initial migration", do the following. Your migrations will be re-created as a single script, based on the current state of your models.
Note: These steps may need to be adapted depending on your project's structure. I am using Alembic via Flask-Migrate.
- Truncate your alembic_version table.
sql> TRUNCATE TABLE alembic_version;
- Delete the migrations directory.
rm -rf migrations
- Re-create the migrations directory from scratch.
python manage.py db init
- Create your database schema (from your model definitions) as a single migration.
python manage.py db migrate
- Apply any pending migrations (if your database isn't already up-to-date).
python manage.py db upgrade