Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset migrations in Django 1.7

(I know there is a title the same as this, but the question is different).

I have managed to get my development machine migrations and production migrations out of sync.

I have a Django app which was using South. I had my own workflow that worked fine (it probably wasn't the correct way to do things, but I had no problems with it).

Basically I have a script that copies the production database dump to my development machine. It also copied the migration files. That way the two were in synch, and I could run South commands as normal.

Now I have upgraded to 1.7, and started using migrations. When I use my previous workflow (copy database dump, and migration files from production), it is not detecting changes on my development machine.

I have read through the migrations document, and I see that the correct way to use it is to

  1. run "make migrations" and "migrate" on my development machine.
  2. run "migrate" on my devlopemnt machine to actually make the database changes
  3. Copy changes over, including migration files.
  4. run "migrate" on the production machine. (without the "makemigrations" step)

Anyway. It is all a mess now. I would like to "reset" my migrations and start from scratch, doing things properly from now on.

What do I need to do?

  1. Delete the contents of the migration table (on both machines)?
  2. Delete the contents of the migration folder? (Including the init.py file).
  3. Start the migrations as per the documentation for a new one.

Have I missed anything? Is there a reason why copying everything from production(database and migration files) doesn't detect any changes on my development machine afterwards

like image 647
wobbily_col Avatar asked Mar 25 '15 10:03

wobbily_col


2 Answers

I would just do the following on both the environments (as long as the code is the same)

  1. Delete your migrations folder
  2. DELETE FROM django_migrations WHERE app = <your app name> . You could alternatively just truncate this table.
  3. python manage.py makemigrations
  4. python manage.py migrate --fake

After this all your changes should get detected across environments.

like image 198
harshil Avatar answered Oct 13 '22 01:10

harshil


Run

python manage.py migrate your_app zero

This will drop all tables from your_app

If you want, since you said you want to start over, you can delete your migrations folder, or maybe rename the folder, create a new migrations folder and run

python manage.py makemigrations your_app
python manage.py migrate your_app

Just like south, you can always go back and forth...

# Go to the first migration
python manage.py migrate your_app 0001

# Go to the third migration
python manage.py migrate your_app 0003

So imagine that your 4th migration is a mess... you can always migrate to the 3rd, remove the 4th migration file and do it again.

Note:

This one of the reasons your models should be in different apps. Say you have 2 models : User and Note. It's a good practice to create 2 apps: users and notes so the migrations are independent from each other.

Try not use a single application for all your models

like image 94
brunofitas Avatar answered Oct 13 '22 03:10

brunofitas