Пример #1
0
async def upgrade(ctx: Context):
    config = ctx.obj["config"]
    app = ctx.obj["app"]
    location = ctx.obj["location"]
    migrated = False
    for version_file in Migrate.get_all_version_files():
        try:
            exists = await Aerich.exists(version=version_file, app=app)
        except OperationalError:
            exists = False
        if not exists:
            async with in_transaction(get_app_connection_name(config,
                                                              app)) as conn:
                file_path = os.path.join(Migrate.migrate_location,
                                         version_file)
                with open(file_path, "r", encoding="utf-8") as f:
                    content = json.load(f)
                    upgrade_query_list = content.get("upgrade")
                    for upgrade_query in upgrade_query_list:
                        await conn.execute_script(upgrade_query)
                await Aerich.create(
                    version=version_file,
                    app=app,
                    content=Migrate.get_models_content(config, app, location),
                )
            click.secho(f"Success upgrade {version_file}", fg=Color.green)
            migrated = True
    if not migrated:
        click.secho("No migrate items", fg=Color.yellow)
Пример #2
0
async def init_db(ctx: Context, safe):
    config = ctx.obj["config"]
    location = ctx.obj["location"]
    app = ctx.obj["app"]

    dirname = os.path.join(location, app)
    if not os.path.isdir(dirname):
        os.mkdir(dirname)
        click.secho(f"Success create app migrate location {dirname}",
                    fg=Color.green)
    else:
        return click.secho(f"Inited {app} already", fg=Color.yellow)

    await Tortoise.init(config=config)
    connection = get_app_connection(config, app)
    await generate_schema_for_client(connection, safe)

    schema = get_schema_sql(connection, safe)

    version = await Migrate.generate_version()
    await Aerich.create(version=version,
                        app=app,
                        content=Migrate.get_models_content(
                            config, app, location))
    with open(os.path.join(dirname, version), "w", encoding="utf-8") as f:
        content = {
            "upgrade": [schema],
        }
        json.dump(content, f, ensure_ascii=False, indent=2)
    return click.secho(f'Success generate schema for app "{app}"',
                       fg=Color.green)
Пример #3
0
async def init_db(ctx: Context, safe):
    config = ctx.obj["config"]
    location = ctx.obj["location"]
    app = ctx.obj["app"]

    dirname = Path(location, app)
    if not dirname.is_dir():
        os.mkdir(dirname)
        click.secho(f"Success create app migrate location {dirname}",
                    fg=Color.green)
    else:
        return click.secho(
            f"Inited {app} already, or delete {dirname} and try again.",
            fg=Color.yellow)

    await Tortoise.init(config=config)
    connection = get_app_connection(config, app)
    await generate_schema_for_client(connection, safe)

    schema = get_schema_sql(connection, safe)

    version = await Migrate.generate_version()
    await Aerich.create(
        version=version,
        app=app,
        content=Migrate.get_models_content(config, app, location),
    )
    content = {
        "upgrade": [schema],
    }
    write_version_file(Path(dirname, version), content)
    click.secho(f'Success generate schema for app "{app}"', fg=Color.green)