Test the application of migration files with Django + PostgreSQL (Evil Way)

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.

Purpose

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.

Premise

--The command described in the procedure must be installed and executable. --The database name of the operation target is targetdb.

Procedure outline

--Prepare the backup destination database for the target database --Disconnect existing session --Back up the target database --Apply migration --Undo from backup

procedure

Prepare the backup destination database for the target database

Preparation of save destination database


$ createdb targetdb_evate;

Disconnect existing session

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;

Back up the target database

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 migration

Apply Django migrations in any way you like.

Apply migration


$ python manage.py migrate

After application, check the operation and execute various tests.

Undo from backup

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

Test the application of migration files with Django + PostgreSQL (Evil Way)
The easiest way to get started with Django
The story of viewing media files in Django
The story of making a web application that records extensive reading with Django
Upload files with Django
Extract the table of image files with OneDrive & Python
Until the start of the django tutorial with pycharm on Windows
Test the version of the argparse module
Test Driven Development with Django Part 4
Test Driven Development with Django Part 6
Measure Django application coverage with Coverage.py
Handle csv files with Django (django-import-export)
Manage Django config files with Python-decouple
Deploy a Django application with Docker
Test Driven Development with Django Part 2
Test the goodness of fit of the distribution
Build a web application with Django
Test Driven Development with Django Part 1
The story of remounting the application server
Test Driven Development with Django Part 5
Application of graphs with plotly sliders
Setting to debug test by entering the contents of the library with pytest
Domain registration and HTTPS conversion of Django application created with CodeStar with Route53
The story of using mysqlclient because PyMySQL cannot be used with Django 2.2