Exemplo n.º 1
0
async def downgrade(ctx: Context, version: int):
    app = ctx.obj["app"]
    config = ctx.obj["config"]
    if version == -1:
        specified_version = await Migrate.get_last_version()
    else:
        specified_version = await Aerich.filter(
            app=app, version__startswith=f"{version}_").first()
    if not specified_version:
        return click.secho("No specified version found", fg=Color.yellow)
    if version == -1:
        versions = [specified_version]
    else:
        versions = await Aerich.filter(app=app, pk__gte=specified_version.pk)
    for version in versions:
        file = version.version
        async with in_transaction(get_app_connection_name(config,
                                                          app)) as conn:
            file_path = os.path.join(Migrate.migrate_location, file)
            with open(file_path, "r", encoding="utf-8") as f:
                content = json.load(f)
                downgrade_query_list = content.get("downgrade")
                if not downgrade_query_list:
                    return click.secho("No downgrade item found",
                                       fg=Color.yellow)
                for downgrade_query in downgrade_query_list:
                    await conn.execute_query(downgrade_query)
                await version.delete()
            os.unlink(file_path)
            click.secho(f"Success downgrade {file}", fg=Color.green)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
0
 async def downgrade(self, version: int, delete: bool):
     ret = []
     if version == -1:
         specified_version = await Migrate.get_last_version()
     else:
         specified_version = await Aerich.filter(
             app=self.app, version__startswith=f"{version}_").first()
     if not specified_version:
         raise DowngradeError("No specified version found")
     if version == -1:
         versions = [specified_version]
     else:
         versions = await Aerich.filter(app=self.app,
                                        pk__gte=specified_version.pk)
     for version in versions:
         file = version.version
         async with in_transaction(
                 get_app_connection_name(self.tortoise_config,
                                         self.app)) as conn:
             file_path = Path(Migrate.migrate_location, file)
             content = get_version_content_from_file(file_path)
             downgrade_query_list = content.get("downgrade")
             if not downgrade_query_list:
                 raise DowngradeError("No downgrade items found")
             for downgrade_query in downgrade_query_list:
                 await conn.execute_query(downgrade_query)
             await version.delete()
             if delete:
                 os.unlink(file_path)
             ret.append(file)
     return ret
Exemplo n.º 5
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)
Exemplo n.º 6
0
async def downgrade(ctx: Context):
    app = ctx.obj["app"]
    config = ctx.obj["config"]
    last_version = await Migrate.get_last_version()
    if not last_version:
        return click.secho("No last version found", fg=Color.yellow)
    file = last_version.version
    async with in_transaction(get_app_connection_name(config, app)) as conn:
        file_path = os.path.join(Migrate.migrate_location, file)
        with open(file_path, "r") as f:
            content = json.load(f)
            downgrade_query_list = content.get("downgrade")
            if not downgrade_query_list:
                return click.secho(f"No downgrade item dound", fg=Color.yellow)
            for downgrade_query in downgrade_query_list:
                await conn.execute_query(downgrade_query)
            await last_version.delete()
        return click.secho(f"Success downgrade {file}", fg=Color.green)
Exemplo n.º 7
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)
Exemplo n.º 8
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