在哪里放置 flask-migrate --multidb 标志在脚本中?
Where to place the flask-migrate --multidb flag in a script?
我需要在我基于旧版Miguel Grinberg's Flask Mega-Tutorial. I have a couple of databases configured with SQLALCHEMY_BINDS
and they are working well, but the problem comes when I migrate them to a new version. Instead of getting three updated versions of my three databases, the three get collapsed into three tables in my first database. I understand from doing some research that this is because Alembic gets confused and munges the new versions together into the first database leaving the remaining two unaltered. I know I need to add the --multidb
flag编写的程序中添加多个数据库来解决这个问题。它通常是初始化的一部分:
$ flask db init --multidb
但我想知道我根据他的教程 (config.py
、db_create.py
、db_migrate.py
、downgrade.py
) 将它放在脚本中的什么位置。我的猜测可能在 db_migrate.py
脚本中的某处?或者 __init__.py
文件?你能给我一个正确的方向吗?谢谢!
migrate.py
import imp
from migrate.versioning import api
from app import db
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' %(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1)
tmp_module = imp.new_module('old_model')
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec old_model in tmp_module.__dict__
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print 'New migration saved as ' + migration
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
__init__.py
from flask import Flask
from flask.ext.socketio import SocketIO, emit
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config')
app.config['SECRET_KEY'] = 'shhhhhh!'
socketio = SocketIO(app)
db = SQLAlchemy(app)
from app import views, models
旧版本的教程没有使用 Flask-Migrate,它使用的是基于不同于 Alembic 的迁移工具的自行开发的解决方案。您无法真正将 Flask-Migrate/Alembic 放入这些脚本中,它们完全不兼容。
您可以查看新版本的教程,特别是第 4 章,了解如何使用 Flask-Migrate。
我需要在我基于旧版Miguel Grinberg's Flask Mega-Tutorial. I have a couple of databases configured with SQLALCHEMY_BINDS
and they are working well, but the problem comes when I migrate them to a new version. Instead of getting three updated versions of my three databases, the three get collapsed into three tables in my first database. I understand from doing some research that this is because Alembic gets confused and munges the new versions together into the first database leaving the remaining two unaltered. I know I need to add the --multidb
flag编写的程序中添加多个数据库来解决这个问题。它通常是初始化的一部分:
$ flask db init --multidb
但我想知道我根据他的教程 (config.py
、db_create.py
、db_migrate.py
、downgrade.py
) 将它放在脚本中的什么位置。我的猜测可能在 db_migrate.py
脚本中的某处?或者 __init__.py
文件?你能给我一个正确的方向吗?谢谢!
migrate.py
import imp
from migrate.versioning import api
from app import db
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' %(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1)
tmp_module = imp.new_module('old_model')
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec old_model in tmp_module.__dict__
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print 'New migration saved as ' + migration
print 'Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
__init__.py
from flask import Flask
from flask.ext.socketio import SocketIO, emit
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config')
app.config['SECRET_KEY'] = 'shhhhhh!'
socketio = SocketIO(app)
db = SQLAlchemy(app)
from app import views, models
旧版本的教程没有使用 Flask-Migrate,它使用的是基于不同于 Alembic 的迁移工具的自行开发的解决方案。您无法真正将 Flask-Migrate/Alembic 放入这些脚本中,它们完全不兼容。
您可以查看新版本的教程,特别是第 4 章,了解如何使用 Flask-Migrate。