fantasy/cli.py
def makemigrations(migrations_root):
    """等价于 django makemigrations 操作"""
    from flask_migrate import (Migrate, init as migrate_init,
                               migrate as migrate_exec)

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

    migrations_root = os.path.expanduser(migrations_root)

    mig = Migrate(app, app.db, directory=migrations_root)

    if not os.path.exists(migrations_root):
        migrate_init(migrations_root)
        pass

    models_file = os.path.join(migrations_root, 'models.txt')

    if not os.path.exists(models_file):
        with open(models_file, 'w') as fw:
            fw.write('# add module name in this file.')
            pass
        pass

    with open(models_file, 'r') as fp:
        modules = fp.readlines()
        pass

    modules = filter(lambda x: x.strip("\n"), modules)
    modules = map(lambda x: x.strip("\n").split("#")[0].strip(), modules)
    modules = list(filter(lambda x: x, modules))

    if not modules:
        click.echo(
            click.style('No models found,'
                        'skip create migrations...'
                        'You need edit models.txt file set your module',
                        fg='yellow'))
        sys.exit(0)

    for m in modules:
        importlib.import_module(m + '.models')
        pass

    migrate_exec(migrations_root)
    mig.init_app(app, app.db)
    pass
def makemigrations(migrations_root):
    """等价于 django makemigrations 操作"""
    from flask_migrate import (Migrate, init as migrate_init,
                               migrate as migrate_exec)

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

    migrations_root = os.path.expanduser(migrations_root)

    mig = Migrate(app, app.db, directory=migrations_root)

    if not os.path.exists(migrations_root):
        migrate_init(migrations_root)
        pass

    models_file = os.path.join(migrations_root, 'models.txt')

    if not os.path.exists(models_file):
        with open(models_file, 'w') as fw:
            fw.write('# add module name in this file.')
            pass
        pass

    with open(models_file, 'r') as fp:
        modules = fp.readlines()
        pass

    modules = filter(lambda x: x.strip("\n"), modules)
    modules = map(lambda x: x.strip("\n").split("#")[0].strip(), modules)
    modules = list(filter(lambda x: x, modules))

    if not modules:
        click.echo(
            click.style('No models found,'
                        'skip create migrations...'
                        'You need edit models.txt file set your module',
                        fg='yellow'))
        sys.exit(0)

    for m in modules:
        importlib.import_module(m + '.models')
        pass

    migrate_exec(migrations_root)
    mig.init_app(app, app.db)
    pass
等价于 django makemigrations 操作