django migrations

정은경·2021년 2월 24일
0

🎸 Play the Django 

목록 보기
54/57

장고 디렉터리 구조

(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  tree
.
├── bitcoin_tracker
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── settings.cpython-38.pyc
│   │   └── urls.cpython-38.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── historical_data
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── admin.cpython-38.pyc
│   │   └── models.cpython-38.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-38.pyc
│   │       └── __init__.cpython-38.pyc
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── manage.py

6 directories, 22 files
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 

historical_data/models.py

(realpython)  ✘ jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  cat historical_data/models.py
from django.db import models

class PriceHistory(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    volume = models.PositiveIntegerField()

(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 
(realpython)  ✘ jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py dbshell
.SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.
sqlite> .tables
auth_group                    django_admin_log
auth_group_permissions        django_content_type
auth_permission               django_migrations
auth_user                     django_session
auth_user_groups              historical_data_pricehistory
auth_user_user_permissions
sqlite>
sqlite> .schema --indent historical_data_pricehistory
CREATE TABLE IF NOT EXISTS "historical_data_pricehistory"(
  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  "date" datetime NOT NULL,
  "price" decimal NOT NULL,
  "volume" integer unsigned NOT NULL
);
sqlite>
sqlite> .quit
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 

show manage.py showmigrations

(realpython)  ✘ jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py showmigrations
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
historical_data
 [X] 0001_initial
 [X] 0002_auto_20210224_0307
sessions
 [X] 0001_initial
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 

특정 migration으로 롤백

python manage.py migrate "migration 파일 이름"

(realpython)  ✘ jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py migrate historical_data 0001_initial
Operations to perform:
  Target specific migration: 0001_initial, from historical_data
Running migrations:
  Rendering model states... DONE
  Unapplying historical_data.0002_auto_20210224_0307... OK
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  cat historical_data/models.py
from django.db import models

class PriceHistory(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    volume = models.DecimalField(max_digits=7, decimal_places=3)

(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py dbshell
SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.
sqlite> .schema --indent historical_data_pricehistory
CREATE TABLE IF NOT EXISTS "historical_data_pricehistory"(
  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  "volume" integer unsigned NOT NULL,
  "date" datetime NOT NULL,
  "price" decimal NOT NULL
);
sqlite>

migrations 파일 내맘대로 네이밍

python manage.py makemigrations "앱이름" --name "나만의 마이그레이션 파일명"

(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  rm historical_data/migrations/0002_auto_20210224_0307.py
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py makemigrations historical_data --name switch_to_decimal
Migrations for 'historical_data':
  historical_data/migrations/0002_switch_to_decimal.py
    - Alter field volume on pricehistory
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  tree
.
├── bitcoin_tracker
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── settings.cpython-38.pyc
│   │   └── urls.cpython-38.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── historical_data
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── admin.cpython-38.pyc
│   │   └── models.cpython-38.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_switch_to_decimal.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-38.pyc
│   │       ├── 0002_auto_20210224_0307.cpython-38.pyc
│   │       └── __init__.cpython-38.pyc
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── manage.py

6 directories, 24 files
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py dbshell
SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.
sqlite> .schema --indent historical_data_pricehistory
CREATE TABLE IF NOT EXISTS "historical_data_pricehistory"(
  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  "volume" integer unsigned NOT NULL,
  "date" datetime NOT NULL,
  "price" decimal NOT NULL
);
sqlite> .quit
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  cat historical_data/models.py
from django.db import models

class PriceHistory(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    volume = models.DecimalField(max_digits=7, decimal_places=3)

(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, historical_data, sessions
Running migrations:
  Applying historical_data.0002_switch_to_decimal... OK
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker 
(realpython)  jek@DESKTOP-L78P7IM  ~/dev_marie/bitcoin_tracker  python manage.py dbshell
SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.
sqlite> .schema --indent historical_data_pricehistory
CREATE TABLE IF NOT EXISTS "historical_data_pricehistory"(
  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  "date" datetime NOT NULL,
  "price" decimal NOT NULL,
  "volume" decimal NOT NULL
);
sqlite>

Reference

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글