Technical Notes: Django migrations system
I read Real Python blogs about Django migrations
In this blog I'll refer to the most important ideas blogs mentioned
In end of the blog, I will put the links to these blogs
Section 1: Understanding SeparateDatabaseAndState:
SeparateDatabaseAndState is instantiated with two lists of operations:
1. State Operations:
contains operations that are only applied to the project state
2. Database Operations:
contains operations that are only applied to the database
This operation lets you do any kind of change to your database, but it’s your responsibility to make sure that the project state fits the database afterwards. Example use cases for SeparateDatabaseAndState
are moving a model from one app to another or creating and index on huge database without downtime.
It is an advanced operation and you won’t need on your first day working with migrations and maybe never at all.
Section 2: Database Schema:
description of all tables with their columns and their respective datatypes.
Section 3: Revert migrations:
If you want to revert the migrations 0002_auto_20181112_1950, you have to pass the 0001_initial as an argument to the migrate command.
in your terminal:
python manage.py migrate 0001_initial
result: the migration has been unapplied, meaning that the changes to the database have been reversed.
Section 4: Naming Migrations:
Section 5: Migrations Operations:
django.contrib.postgres.operations
that you can use to install various PostgreSQL extensions:BtreeGinExtension
BtreeGistExtension
CITextExtension
CryptoExtension
HStoreExtension
TrigramExtension
UnaccentExtension
Note that a migration containing one of these operations requires a database user with superuser privileges.
Last but not least, you can also create your own operation classes. If you want to look into that, then take a look at the Django documentation on creating custom migration operations.
Section 6: Sort How Migrations Will applied:
run_before
:Section 8: Advice:
Make it a habit to check the generated migrations and test them on a copy of your database before running them on the production data.
Data Migrations:
Migrations are mainly for keeping the data model of you database up-to-date, but a database is more than just a data model. Most notably, it’s also a large collection of data. So any discussion of database migrations wouldn’t be complete without also talking about data migrations.
1] creating an empty migration file and putting it in the right place if we type:
Comments
Post a Comment