alembic迁移不依赖配置文件
工作目录: __init__.py
#!/usr/bin/env python
# -*- coding=utf8 -*-
import os
if __name__ != '__main__':import sysCUR_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 Configfrom 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 initfrom __init__ import REPO_PATH, CONFIG_FILE, get_alembic_configdef 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_configfrom alembic.config import CommandLinedef 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.3cmd_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_modulefrom alembic import contextfrom 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 URLand not an Engine, though an Engine is acceptablehere as well. By skipping the Engine creationwe don't even need a DBAPI to be available.Calls to context.execute() here emit the given string to thescript 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 Engineand 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()
alembic迁移不依赖配置文件相关推荐
- mysql asp.net core_基于 ASP.NET Core 2.0 WebAPI 后台框架搭建(2) - EF Core (MySQL) CodeFirst 数据库迁移与依赖注入...
概述 上一章,我们简单的搭建了依赖注入的三层架构,但是,没有真正的使用,而我们现在就使用MySQL数据,基于 EF Core 的 DbFirst 进行框架搭建. 步骤 1. 右击 Entity 项目, ...
- oracle迁移需要注意配置文件,xtts迁移实践
XTTS迁移环境调研 --源库.目标库硬件及软件清理 源库: 192.168.56.20 hostname:slient rdbms:11.2.0.4 os:rhel 6.7 目标库: 192. ...
- 网站服务器迁移域名怎么配置文件,记录网站服务器搬家如何迁移 Let’s Encrypt 证书 | 老左笔记...
我们网站选择服务器或者主机也不会一成不变的在一台服务器或者一个主机商.由于商家之间的促销活动,不稳定因素等,我们需要网站服务器搬家迁移数据还是很正常的事情.昨天有网友遇到一个问题给老左留言(公众号:站 ...
- linux系统迁移的重要配置文件,mylinuxbackup
<<<<<<< HEAD mylinuxbackup 一些重要的配置文件备份,方便以后重装系统之后可以直接拷贝配置就可以了 Source code for b ...
- python数据库迁移命令_Python 数据库迁移工具 Alembic
Alembic 是一款轻量型的数据库迁移工具,它与 SQLAlchemy 一起共同为 Python 提供数据库管理与迁移支持. Alembic 的应用 Alembic 使用 SQLAlchemy 作为 ...
- kubernetes要实现的目标——随机关掉一台机器,看你的服务能否正常;减少的应用实例能否自动迁移并恢复到其他节点;服务能否随着流量进行自动伸缩...
Kubernetes 是来自 Google 云平台的开源容器集群管理系统.基于 Docker 构建一个容器的调度服务.该系统可以自动在一个容器集群中选择一个工作容器供使用.其核心概念是 Contain ...
- flutter项目迁移空安全
迁移 你的代码里大部分需要更改的代码,都是可以轻易推导的.例如,如果一个变量可以为空,它的类型需要 ? 后缀.一个不可以为空的命名参数,需要使用 required 标记. 针对迁移,你有两个选项可以选 ...
- 如何迁移 Flink 任务到实时计算
简介:本文由阿里巴巴技术专家景丽宁(砚田)分享,主要介绍如何迁移Flink任务到实时计算 Flink 中来. 作者 | 景丽宁(砚田),阿里巴巴技术专家景丽宁(砚田) 本文由阿里巴巴技术专家景丽宁(砚 ...
- 使用 .NET 升级助手将.NET Framework应用迁移到.NET 5
从.NET Framework 迁移到.NET 5 犹如搬家,我们都知道搬家是很痛苦的,我们请求搬家公司来减轻我们的压力,.NET 升级助手 的作用就类似我们聘请的搬家公司,帮助我们处理繁重乏味的迁移 ...
最新文章
- 「SAP技术」A项目关联公司间退货STO流程
- 微型计算机的评估指标,T∕CECA-G 0060-2020 “领跑者”标准评价要求 微型计算机(9页)-原创力文档...
- 20行代码实现电影评论情感分析
- 写文件 追加_总结Java中创建并写文件的5种方式
- Spring 学习04
- leetcode 66. 加一(C语言)
- 谈谈你对AOP的理解
- GPU环境配置指南(Ubuntu16.04+CUDA+CUDNN)
- 【Elasticsearch】 es kibana 安全实战
- [PAT] 02-线性结构2 Reversing Linked List(单向链表的逆转) - C语言实现
- html原生listview,Html中使用M$控件系列之 ListView 篇
- LayaAir TTF字体使用
- NI MultiSim 正版软件的下载链接
- java对比两张图片是否一致_Java实现图片对比功能
- novoton-msys_init函数
- 计算机与通信工程学院运动会海报,【图文】运动会宣传海报资料
- dsp正弦信号发生c语言,正弦信号发生器的DSP实现方法及比较
- docker安装和入门教程
- 根据股票涨跌用KMeans进行分类
- 顺丰php下单,顺丰订单查询