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:

    You first have to remove the old migration. You have already unapplied it, so you can safely delete the file:
    
    Now you can recreate it with a more descriptive name:
 

    
This will create the same migration as before, except with the new name of 0002_switch_to_decimals.

Section 5: Migrations Operations:

    Since Django 2.0, there are also a couple of PostgreSQL-specific operations available in 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: 

    You can also enforce that a migration is run before another migration using the attribute run_before:

Section 7: Viewing the Migration:

    You generally don’t have to worry about the SQL that migrations generate. But if you want to double-check that the generated SQL makes sense or are just curious what it looks like, then Django’s got you covered with the sqlmigrate management command:


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:






Links:


- video about data migrations: Data Migrations with Django

Comments

Popular posts from this blog

SVU-CPC مشاركتي الأولى في مسابقة البرمجة التنافسية