Skillversitas

Migrations

The migration is the schema the no the SQL: the makemigrations the diff (the model the change the 1 file), the migrate the SQL the DB (the table, the column, the index), and the 12factor is the history (the applied the record, the revert the 1 the no the data). The sqlmigrate is the read (the no the run, the debug), the showmigrations is the state, and the data migration is the row (the no the table, the transform). This chapter covers the makemigrations (the diff, the no the DB), the migrate (the SQL, the apply), the sqlmigrate the showmigrations (the read, the state), and the data migration (the row, the transform).

The makemigrations and the migrate

# the MAKEMIGRATIONS (the diff, the no DB):
python manage.py makemigrations
#   the Migrations: None
#   the Changes:
#     the notes_app
#       the Create model Note
#   the 1 file: the migrations/0001_initial.py
#     the operations = [
#       the migrations.CreateModel(
#         name="Note",
#         fields=[
#           the ("title", models.CharField(
#             max_length=200)),
#           the ("body", models.TextField()),
#           the ("created", models.DateTimeField(
#             auto_now_add=True)),
#         ]),
#     ]
#   the no the DB (the no the touch,
#     the review the git)

# the MIGRATE (the SQL, the apply):
python manage.py migrate
#   the Operations to apply:
#     the 0001_initial
#   the Applying notes_app.0001_initial...
#   the OK (the table the create,
#     the column, the index)
#   the the state: the
#     django_migrations the record
#     (the applied, the no the
#     re-apply)

The makemigrations is the diff: the model the change (the field the add, the delete, the type the change), the 1 file (the 0001_initial.py, the operations the list), and the no the DB (the touch the no, the review the git the yes) - the migrate is the apply: the SQL the DB (the CREATE TABLE, the ALTER TABLE, the INDEX), and the django_migrations is the record (the applied, the no the re-apply) - the 12factor is the history (the 0001 the 0002 the 0003 the chain, the no the re-write, the new the append) - the sqlmigrate is the next: the read the no the run, the debug the SQL the inspect.

The sqlmigrate and the data migration

# the SQLMIGRATE (the read, the no run):
python manage.py sqlmigrate \
    notes_app 0001
#   the BEGIN;
#   the CREATE TABLE "notes_app_note" (
#     the "id" bigint NOT NULL PRIMARY
#       KEY GENERATED BY DEFAULT AS
#       IDENTITY,
#     the "title" varchar(200) NOT NULL,
#     the "body" text NOT NULL,
#     the "created" timestamp with time
#       zone NOT NULL
#   );
#   the COMMIT;
#   the no the DB (the print only,
#     the debug, the CI's)

# the SHOWMIGRATIONS (the state):
python manage.py showmigrations
#   the admin [X] (the applied)
#   the auth [X]
#   the notes_app [X] 0001_initial
#   the contenttypes [X]
#   the [ ] the no (the pending)

# the DATA MIGRATION (the row, the
#   transform):
#   the the 1: the makemigrations the
#     the data the flag
python manage.py makemigrations \
    notes_app --empty
#   the 0002_fill_slugs.py (the run):
# from django.db import migrations

# def fill(apps, schema_editor):
#     Note = apps.get_model(
#         "notes_app", "Note")
#     for n in Note.objects.all():
#         n.slug = slugify(n.title)
#         n.save()

# class Migration(migrations.Migration):
#     dependencies = [("notes_app",
#         "0001_initial")]
#     operations = [
#         migrations.RunPython(fill)]
#   the 2: the migrate (the run the
#     the 1 time, the no the re-run)

The sqlmigrate is the read: the notes_app 0001 the SQL the print (the CREATE TABLE, the column, the no the run), and the debug is the inspect (the index the no the auto, the constraint the check) - the showmigrations is the state (the [X] the applied, the [ ] the pending, the CI the check the no the pending). The data migration is the row: the RunPython the 1 function (the apps.get_model the historical, the no the current), the fill the slug (the loop, the save), and the migrate is the run (the 1 time, the no the re-run, the state the record) - the no the data the schema (the transform the row, the migration the 2 the type), and the rule is the one: the makemigrations the diff the no the DB, the migrate the SQL the apply, the sqlmigrate the read, the data the RunPython - the next chapter's query is the QuerySet the full the filter the aggregate the N+1 the fix.

The Working Rules

  • The makemigrations is the diff, the no DB. The model the change (the field, the delete, the type), the 1 file (the 0001_initial, the operations), and the review the git (the no the apply, the inspect the operations) - the --empty the data, the --name the custom (the 0002_fill_slugs, the no the auto).
  • The migrate is the SQL, the django_migrations the record. The CREATE TABLE the ALTER TABLE the INDEX (the apply), and the applied the no the re-apply (the state, the CI's check) - the --plan the dry-run (the no the apply, the read), and the --verbosity the 3 (the SQL the print, the debug).
  • The sqlmigrate is the read, the showmigrations the state. The sqlmigrate notes_app 0001 the SQL (the no the run, the index the check), and the showmigrations the [X] the applied (the [ ] the pending, the CI's no the pending) - the migrate the 0001 the specific, and the fake the no the apply (the state the only, the manual).
  • The data migration is the RunPython, the 1 time. The apps.get_model the historical (the no the current, the forward the safe), the fill the loop (the save, the no the bulk), and the migrate the 1 time (the no the re-run, the state the record) - the no the data the schema, the transform the row, and the bulk_create the fast (the 10K the 1x the no the 100x).

Summary

The migration is the schema the no the SQL: the makemigrations the diff (the model the change, the 1 file the operations, the no the DB, the review the git), the migrate the SQL the apply (the CREATE TABLE, the ALTER TABLE, the INDEX, the django_migrations the record, the no the re-apply), the sqlmigrate the read (the SQL the print, the no the run, the debug), and the showmigrations the state (the [X] the applied, the [ ] the pending, the CI's check). The data migration is the RunPython (the apps.get_model the historical, the fill the loop, the migrate the 1 time) - the 12factor is the history (the chain, the append, the no the re-write). The next chapter's query is the QuerySet the full: the filter the chain the 1 SQL, the aggregate the COUNT the SUM, the select_related the prefetch_related the N+1 the fix, and the annotation the compute the SQL the no the Python.