I have an web app base on miguel flask tutorial
So I use sqlalchemy-migrate for database and Now I should Use Alembic in migration How can I do the configuration? is there any way that I can change from sqlalchemy-migrate to Flask-Migrate?(coz my app is complete and do it again is so much work again) Thanx
db_migrate.py :
#!flask/bin/python
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))
There are three possible ways to handle this, two are relatively easy, but the last is pretty laborious:
This is the easiest, but less interesting. Just install and setup Flask-Migrate as you would for a new project, and the next time you need to migrate, use this instead of sqlalchemy-migrate.
The drawback is that the current state of the migration is not recorded by Alembic, only the migrations you apply in the future are.
This improves on the previous method and makes Flask-Migrate's repository complete, but all the migrations that you have in sqlalchemy-migrate are collapsed into a single migration for Alembic.
The process is as follows:
db_downgrade.py
script from the tutorial, set your database to version 0 (the empty database).This is the most complex of the solutions, as it requires every single migration you have in sqlalchemy-migrate to be transferred individually to Alembic.
The process is as follows:
db_downgrade.py
script from the tutorial, set your database to version 0 (the empty database). Or if it is easier, just delete all the tables in the database manually.If you need a recommendation, I think #2 is the best option in most cases. I would only go through the pain of #3 if I expected I would need to downgrade my database to specific migrations in the history.