Beispiel #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)
Beispiel #2
0
 async def heads(self):
     ret = []
     versions = Migrate.get_all_version_files()
     for version in versions:
         if not await Aerich.exists(version=version, app=self.app):
             ret.append(version)
     return ret
Beispiel #3
0
async def upgrade(ctx: Context):
    config = ctx.obj["config"]
    app = ctx.obj["app"]
    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 = Path(Migrate.migrate_location, version_file)
                content = get_version_content_from_file(file_path)
                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=get_models_describe(app),
                )
            click.secho(f"Success upgrade {version_file}", fg=Color.green)
            migrated = True
    if not migrated:
        click.secho("No upgrade items found", fg=Color.yellow)
Beispiel #4
0
async def history(ctx: Context):
    """List all migrate items."""
    versions = Migrate.get_all_version_files()
    for version in versions:
        typer.secho(version, fg=typer.colors.GREEN)
    if not versions:
        typer.secho("No history,try migrate", fg=typer.colors.GREEN)
Beispiel #5
0
async def heads(ctx: Context):
    app = ctx.obj["app"]
    versions = Migrate.get_all_version_files()
    is_heads = False
    for version in versions:
        if not await Aerich.exists(version=version, app=app):
            click.secho(version, fg=Color.green)
            is_heads = True
    if not is_heads:
        click.secho("No available heads,try migrate first", fg=Color.green)
Beispiel #6
0
async def heads(ctx: Context):
    """Show current available heads in migrate location."""
    app,config,location=await connect_tortoise(ctx)
    versions = Migrate.get_all_version_files()
    is_heads = False
    for version in versions:
        if not await Aerich.exists(version=version, app=app):
            typer.secho(version, fg=typer.colors.GREEN)
            is_heads = True
    if not is_heads:
        typer.secho("No available heads,try migrate", fg=typer.colors.GREEN)
Beispiel #7
0
async def upgrade(ctx: Context):
    config = ctx.obj["config"]
    app = ctx.obj["app"]
    migrated = False
    for version in Migrate.get_all_version_files():
        if not await Aerich.exists(version=version, app=app):
            async with in_transaction(get_app_connection_name(config, app)) as conn:
                file_path = os.path.join(Migrate.migrate_location, version)
                with open(file_path, "r") as f:
                    content = json.load(f)
                    upgrade_query_list = content.get("upgrade")
                    for upgrade_query in upgrade_query_list:
                        await conn.execute_query(upgrade_query)
            await Aerich.create(version=version, app=app)
            click.secho(f"Success upgrade {version}", fg=Color.green)
            migrated = True
    if not migrated:
        click.secho("No migrate items", fg=Color.yellow)
Beispiel #8
0
def test_sort_all_version_files(mocker):
    mocker.patch(
        "os.listdir",
        return_value=[
            "1_datetime_update.sql",
            "11_datetime_update.sql",
            "10_datetime_update.sql",
            "2_datetime_update.sql",
        ],
    )

    Migrate.migrate_location = "."

    assert Migrate.get_all_version_files() == [
        "1_datetime_update.sql",
        "2_datetime_update.sql",
        "10_datetime_update.sql",
        "11_datetime_update.sql",
    ]
Beispiel #9
0
async def upgrade(ctx: Context):
    app = ctx.obj["app"]
    config = ctx.obj["config"]
    available_versions = Migrate.get_all_version_files(is_all=False)
    if not available_versions:
        return click.secho("No migrate items", fg=Color.yellow)
    async with in_transaction(get_app_connection_name(config, app)) as conn:
        for file in available_versions:
            file_path = os.path.join(Migrate.migrate_location, file)
            with open(file_path, "r") as f:
                content = json.load(f)
                upgrade_query_list = content.get("upgrade")
                for upgrade_query in upgrade_query_list:
                    await conn.execute_query(upgrade_query)

            with open(file_path, "w") as f:
                content["migrate"] = True
                json.dump(content, f, indent=2, ensure_ascii=False)
                click.secho(f"Success upgrade {file}", fg=Color.green)
Beispiel #10
0
async def upgrade(ctx: Context):
    """Upgrade to latest version."""
    app, config, location = await connect_tortoise(ctx)
    migrated = False
    for version in Migrate.get_all_version_files():
        try:
            exists = await Aerich.exists(version=version, 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)
                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, app=app)
            typer.secho(f"Success upgrade {version}", fg=typer.colors.GREEN)
            migrated = True
    if not migrated:
        typer.secho("No migrate items", fg=typer.colors.YELLOW)
Beispiel #11
0
 async def upgrade(self):
     migrated = []
     for version_file in Migrate.get_all_version_files():
         try:
             exists = await Aerich.exists(version=version_file,
                                          app=self.app)
         except OperationalError:
             exists = False
         if not exists:
             async with in_transaction(
                     get_app_connection_name(self.tortoise_config,
                                             self.app)) as conn:
                 file_path = Path(Migrate.migrate_location, version_file)
                 content = get_version_content_from_file(file_path)
                 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=self.app,
                     content=get_models_describe(self.app),
                 )
             migrated.append(version_file)
     return migrated
Beispiel #12
0
async def history(ctx: Context):
    versions = Migrate.get_all_version_files()
    for version in versions:
        click.secho(version, fg=Color.green)
    if not versions:
        click.secho("No history,try migrate", fg=Color.green)
Beispiel #13
0
 async def history(self):
     ret = []
     versions = Migrate.get_all_version_files()
     for version in versions:
         ret.append(version)
     return ret
Beispiel #14
0
def history(ctx):
    for version in Migrate.get_all_version_files():
        click.secho(version, fg=Color.green)
Beispiel #15
0
def heads(ctx: Context):
    for version in Migrate.get_all_version_files(is_all=False):
        click.secho(version, fg=Color.green)