Skip to content

Instantly share code, notes, and snippets.

Embed
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/env.py``
6. Now, import your metadata/db object from your app.:
# {{my_project}}/{{my_project_dir}}/app.py
# an example app
from gino.ext.sanic import Gino
from sanic import sanic
# Make sure you also configure Gino from app.config
# https://github.com/fantix/gino/blob/master/docs/sanic.rst
app = Sanic()
db = Gino
db.init_app(app)
# {{my_project}}/alembic/env.py
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! :)
@kexirong

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/config.py", line 575, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/usr/local/lib/python3.6/site-packages/alembic/config.py", line 569, in main
    self.run_cmd(cfg, options)
  File "/usr/local/lib/python3.6/site-packages/alembic/config.py", line 549, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "/usr/local/lib/python3.6/site-packages/alembic/command.py", line 298, in upgrade
    script.run_env()
  File "/usr/local/lib/python3.6/site-packages/alembic/script/base.py", line 489, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/usr/local/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python3.6/site-packages/alembic/util/compat.py", line 173, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "alembic/env.py", line 83, in <module>
    run_migrations_online()
  File "alembic/env.py", line 77, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/usr/local/lib/python3.6/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/local/lib/python3.6/site-packages/alembic/runtime/migration.py", line 518, in run_migrations
    step.migration_fn(**kw)
  File "/home/kexirong/qkacmdb-sanic/alembic/versions/8899e0ca4256_init.py", 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