Alembic

From 탱이의 잡동사니
Jump to navigation Jump to search

Overview

SQLAlchemy based database migration tool alembic 내용 정리

Add revision

alembic revision 을 사용하면 새로운 변경사항을 추가할 수 있다.

$ alembic -c config.ini revision -m "increse_reg_server_size" 

Example

$ alembic -c config.ini revision -m "increse_reg_server_size"
  Generating /home/pchero/gittmp/asterisk/contrib/ast-db-manage/config/versions/1ae0609b6646_increse_reg_server_size.py ...  done

Example

<source lang=bash> $ alembic init myproject

 Creating directory /home/pchero/github/etc_examples/alembic_example/simple_test/myproject ...  done
 Creating directory /home/pchero/github/etc_examples/alembic_example/simple_test/myproject/versions ...  done
 Generating /home/pchero/github/etc_examples/alembic_example/simple_test/myproject/env.py ...  done
 Generating /home/pchero/github/etc_examples/alembic_example/simple_test/myproject/script.py.mako ...  done
 Generating /home/pchero/github/etc_examples/alembic_example/simple_test/myproject/README ...  done
 Generating /home/pchero/github/etc_examples/alembic_example/simple_test/alembic.ini ...  done
 Please edit configuration/connection/logging settings in '/home/pchero/github/etc_examples/alembic_example/simple_test/alembic.ini' before proceeding.

$ tree . . ├── alembic.ini └── myproject

   ├── env.py
   ├── README
   ├── script.py.mako
   └── versions

</source>

Upgrade

스크립트를 적용한다. upgrade() 에 정의된 작업을 수행한다. <source lang=bash> $ alembic -c alembic.ini upgrade head INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.runtime.migration] Running upgrade c939ba877f8f -> 5d2aab77fd9d, add provider info </source>

Downgrade

적용된 스크립트를 되돌릴 때 사용한다. downgrade() 에 정의된 작업을 수행한다. <source lang=bash> $ alembic -c alembic.ini downgrade -1 INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.runtime.migration] Running downgrade 5d2aab77fd9d -> c939ba877f8f, add provider info </source>

Current status check

<source lang=bash> $ alembic current --verbose INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. Current revision(s) for mysql://test-manager:XXXXX@192.168.0.10/test_manager: Rev: 5d2aab77fd9d (head) Parent: c939ba877f8f Path: /home/pchero/gitlab/pchero/test-manager/main/versions/5d2aab77fd9d_add_provider_info.py

   add provider info
   
   Revision ID: 5d2aab77fd9d
   Revises: c939ba877f8f
   Create Date: 2021-03-01 01:53:00.395262

</source>

History check

<source lang=bash> $ alembic history --verbose 7s Rev: 5d2aab77fd9d (head) Parent: c939ba877f8f Path: /home/pchero/gitlab/voipbin/test-manager/main/versions/5d2aab77fd9d_add_provider_info.py

   add provider info
   
   Revision ID: 5d2aab77fd9d
   Revises: c939ba877f8f
   Create Date: 2021-03-01 01:53:00.395262

</source>

See also