async def downgrade(ctx: Context, version: int, delete: bool): 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 = Path(Migrate.migrate_location, file) content = get_version_content_from_file(file_path) downgrade_query_list = content.get("downgrade") if not downgrade_query_list: click.secho("No downgrade items found", fg=Color.yellow) return for downgrade_query in downgrade_query_list: await conn.execute_query(downgrade_query) await version.delete() if delete: os.unlink(file_path) click.secho(f"Success downgrade {file}", fg=Color.green)
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
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)
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