I have been assigned the task to work on project involving react UI and django backend. There are two versions of this app:
- older version which runs on older versions of react and python (v2)
- newer version which runs on newer versions of react and python (v3)
I have been tasked to move some functionality from older version to newer version and test it with postgres database dump from a live environment running older version. The challenge could be differences in the database schema of newer and older versions. (But, most possibly there wont be much differences if some minor ones.)
So I proceeded to take the database dump, attached it to the database running on my laptop and specified its name in my django's settings.ini. But when I started my django app, it gave me error
You have 7 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, lab, otherlabs. Run 'python manage.py migrate' to apply them.
When I ran python manage.py migrate
, it gave me an error:
Migration
abc.0001_initial
is applied before its dependencyauth.0011_xyz
on database 'default'.
So, I deleted record corresponding to abc.0001_initial
from django_migrations
table and reran python manage.py migrate
. Now I got same error for migration of another project def
but with the same dependency auth.0011_xyz
:
Migration
def.0001_initial
is applied before its dependencyauth.0011_xyz
on database 'default'.
Should I proceed with deleting record corresponding to def.0001_initial
too? Am afraid that this will continue till I delete all such 0001_initial
-suffixed records. There are 35 projects and hence 35 such 0001_initial
-suffixed records.
Q1. Is it safe to delete them all and run the migrations? Is deleting all migrations and rerunning them really bad idea if they are schema-only migrations (we use fixtures for importing data)? Or is it simply impossible to do as most of those migrations will fail as tables corresponding to most models are already there in the database (or this wont give any error?), so the only solution remained is to delete migrations record one by one from db till I get no errors. But wont this still give error as I am only deleting migrations record and not actual tables created by migrations? Is there any other approach?
Q2. Are migrations completely safe to "re"-run? I mean if one fails because corresponding table / columns are already there or for some other reasons, will it leave database in consistent state or will it mess up the database?
Q3. Also will it continue to execute rest of the migrations after one fails? OR
Q4. Can / should I execute rest of the migrations if one fails? If yes, do I need to pass some arguments to python manage.py migrate
or its default behavior?
Update
I tried removing all migrations from the django_migrations
table. And then running python manage.py makemigrations
followed by python manage.py migrate
. But migration did not complete and terminated with error "relation already exist". So applied fake migration python manage.py migrate --fake
. It succeeded (seems that it faked / skipped all migrations as all corresponding tables were present, so it did not create new columns in already existing tables). But then my REST API calls started failing with error in response saying: "column xyz does not exist".
Manually copying data seems impossible since there are 156 tables
Please let me know if there are any solution to this.