How to delete django migrations after squashing them?
Asked Answered
E

4

6

Django documentation says we could delete migrations after squashing them:

You should commit this migration but leave the old ones in place; the new migration will be used for new installs. Once you are sure all instances of the code base have applied the migrations you squashed, you can delete them.

Here, does deleting means deleting only the migration files, or the entries in the django_migrations table as well?

Here is some background: I have only the development machine, so just one code base. After squashing some of the migrations that I had already applied, I deleted the files and the database entries. Tested if this is OK by making migrations, it did not find anything. So, everything looked good. Next day, I had to change something, and made migration. When I tried to migrate, it tried to apply the squashed migration too (which was applied part by part before being squashed). So, I had to go back and recreate the entries in the django_migrations table. So, it seems like I had to keep the database entries. I am trying to make sure before I mess up anything again, and understand why it looked fine first, and then tried to apply the squashed migration.

Expose answered 4/6, 2015 at 0:22 Comment(0)
R
9

Squashed migrations are never marked as applied, which will be fixed in 1.8.3 (see #24628).

The steps to remove the old migrations are:

  1. Make sure all replaced migrations are applied (or none of them).
  2. Remove the old migration files, remove the replaces attribute from the squashed migrations.
  3. (Workaround) Run ./manage.py migrate <app_label> <squashed_migration> --fake.

The last step won't be necessary when 1.8.3 arrives.

Risotto answered 4/6, 2015 at 12:14 Comment(0)
S
2

Converting squashed migrations has gotten easier since the question was posted. I posted a small sample project that shows how to squash migrations with circular dependencies, and it also shows how to convert the squashed migration into a regular migration after all the installations have migrated past the squash point.

As the Django documentation says:

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

  • Deleting all the migration files it replaces.
  • Updating all migrations that depend on the deleted migrations to depend on the squashed migration instead.
  • Removing the replaces attribute in the Migration class of the squashed migration (this is how Django tells that it is a squashed migration).
Sprung answered 8/6, 2016 at 20:47 Comment(0)
C
0

I'm no expert by any means, but I just squashed my migrations, and ended up doing the following:

Ran this query to removed the old migrations (squashed)

DELETE FROM south_migrationhistory;

Run this management command to remove the ghosted migrations

./manage.py migrate --fake --delete-ghost-migrations 

Django 1.7 also has squashmigrations

Clemmy answered 4/6, 2015 at 2:42 Comment(1)
I should have mentioned I used Django 1.8, so this is not a south migration. Thanks for the answer though.Expose
D
0

Django extensions have a command for this https://django-extensions.readthedocs.io/en/latest/delete_squashed_migrations.html

Apparently answers need to be longer...

So you need to install django extensions in the normal way, which is generally a good idea anyhow and then per the linked doco the command is "manage.py delete_squashed_migrations".

FWIW the implementation is a bit shaky, it makes assumption about the whitespace in the migrations and those assumptions have been broken by the recent adoption of automatic black formatting of migration files. Running black with a very long line length first helps.

Degradation answered 10/5 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.