﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26760	Delete nonexistent migrations from django_migrations table	Jarek Glowacki	Jacob Walls	"Django adds a `django_migrations` table to the database which list all of the migrations that have been applied (and when).

With the introduction of `squashmigrations`, it is possible for this table to contain a lot of old migrations that no longer exist. This can be problematic if naming duplication occurs:

Example:
I have an app with:
{{{
my_app/migrations/
0001_initial.py
0002_blah.py
0003_blah.py
}}}
I squash and delete replaced migrations:
{{{
my_app/migrations/
0001_initial_squashed_0003_blah.py
}}}
I create a new migration and use poor naming:
{{{
my_app/migrations/
0001_initial_squashed_0003_blah.py
0002_blah.py
}}}
My new migration never runs because the `django_migrations` table thinks it has already been applied.

I propose truncation of the `django_migrations` table so that it includes only migrations that actually exist in the django project. This could be done automatically (when executor runs, or inside the `migrate` mcommand). Or have its own mcommand that requires it be run manually. I prefer the automatic approach though.

Pros:
- Cleans up old data that just bloats the database.
- Protects users from the trap mentioned above where a new migration is created with the same name as one that was applied in the past.
Cons:
- A loss of historical information.


Note:
Need to be careful with implementation to avoid a possible new trap if a user squashes migrations and then proceeds to delete the replaced migrations before running the squashed migrations on their database -> django will think squashed migrations haven't been applied and will attempt to reapply them. This can be remedied simply by not removing migrations mentioned in `replaces` lists of other migrations from `django_migrations` (ie. we'd consider replaced migrations as still existing, even if their actual files have already been removed)."	Cleanup/optimization	closed	Migrations	dev	Normal	fixed	django_migrations squash migrations	Markus Holtermann dev@…	Ready for checkin	1	0	0	0	0	0
