From b53773961b37aad4a1ca19c19fc4d9d1eb8a3ee9 Mon Sep 17 00:00:00 2001 From: Till Skrodzki Date: Sat, 23 Sep 2023 08:51:24 +0300 Subject: [PATCH] Dirty use text() for DbCleanup with limits --- mapadroid/db/helper/PokemonHelper.py | 25 +++++++++++++------ mapadroid/db/helper/PokestopIncidentHelper.py | 17 ++++++++++--- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/mapadroid/db/helper/PokemonHelper.py b/mapadroid/db/helper/PokemonHelper.py index efc80c3e8..5c49735fc 100644 --- a/mapadroid/db/helper/PokemonHelper.py +++ b/mapadroid/db/helper/PokemonHelper.py @@ -69,7 +69,8 @@ async def get_pokemon_spawn_counts(session: AsyncSession, hours: int = None) -> @staticmethod async def get_to_be_encountered(session: AsyncSession, geofence_helper: Optional[GeofenceHelper], - min_time_left_seconds: int, eligible_mon_ids: Optional[List[int]]) -> List[Tuple[int, Location, int]]: + min_time_left_seconds: int, eligible_mon_ids: Optional[List[int]]) -> List[ + Tuple[int, Location, int]]: if min_time_left_seconds is None or not eligible_mon_ids: logger.warning( "DbWrapper::get_to_be_encountered: Not returning any encounters since no time left or " @@ -144,8 +145,8 @@ async def get_mons_in_rectangle(session: AsyncSession, @staticmethod async def get_all_shiny(session: AsyncSession, timestamp_after: Optional[int] = None, timestamp_before: Optional[int] = None) -> Dict[int, - Tuple[Pokemon, - List[TrsStatsDetectWildMonRaw]]]: + Tuple[Pokemon, + List[TrsStatsDetectWildMonRaw]]]: """ Used to be DbStatsReader::get_shiny_stats_v2 Args: @@ -294,8 +295,8 @@ async def get_noniv_encounters_count(session: AsyncSession, @staticmethod async def get_changed_since(session: AsyncSession, _timestamp: int, mon_types: Optional[Set[MonSeenTypes]] = None) -> List[Tuple[Pokemon, TrsSpawn, - Optional[Pokestop], - Optional[PokemonDisplay]]]: + Optional[Pokestop], + Optional[PokemonDisplay]]]: if not mon_types: mon_types = {MonSeenTypes.encounter, MonSeenTypes.lure_encounter} @@ -320,10 +321,18 @@ async def delete_older_than_n_hours(session: AsyncSession, hours: int, limit: Op where_condition = Pokemon.disappear_time < DatetimeWrapper.now() - datetime.timedelta(hours=hours) stmt = delete(Pokemon).where(where_condition) if limit: - stmt = stmt.with_dialect_options(mysql_limit=limit, mariadb_limit=limit) - await session.execute(stmt) + # Rather ugly construct as stmt.with_dialect_options currently does not work + # See https://groups.google.com/g/sqlalchemy/c/WDKhyAt6eAk/m/feteFNZnAAAJ + stmt = text(f"{str(stmt)} LIMIT :limit") + await session.execute(stmt, + { + "disappear_time_1": DatetimeWrapper.now() - datetime.timedelta(hours=hours), + "limit": limit + }) + else: + await session.execute(stmt) @staticmethod async def run_optimize(session: AsyncSession) -> None: - stmt = text(f"OPTIMIZE {Pokemon.__tablename__}") + stmt = text(f"OPTIMIZE TABLE {Pokemon.__tablename__}") await session.execute(stmt) diff --git a/mapadroid/db/helper/PokestopIncidentHelper.py b/mapadroid/db/helper/PokestopIncidentHelper.py index 1af33eaa4..46b173fff 100644 --- a/mapadroid/db/helper/PokestopIncidentHelper.py +++ b/mapadroid/db/helper/PokestopIncidentHelper.py @@ -24,11 +24,20 @@ async def get(session: AsyncSession, pokestop_id: str, incident_id: str) -> Opti async def delete_older_than_n_hours(session: AsyncSession, hours: int, limit: Optional[int]) -> None: where_condition = PokestopIncident.incident_expiration < DatetimeWrapper.now() - datetime.timedelta(hours=hours) stmt = delete(PokestopIncident).where(where_condition) - if limit is not None: - stmt = stmt.with_dialect_options(mysql_limit=limit, mariadb_limit=limit) - await session.execute(stmt) + if limit: + # Rather ugly construct as stmt.with_dialect_options currently does not work + # See https://groups.google.com/g/sqlalchemy/c/WDKhyAt6eAk/m/feteFNZnAAAJ + stmt = text(f"{str(stmt)} LIMIT :limit") + await session.execute(stmt, + { + "incident_expiration_1": DatetimeWrapper.now() - datetime.timedelta(hours=hours), + "limit": limit + } + ) + else: + await session.execute(stmt) @staticmethod async def run_optimize(session: AsyncSession) -> None: - stmt = text(f"OPTIMIZE {PokestopIncident.__tablename__}") + stmt = text(f"OPTIMIZE TABLE {PokestopIncident.__tablename__}") await session.execute(stmt)