使用 Alembic

Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. It’s also possible to use with GINO.

To add migrations to project first of all, add alembic as dependency:

  1. $ pip install --user alembic

When you need to set up alembic for your project.

Prepare sample project. We will have a structure:

  1. alembic_sample/
  2. my_app/
  3. models.py

Inside models.py define simple DB Model with GINO:

  1. from gino import Gino
  2. db = Gino()
  3. class User(db.Model):
  4. __tablename__ = 'users'
  5. id = db.Column(db.Integer(), primary_key=True)
  6. nickname = db.Column(db.Unicode(), default='noname')

Set up Alembic

This will need to be done only once. Go to the main folder of your project alembic_sample and run:

  1. $ alembic init alembic

Alembic will create a bunch of files and folders in your project directory. One of them will be alembic.ini. Open alembic.ini (you can find it in the main project folder alembic_sample). Now change property sqlalchemy.url = with your DB credentials. Like this:

  1. sqlalchemy.url = postgres://{{username}}:{{password}}@{{address}}/{{db_name}}

Next go to folder alembic/ and open env.py file. Inside the env.py file you need to import the db object. In our case db object is db from models modules. This is a variable that links to your Gino() instance.

Inside alembic/env.py:

  1. from main_app.models import db

And change target_metadata = to:

  1. target_metadata = db

That’s it. We finished setting up Alembic for a project.

注解

All alembic commands must be run always from the folder that contains the alembic.ini file.

Create first migration revision

Same commands you must run each time when you make some changes in DB Models and want to apply these changes to your DB Schema.

  1. $ alembic revision -m "first migration" --autogenerate --head head

If you have any problems relative to package imports similar to this example:

  1. File "alembic/env.py", line 7, in <module>
  2. from main_app.models import db
  3. ModuleNotFoundError: No module named 'main_app'

Either install your project locally with pip install -e ., poetry install or python setup.py develop, or add you package to PYTHONPATH, like this:

  1. $ export PYTHONPATH=$PYTHONPATH:/full_path/to/alembic_sample

After the successful run of alembic revision in folder alembic/versions you will see a file with new migration.

Apply migration on DB

Now time to apply migration to DB. It will create tables based on you DB Models.

  1. $ alembic upgrade head

Great. Now you apply your first migration. Congratulations!

Next time, when you will make any changes in DB models just do:

  1. $ alembic revision -m "your migration description" --autogenerate --head head

And

  1. alembic upgrade head

Full documentation about how to work with Alembic migrations, downgrades and other things - you can find in official docs https://alembic.sqlalchemy.org