简体   繁体   中英

change database migrations from sqlalchemy-migrate to Flask-Migrate

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:

1. Only track future migrations with Flask-Migrate

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.

2. Track the current state of the database as a single initial migration

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:

  • Create the Flask-Migrate repository
  • Backup the data in your database, if you care (you will lose it in your next step!)
  • Using the db_downgrade.py script from the tutorial, set your database to version 0 (the empty database).
  • Generate a migration with Flask-Migrate. This migration will include all the database changes until your current version.
  • Restore the data to your database.
  • Apply future migrations with Flask-Migrate normally. You can delete the sqlalchemy-migrate repository, as all the database changes are now in Alembic.

3. Transfer the entire migration history to Flask-Migrate

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:

  • Create the Flask-Migrate repository
  • Backup the data in your database, if you care (you will lose it in your next step!)
  • Using the 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.
  • Assuming you have your application on source control, checkout the version of code that matches your first database migration.
  • Generate a migration with Flask-Migrate. This migration will be identical to your first sqlalchemy-migrate migration.
  • Checkout the version of code that matches the second sqlalchemy-migrate migration.
  • Generate a second migration with Flask-Migrate. This migration will be identical to your second sqlalchemy-migrate migration.
  • Continue like this until you reach the current version of code and the last database migration.
  • Restore the data to your database.
  • Apply future migrations with Flask-Migrate normally. You can delete the sqlalchemy-migrate repository, as all the database changes are now in Alembic.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM