Skip to content

What to do with missing migration files

Wessel Stoop edited this page Aug 28, 2015 · 1 revision

This page is intended for Wessel to document how to solve a complicated database problem, and for potential other developers that run into the same problem.

The goal

The Django models have been expanded, and the database needs to reflect this. This can normally be achieved with the instructions here: https://github.com/Woseseltops/signbank/wiki/Developer-guide#adjusting-models .

The problem

Because of some syncing problems between various workspaces, various migration files have been lost. In the hypothetical situation that all of the problems were caused in one week, you could summarize the problem as follows:

  • The migration files are still as they were Monday
  • The database structure is still as it was Tuesday
  • The Django models were changed on Wednesday

Normally, you would use Django South to update the database structure to match Wednesday's code, but this is prevented by the fact that the migration files are even further behind.

The solution

Build the migration files from scratch. Steps to achieve this:

  1. Back up everything.
  2. Go back to a commit where the models match the database from Tuesday. This way, the only discrepancy is between the the migration files and the database. This assumes the database is not under version control.
  3. Delete all migration files.
  4. python manage.py schemamigration <app-name> --initial This creates a new migration matching the current database models.
  5. python manage.py migrate <app-name> 0001 --fake --delete-ghost-migrations Officially installs your new migration. In reality, because the migration matches the current database structure exactly, it will only throw away information on past migrations. The resulting database will think it was created from that one initial 0001 migration.
  6. Save the migration file somewhere else.
  7. git checkout master to go back to Wednesday's code, and replace the previous migration files with the newly created migration file. The migration files and database structure are now both on Tuesday, the code is on Wednesday.
  8. The regular procedure to update a database.