15. Migrations - chohankyun/python-django-study GitHub Wiki

##Migrations
λͺ¨λΈμ˜ 변경을 λ°μ΄ν„°λ² μ΄μŠ€μ— 적용 ν•΄μ£ΌλŠ” 방법
DB μŠ€ν‚€λ§ˆ 버전 관리 μ‹œμŠ€ν…œ
app/migrations 폴더 및에 migration file 생성 및 DB 에 적용
MIGRATION_MODULES μ„€μ •μœΌλ‘œ 폴더λ₯Ό λ³€κ²½ ν•  수 있음

###λͺ…λ Ήμ–΄

  • migrate : migration file 의 λ‚΄μš©μ„ DB 에 적용 μ‹œν‚¨λ‹€.
  • makemigrations : λͺ¨λΈμ„ λ°”νƒ•μœΌλ‘œ migration file 을 λ§Œλ“ λ‹€.
  • sqlmigrate : migration file 을 μ½μ–΄μ„œ SQL 문으둜 보여쀀닀.
  • showmigrations : DB 에 적용된 migrations file 을 보여쀀닀.

###μž‘μ—… 흐름

  • model 생성, μˆ˜μ •, μ‚­μ œ
  • makemigrations λͺ…λ Ήμ–΄ μ‹€ν–‰

makemigrations

$ python manage.py makemigrations
Migrations for 'books':
  books/migrations/0003_auto.py:
  - Alter field author on book
  • ν˜„μž¬μ˜ migrations file 비ꡐ
  • μ—…λ°μ΄νŠΈ 된 migrations file 생성
  • migrate λͺ…λ Ήμ–΄ μ‹€ν–‰

migrate

$ python manage.py migrate
Operations to perform:
  Apply all migrations: books
Running migrations:
  Rendering model states... DONE
  Applying books.0003_auto... OK
  • database 에 적용

####νŠΉμ • model 만 적용
makemigrations --name μ‚¬μš©

$ python manage.py makemigrations --name changed_my_model your_app_label

###버전 관리 migration 버전 관리 정보가 μ €μž₯λœλ‹€.

  • migration file 의 λ²ˆν˜ΈλŠ” 개발자 참쑰용
  • migration file 의 파일λͺ…에 의쑴

migration file 이 같은 app 에 적용 λ λ•Œ

  • λ¬Έμ œκ°€ μ—†λ‹€κ³  μƒκ°λ˜λ©΄ κ·Έλƒ₯ μ‹€ν–‰ν•˜λ©΄ μžλ™μœΌλ‘œ μˆœμ„œλ˜λ‘œ 적용
  • λ¬Έμ œκ°€ μžˆλ‹€κ³  μƒκ°λ˜λ©΄ μˆ˜μ •ν•΄μ„œ 적용

###μ˜μ‘΄μ„±

  • migration file μ•ˆμ— μžˆλŠ” dependencies 에 μžˆλŠ”κ²ƒμ΄ 적용된 이후 적용

###migration file
migratios 폴더에 μ €μž₯

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [("migrations", "0001_initial")]

    operations = [
        migrations.DeleteModel("Tribble"),
        migrations.AddField("Author", "rating", models.IntegerField(default=0)),
    ]

django.db.migrations.Migration

  • Migration μ‹€ν–‰
  • μ›λž˜λŠ” 4개의 속성 쑴재
  • λŒ€λΆ€λΆ„ 2개의 μ†μ„±λ§Œ μ‚¬μš©
  • dependencies : μ˜μ‘΄μ„± 리슀트
  • operations : migration λŒ€μƒ μ„ μ–Έ, sql μƒμ„±μ‹œ μ°Έμ‘°

migration file 을 μˆ˜λ™μœΌλ‘œ 생성 ν•  수 μžˆλ‹€,
ν•˜μ§€λ§Œ κ°€λŠ₯ν•˜λ©΄ μˆ˜λ™μœΌλ‘œ 처리 ν•˜μ§€ 말것을 ꢌμž₯

###Model managers
RunPython λͺ…령을 μˆ˜ν–‰ ν•˜κΈ° μœ„ν•΄μ„œ Manager 객체λ₯Ό μ§λ ¬ν™”ν•΄μ„œ migration 에 포함 μ‹œν‚¨λ‹€.
Manage 객체λ₯Ό 직렬화 ν•˜λ €λ©΄ use_in_migrations = True λ₯Ό μ„€μ • ν•œλ‹€.
(μ–Έμ œ μ“°λŠ”μ§€ λͺ¨λ₯΄κ² λ‹€?)

class MyManager(models.Manager):
    use_in_migrations = True

class MyModel(models.Model):
    objects = MyManager()

###Inital migrrations
DB의 ν…Œμ΄λΈ” 생성을 μœ„ν•œ 졜초 migration νŒŒμΌμ„ λ§Œλ“œλŠ” 것
졜초 migrrations file

initial = True

DB ν…Œμ΄λΈ”μ΄ λ§Œλ“€μ–΄μ§„ μƒνƒœμ—μ„œ 졜초의 migration 적용 ν•˜λŠ” 법

 migrate --fake-initial 

###νŠΉμ • App μ—λ§Œ migration 적용

$ python manage.py makemigrations your_app_label
  • model 이 λ³€κ²½λ˜μ§€ μ•Šμ€ μƒμ—μ„œ migration 을 적용 ν•˜λ €λ©΄, intial migration 을 λ¨Όμ „ μˆ˜ν–‰ ν•œλ‹€.
  • μˆ˜λ™μœΌλ‘œ DB λ₯Ό λ³€κ²½ν•˜μ§€ 마라

###model field 주의

  • system_check_deprecated_details
  • system_check_removed_details

###Data Migrations

  • RunPython 을 μ‚¬μš©ν•΄μ„œ μ–΄λ ΅μ§€ μ•Šκ²Œ data migration

####μž‘μ—… 흐름
####λΉ„μ–΄μžˆλŠ” migration file 생성

python manage.py makemigrations --empty yourappname  

####migration file μ—΄κΈ°

# -*- coding: utf-8 -*-
# Generated by Django A.B on YYYY-MM-DD HH:MM
from __future__ import unicode_literals  

from django.db import migrations, models

class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ('yourappname', '0001_initial'),
   ]

    operations = [
   ]

####RunPython 을 μ‚¬μš©ν•œ Data Migration
RunPython μ—μ„œ 호좜 ν•  function μž‘μ„± function μ•„κ·œλ¨ΌνŠΈ

  • apps : λͺ¨λ“  app
  • schema_editor : DB μˆ˜μ • μŠ€ν‚€λ§ˆ

λ‹€μŒμ€ first_name κ³Ό last_name 을 ν•©ν•˜λŠ” 것

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

def combine_names(apps, schema_editor):
    # We can't import the Person model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    Person = apps.get_model("yourappname", "Person")
    for person in Person.objects.all():
        person.name = "%s %s" % (person.first_name, person.last_name)
        person.save()

class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(combine_names),
    ]

####python manage.py migrate μ‹€ν–‰