python 3.5的迁移工具+
mongrations的Python项目详细描述
混合
python 3.6+的迁移工具。Mongurations最初是作为MongoDB迁移工具启动的,但引入了MySQL&Postgres 作为Mongurations工具的兼容服务器。
入门
一。生成迁移文件
mongrations -C true --name insert-into-members
2.生成的迁移文件(import和类定义的内容是 为迁移文件自动生成-up()和down()的内容由用户定义)
frommongrationsimportMongrations,Databasefrompydotenvimportload_envload_env()# This is a MongoDB example. Go to /examples directory# for more examplesclassMongration(Database):def__init__(self):super(Database,self).__init__()defup(self):collection=self.db['members']data={'accountId':1,'username':'admin','email':'admin@able.digital','firstName':'Site','lastName':'Owner'}collection.insert_one(data)defdown(self):collection=self.db['members']collection.delete_one({'username':'admin'})Mongrations(Mongration,'sync')
三。运行迁移
mongrations -M true
安装
pip install --upgrade mongrations
或在本地安装
python setup.py build sudo python setup.py install
使用
mongurations提供了一个cli工具以及一个pythonic迁移方法的类。pymongo、pymysql和psycopg2用于 引擎盖,跟着PyMongo, PyMySQL或Psycopg2的文档 有关如何创建迁移的说明。对于此应用程序中使用的环境变量工具,请执行以下操作 this repo(它也与此软件包一起安装)。
有关详细信息,请参阅mongurationsdocumentation。
cli
Usage: mongrations [OPTIONS] Options: -M, --migrate BOOLEAN Run migrations -C, --create BOOLEAN Create new migration -N, --name TEXT Name for newly created migration -F, --file_path TEXT File path for newly created migration -U, --undo BOOLEAN Undo last migration -D, --down BOOLEAN Revert database --version Show the version and exit. --help Show this message and exit.
cli示例
mongrations -C true --name [migration_name]# create new migration mongrations -M true# run migrations mongrations -D true# tear down migrations mongrations -U true# undo last migration
mongurations类
frommongrationsimportMongrationsClimigrations=MongrationsCli()migrations.create(directory='migrations',name='file_name')migrations.migrate()migrations.down()migrations.undo()
在examples/folder中运行示例迁移
问题
请将所有问题报告给回购。
注释
您可以通过setup.py develop build从源代码安装psycopg2,也可以参考它们的repo。
下一个
单元测试。