From 8865af9104f570401ae1bf1e7b0ca7afe7bdf5aa Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 15 Mar 2023 21:19:36 +0100 Subject: [PATCH] Remove .query from pairlock --- freqtrade/persistence/pairlock.py | 10 ++++------ freqtrade/persistence/pairlock_middleware.py | 6 ++++-- freqtrade/rpc/rpc.py | 2 +- freqtrade/util/binance_mig.py | 3 ++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/freqtrade/persistence/pairlock.py b/freqtrade/persistence/pairlock.py index 0cd595b66..76382b849 100644 --- a/freqtrade/persistence/pairlock.py +++ b/freqtrade/persistence/pairlock.py @@ -1,8 +1,8 @@ from datetime import datetime, timezone from typing import Any, ClassVar, Dict, Optional -from sqlalchemy import String, or_ -from sqlalchemy.orm import Mapped, Query, QueryPropertyDescriptor, mapped_column +from sqlalchemy import ScalarResult, String, or_, select +from sqlalchemy.orm import Mapped, QueryPropertyDescriptor, mapped_column from freqtrade.constants import DATETIME_PRINT_FORMAT from freqtrade.persistence.base import ModelBase, SessionType @@ -37,7 +37,7 @@ class PairLock(ModelBase): f'lock_end_time={lock_end_time}, reason={self.reason}, active={self.active})') @staticmethod - def query_pair_locks(pair: Optional[str], now: datetime, side: str = '*') -> Query: + def query_pair_locks(pair: Optional[str], now: datetime, side: str = '*') -> ScalarResult['PairLock']: """ Get all currently active locks for this pair :param pair: Pair to check for. Returns all current locks if pair is empty @@ -53,9 +53,7 @@ class PairLock(ModelBase): else: filters.append(PairLock.side == '*') - return PairLock.query.filter( - *filters - ) + return PairLock.session.scalars(select(PairLock).filter(*filters)) def to_json(self) -> Dict[str, Any]: return { diff --git a/freqtrade/persistence/pairlock_middleware.py b/freqtrade/persistence/pairlock_middleware.py index e6860bbe6..a8950961e 100644 --- a/freqtrade/persistence/pairlock_middleware.py +++ b/freqtrade/persistence/pairlock_middleware.py @@ -2,6 +2,8 @@ import logging from datetime import datetime, timezone from typing import List, Optional +from sqlalchemy import select + from freqtrade.exchange import timeframe_to_next_date from freqtrade.persistence.models import PairLock @@ -126,7 +128,7 @@ class PairLocks(): PairLock.active.is_(True), PairLock.reason == reason ] - locks = PairLock.query.filter(*filters) + locks = PairLock.session.scalars(select(PairLock).filter(*filters)).all() for lock in locks: logger.info(f"Releasing lock for {lock.pair} with reason '{reason}'.") lock.active = False @@ -170,6 +172,6 @@ class PairLocks(): Return all locks, also locks with expired end date """ if PairLocks.use_db: - return PairLock.query.all() + return PairLock.session.scalars(select(PairLock)).all() else: return PairLocks.locks diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index f86362841..6915c2025 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -959,7 +959,7 @@ class RPC: if pair: locks = PairLocks.get_pair_locks(pair) if lockid: - locks = PairLock.query.filter(PairLock.id == lockid).all() + locks = PairLock.session.scalar(select(PairLock).filter(PairLock.id == lockid)).all() for lock in locks: lock.active = False diff --git a/freqtrade/util/binance_mig.py b/freqtrade/util/binance_mig.py index 708bb1db7..37a2d2ef1 100644 --- a/freqtrade/util/binance_mig.py +++ b/freqtrade/util/binance_mig.py @@ -1,6 +1,7 @@ import logging from packaging import version +from sqlalchemy import select from freqtrade.constants import Config from freqtrade.enums.tradingmode import TradingMode @@ -44,7 +45,7 @@ def _migrate_binance_futures_db(config: Config): # Should symbol be migrated too? # order.symbol = new_pair Trade.commit() - pls = PairLock.query.filter(PairLock.pair.notlike('%:%')) + pls = PairLock.session.scalars(select(PairLock).filter(PairLock.pair.notlike('%:%'))).all() for pl in pls: pl.pair = f"{pl.pair}:{config['stake_currency']}" # print(pls)