Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset south migrations to capture the current state of my django models

I have an app that currently has 35 south migrations. These take a while to go through when setting up a new deployment (we create new deployments often), and the app is continually evolving--adding more migrations. Additionally, the migrations include some potentially complex data migrations and a few custom migrations that break SQLite3 (not a huge problem right now since everything is on Postgres, but its nice to be able to set up a quick test environment), and generally just more things that can go wrong.

All of our deployments and developers are up to date, and I'd like to clear out all of the app's migrations and create a single initial migration (0001) that captures the current state of the app, and then go forward with new migrations from there. I did this a few years ago with a different app, and it worked out nicely, but I've since forgotten what the process was and lost track of the blog post that explained how to do it. Can anyone break down this process for me?

like image 229
B Robster Avatar asked Oct 10 '12 15:10

B Robster


People also ask

How do I reset Django migrations?

Django's migration can be reset by cleaning all the migration files except __init__.py files under each project app directory, followed by dropping the database and creating migration again using python manage.py makemigrations and python manage.py migrate .

What is South migration in Django?

South is a migration tool used with Django. There will be times when you would be adding fields to a model or changing the type of field (eg: Field was an IntegerField and you want to change it to FloatField). In such cases syncdb doesn't help and South comes to your rescue.


1 Answers

I figured this out (wasn't too bad). To set up the migration reset, I do the following:

rm <app-dir>/migrations/*
python manage.py schemamigration <app-name> --initial
python manage.py migrate <app-name> 0001 --fake  --delete-ghost-migrations

I commit the changes to the repository, and then for each deployment of the code elsewhere, run:

python manage.py migrate <app-name> 0001 --fake --delete-ghost-migrations

Make sure you don't add anything new between the time you last migrated everywhere else and you reset things or the new 0001 migration won't match up with the schema!

Caveats: See guettli's comment (and my responses)

like image 195
B Robster Avatar answered Oct 21 '22 18:10

B Robster