Add Rollback function to Trade

simplifies Session work
This commit is contained in:
Matthias 2022-08-06 15:08:13 +02:00
parent 45d68222a1
commit 5250189f77
5 changed files with 10 additions and 5 deletions

View File

@ -196,7 +196,7 @@ class Order(_DECL_BASE):
if filtered_orders: if filtered_orders:
oobj = filtered_orders[0] oobj = filtered_orders[0]
oobj.update_from_ccxt_object(order) oobj.update_from_ccxt_object(order)
Order.query.session.commit() Trade.commit()
else: else:
logger.warning(f"Did not find order for {order}.") logger.warning(f"Did not find order for {order}.")
@ -1148,6 +1148,10 @@ class Trade(_DECL_BASE, LocalTrade):
def commit(): def commit():
Trade.query.session.commit() Trade.query.session.commit()
@staticmethod
def rollback():
Trade.query.session.rollback()
@staticmethod @staticmethod
def get_trades_proxy(*, pair: str = None, is_open: bool = None, def get_trades_proxy(*, pair: str = None, is_open: bool = None,
open_date: datetime = None, close_date: datetime = None, open_date: datetime = None, close_date: datetime = None,

View File

@ -18,9 +18,9 @@ def get_rpc_optional() -> Optional[RPC]:
def get_rpc() -> Optional[Iterator[RPC]]: def get_rpc() -> Optional[Iterator[RPC]]:
_rpc = get_rpc_optional() _rpc = get_rpc_optional()
if _rpc: if _rpc:
Trade.query.session.rollback() Trade.rollback()
yield _rpc yield _rpc
Trade.query.session.rollback() Trade.rollback()
else: else:
raise RPCException('Bot is not in the correct state') raise RPCException('Bot is not in the correct state')

View File

@ -876,7 +876,7 @@ class RPC:
lock.active = False lock.active = False
lock.lock_end_time = datetime.now(timezone.utc) lock.lock_end_time = datetime.now(timezone.utc)
PairLock.query.session.commit() Trade.commit()
return self._rpc_locks() return self._rpc_locks()

View File

@ -70,7 +70,7 @@ def authorized_only(command_handler: Callable[..., None]) -> Callable[..., Any]:
) )
return wrapper return wrapper
# Rollback session to avoid getting data stored in a transaction. # Rollback session to avoid getting data stored in a transaction.
Trade.query.session.rollback() Trade.rollback()
logger.debug( logger.debug(
'Executing handler: %s for chat_id: %s', 'Executing handler: %s for chat_id: %s',
command_handler.__name__, command_handler.__name__,

View File

@ -2375,6 +2375,7 @@ def test_Trade_object_idem():
'delete', 'delete',
'session', 'session',
'commit', 'commit',
'rollback',
'query', 'query',
'open_date', 'open_date',
'get_best_pair', 'get_best_pair',