7

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.

geckon
  • 8,316
  • 4
  • 35
  • 59
mehmet
  • 7,720
  • 5
  • 42
  • 48

3 Answers3

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.

knbk
  • 52,111
  • 9
  • 124
  • 122
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).
rollingthedice
  • 1,095
  • 8
  • 17
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
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

Rabbott
  • 4,282
  • 1
  • 30
  • 53
  • I should have mentioned I used Django 1.8, so this is not a south migration. Thanks for the answer though. – mehmet Jun 09 '15 at 16:25