Operation memorandum with Django + PostgreSQL for myself It's not the right way to do it, and it should be done in a straightforward manner. Described that there is also such a method.
The correctness of the procedure is not guaranteed.
If you change the model in Django In the verification environment shared by everyone Respond after applying the migration file.
However, it is a corresponding procedure for those who are troublesome to write additional code.
--The command described in the procedure must be installed and executable. --The database name of the operation target is targetdb.
--Prepare the backup destination database for the target database --Disconnect existing session --Back up the target database --Apply migration --Undo from backup
Preparation of save destination database
$ createdb targetdb_evate;
Disconnect the session connected to PostgreSQL. (Of course, after confirming that no operations have been performed)
Check existing sessions
$ psql
postgres=# select pid, datname from pg_stat_activity;
Specify the pid confirmed by the above command and throw the following SQL.
Disconnect session
postgres=# select pg_terminate_backend($pid) from pg_stat_activity;
From the target database (targetdb) to the save destination database (targetdb_evade) Back up your data to.
Database backup
$ pg_dump -Ft targetdb | pg_restore -d targetdb_evade
If there are any sessions left, it will fail here.
Apply Django migrations in any way you like.
Apply migration
$ python manage.py migrate
After application, check the operation and execute various tests.
Return to the state before migration from the save destination database ** Disconnect existing session ** Repeat steps to repeat Disconnect all sessions.
Delete the database (targetdb) to which migration has been applied, and Rename the saved database (targetdb_evade) and you're done.
Return from backup
$ psql
postgres=# drop database targetdb;
postgres=# alter database targetdb_evade rename to targetdb;
Recommended Posts