3

What is the procedure to restore a Django project using an already restored database from a PostgreSQL pg_dump. All django source code also exist. Will Django migration safe?

cw12345
  • 383
  • 1
  • 2
  • 9
  • Does your Pg dump contains create table statements and if not then super easy. – Arpit Solanki Jul 08 '17 at 21:25
  • yes pg_dump has create table statements. – cw12345 Jul 08 '17 at 21:30
  • If you migrate and then restore Pg dump then it will create new tables so migrations will get overwritten. If its possible you should remove create table statements, Django migrations already creates tables from you. – Arpit Solanki Jul 08 '17 at 21:32
  • 1
    Do you have django internal tables in your dump `django_*`? If yes then you don't need to run any migrations, just load the whole db and django will think it is fully migrated (migration history is stored in db) – serg Jul 08 '17 at 22:46

1 Answers1

1

If your dump has create table statements and contains all django tables, you can restore it directly onto an empty database. Django will know the status of the migrations as they are stored in a table in the DB.

So the steps would be:

  1. Drop and recreate DB.

    If you now run python manage.py showmigrations all migrations will appear unapplied

  2. Restore DB from dump

    If you now run python manage.py showmigrations now, the corresponding migrations will appear applied. If your django project has new migrations that weren't applied when the dump was created they will appear unapplied.

And that's it! Now you can apply the new migrations if there are any and keep working on the Django project.

Enric Calabuig
  • 470
  • 6
  • 12