python에서 alembic활용하여 migration하기

최진영·2022년 1월 18일
0

1. 용어

migration : 데이터베이스에서 다른종류의 데이터베이스로 옮기는 형태를 말합니다. 영어 단어 그대로의 뜻은 '이주'로 데이터베이스 뿐만 아니라 운영체제 간 이동하는 것도 포함하고 있습니다.



2. 필요성

데이터베이스에서는 데이터를 수정할 때 큰 위험이 따릅니다. 그래서 update, delete, insert를 했을 때 back up의 필요성을 느껴 특정 시점으로 돌아가능 기능이 있는데 python에서는 alembic을 활용하여 migration합니다.

시작하기

alembic init <migration dir name>

위의 코드를 실행하면 아래의 파일들이 생성됩니다.

├── alembic.ini
└── "migration dir name"
├── README
├── env.py
├── env.pyc
├── script.py.mako
└── versions

migration 목록 보기

alembic history


revision

revision은
base - revison - revision - head 이며
revision은 시간 순으로 생성됩니다.
가장 초기 상태를 base
가장 최근 상태를 head라고 합니다.

새로운 revision 생성하기

alembic revision -m <revision title>


가장 최신의 revision으로 upgrade

alembic upgrade head


특정 시점(revision)으로 downgrade

alembic downgrade <revision>


특정 시점(revision)으로 upgrade

alembic upgrade <revision>

revision 단계 upgrade

alembic upgrade +1
alembic upgrade -1

위와 같이 현재 상태에서 정수형으로 이동할 수 있다.

0개의 댓글