Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sanic/Gino/Alembic migrations
# Do the first 6 steps only once.
1. pip install --user alembic
2. bash: ``cd {{my_project}} && alembic init alembic``
3. bash: ``text_editor {{my_project}}/alembic.ini``
4. Change: "sqlalchemy.url = postgres://{{username}}:{{password}}@{{address}}/{{db_name}}"
5. bash: ``text_editor {{my_project}}/alembic/``
6. Now, import your metadata/db object from your app.:
# {{my_project}}/{{my_project_dir}}/
# an example app
from gino.ext.sanic import Gino
from sanic import sanic
# Make sure you also configure Gino from app.config
app = Sanic()
db = Gino
# {{my_project}}/alembic/
from {{my_project_dir}} import db
target_metadata = db
# Now, whenever you want to do migrations, execute the following in a bash terminal
# Autogenerates migrations based on the models you defined in your app
7. alembic revision -m "Migration message" --autogenerate --head head
# Applies migrations
8. alembic upgrade head
Done! :)

This comment has been minimized.

Copy link

@kexirong kexirong commented Nov 18, 2019

[kexirong@localhost qkacmdb-sanic]$ alembic upgrade head
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 1954d3e07c17 -> 8899e0ca4256, init
Traceback (most recent call last):
  File "/usr/local/bin/alembic", line 11, in <module>
    load_entry_point('alembic==1.3.1', 'console_scripts', 'alembic')()
  File "/usr/local/lib/python3.6/site-packages/alembic/", line 575, in main
  File "/usr/local/lib/python3.6/site-packages/alembic/", line 569, in main
    self.run_cmd(cfg, options)
  File "/usr/local/lib/python3.6/site-packages/alembic/", line 549, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "/usr/local/lib/python3.6/site-packages/alembic/", line 298, in upgrade
  File "/usr/local/lib/python3.6/site-packages/alembic/script/", line 489, in run_env
    util.load_python_file(self.dir, "")
  File "/usr/local/lib/python3.6/site-packages/alembic/util/", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python3.6/site-packages/alembic/util/", line 173, in load_module_py
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "alembic/", line 83, in <module>
  File "alembic/", line 77, in run_migrations_online
  File "<string>", line 8, in run_migrations
  File "/usr/local/lib/python3.6/site-packages/alembic/runtime/", line 846, in run_migrations
  File "/usr/local/lib/python3.6/site-packages/alembic/runtime/", line 518, in run_migrations
  File "/home/kexirong/qkacmdb-sanic/alembic/versions/", line 29, in upgrade
    sa.Column('roles', gino.schema.Enum('staff', 'admin', 'superuser'), nullable=True, comment='用户类别'),
NameError: name 'gino' is not defined

i can't use

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment