fantasy/cli.py
def migrate(migrations_root):
    """等价于 django migrate 操作"""
    from flask_migrate import Migrate, upgrade as migrate_upgrade
    from flask_sqlalchemy import SQLAlchemy
    from sqlalchemy.engine.url import make_url
    from sqlalchemy_utils import database_exists, create_database

    db = SQLAlchemy()
    dsn = make_url(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(dsn):
        create_database(dsn)
        pass

    migrations_root = migrations_root or os.path.join(
        os.environ.get('FANTASY_MIGRATION_PATH',
                       os.getcwd()),
        'migrations')
    migrations_root = os.path.expanduser(migrations_root)

    if os.path.exists(migrations_root):
        mig = Migrate(app, db, directory=migrations_root)
        mig.init_app(app, db)
        migrate_upgrade(migrations_root)
    else:
        click.echo(
            click.style('migration files not exist,skip migrate...', fg='red'))
        sys.exit(-1)
    pass
def migrate(migrations_root):
    """等价于 django migrate 操作"""
    from flask_migrate import Migrate, upgrade as migrate_upgrade
    from flask_sqlalchemy import SQLAlchemy
    from sqlalchemy.engine.url import make_url
    from sqlalchemy_utils import database_exists, create_database

    db = SQLAlchemy()
    dsn = make_url(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(dsn):
        create_database(dsn)
        pass

    migrations_root = migrations_root or os.path.join(
        os.environ.get('FANTASY_MIGRATION_PATH',
                       os.getcwd()),
        'migrations')
    migrations_root = os.path.expanduser(migrations_root)

    if os.path.exists(migrations_root):
        mig = Migrate(app, db, directory=migrations_root)
        mig.init_app(app, db)
        migrate_upgrade(migrations_root)
    else:
        click.echo(
            click.style('migration files not exist,skip migrate...', fg='red'))
        sys.exit(-1)
    pass
等价于 django migrate 操作