migrations.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:python_ddd_flask 作者: igorvinnicius 项目源码 文件源码
def db_migrate():
        import imp
        from config import config
        from app.infrastructure.mappings import mapping
        from app.infrastructure.mappings.mapping import metadata
        db = SQLAlchemy()
        config = config[os.getenv('FLASK_CONFIG') or 'default']
        metadata.create_all(db.engine)
        v = api.db_version(config.SQLALCHEMY_DATABASE_URI, config.SQLALCHEMY_MIGRATE_REPO)
        migration = config.SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
        tmp_module = imp.new_module('old_model')
        old_model = api.create_model(config.SQLALCHEMY_DATABASE_URI, config.SQLALCHEMY_MIGRATE_REPO)
        exec(old_model, tmp_module.__dict__)
        script = api.make_update_script_for_model(config.SQLALCHEMY_DATABASE_URI, config.SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, metadata)
        open(migration, "wt").write(script)
        api.upgrade(config.SQLALCHEMY_DATABASE_URI, config.SQLALCHEMY_MIGRATE_REPO)
        v = api.db_version(config.SQLALCHEMY_DATABASE_URI, config.SQLALCHEMY_MIGRATE_REPO)
        print('New migration saved as ' + migration)
        print('Current database version: ' + str(v))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号