工作目录: __init__.py
#!/usr/bin/env python
# -*- coding=utf8 -*-
import os
if __name__ != '__main__':
import sys
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_DIR = os.path.dirname(CUR_DIR)
sys.path.insert(0, PROJECT_DIR)
sys.path.insert(0, CUR_DIR)
from alembic.config import Config
from utils.orm import DB
# db仓库地址
REPO_PATH = os.path.join(PROJECT_DIR, "db_repository")
# 迁移工具配置文件地址 初始化仓库时的依赖 以后不使用此配置
CONFIG_FILE = "/tmp/alembic.ini"
# 迁移版本管理表
# MIGRATE_VERSION_TABLE = "migrate_version"
def get_alembic_config(file_: str = None):
config = Config(file_=file_)
config.set_main_option("sqlalchemy.url", DB.create_url(sync=False))
config.set_main_option("script_location", REPO_PATH)
config.set_main_option("prepend_sys_path", PROJECT_DIR)
return config
初始化db仓库
#!/usr/bin/env python
# -*- coding=utf8 -*-
from alembic.command import init
from __init__ import REPO_PATH, CONFIG_FILE, get_alembic_config
def main():
# 初始化db仓库
config = get_alembic_config(CONFIG_FILE)
init(config, REPO_PATH)
if __name__ == '__main__':
main()
迁移管理
#!/usr/bin/env python
# -*- coding=utf8 -*-
from __init__ import get_alembic_config
from alembic.config import CommandLine
def main(argv=None, prog=None, **kwargs):
"""
等价于alembic命令(不再依赖配置文件alembic.ini)
"""
cmd_line = CommandLine(prog=prog)
options = cmd_line.parser.parse_args(argv)
if not hasattr(options, "cmd"):
# see http://bugs.python.org/issue9253, argparse
# behavior changed incompatibly in py3.3
cmd_line.parser.error("too few arguments")
else:
config = get_alembic_config()
cmd_line.run_cmd(config, options)
if __name__ == '__main__':
main()
env.py
# from importlib import import_module
from alembic import context
from utils.orm import DB
from models import BaseModel
# include db models
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
# config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
# fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = BaseModel.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = DB.create_url(sync=False)
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = DB.get_instance(sync=False)
with engine.connect() as conn:
context.configure(
connection=conn,
target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
|