Rollback after each request

This closes the transaction and avoids "sticking" transactions.
This commit is contained in:
Matthias 2021-10-17 08:36:11 +02:00
parent 89ca8abea9
commit fb2c8f7621
3 changed files with 5 additions and 10 deletions

View File

@ -1,4 +1,4 @@
from typing import Any, Dict, Optional from typing import Any, Dict, Iterator, Optional
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.rpc.rpc import RPC, RPCException from freqtrade.rpc.rpc import RPC, RPCException
@ -12,11 +12,12 @@ def get_rpc_optional() -> Optional[RPC]:
return None return None
def get_rpc() -> Optional[RPC]: def get_rpc() -> Optional[Iterator[RPC]]:
_rpc = get_rpc_optional() _rpc = get_rpc_optional()
if _rpc: if _rpc:
Trade.query.session.rollback() Trade.query.session.rollback()
return _rpc yield _rpc
Trade.query.session.rollback()
else: else:
raise RPCException('Bot is not in the correct state') raise RPCException('Bot is not in the correct state')

View File

@ -227,7 +227,6 @@ def create_mock_trades(fee, use_db: bool = True):
if use_db: if use_db:
Trade.commit() Trade.commit()
Trade.query.session.flush()
def create_mock_trades_usdt(fee, use_db: bool = True): def create_mock_trades_usdt(fee, use_db: bool = True):
@ -261,7 +260,6 @@ def create_mock_trades_usdt(fee, use_db: bool = True):
if use_db: if use_db:
Trade.commit() Trade.commit()
Trade.query.session.flush()
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)

View File

@ -570,7 +570,6 @@ def test_api_trades(botclient, mocker, fee, markets):
assert rc.json()['total_trades'] == 0 assert rc.json()['total_trades'] == 0
create_mock_trades(fee) create_mock_trades(fee)
Trade.query.session.flush()
rc = client_get(client, f"{BASE_URI}/trades") rc = client_get(client, f"{BASE_URI}/trades")
assert_response(rc) assert_response(rc)
@ -597,7 +596,6 @@ def test_api_trade_single(botclient, mocker, fee, ticker, markets):
assert rc.json()['detail'] == 'Trade not found.' assert rc.json()['detail'] == 'Trade not found.'
create_mock_trades(fee) create_mock_trades(fee)
Trade.query.session.flush()
rc = client_get(client, f"{BASE_URI}/trade/3") rc = client_get(client, f"{BASE_URI}/trade/3")
assert_response(rc) assert_response(rc)
@ -694,7 +692,6 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
assert rc.json() == {"error": "Error querying /api/v1/edge: Edge is not enabled."} assert rc.json() == {"error": "Error querying /api/v1/edge: Edge is not enabled."}
@pytest.mark.usefixtures("init_persistence")
def test_api_profit(botclient, mocker, ticker, fee, markets): def test_api_profit(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
@ -745,7 +742,6 @@ def test_api_profit(botclient, mocker, ticker, fee, markets):
} }
@pytest.mark.usefixtures("init_persistence")
def test_api_stats(botclient, mocker, ticker, fee, markets,): def test_api_stats(botclient, mocker, ticker, fee, markets,):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
@ -811,7 +807,7 @@ def test_api_performance(botclient, fee):
trade.close_profit_abs = trade.calc_profit() trade.close_profit_abs = trade.calc_profit()
Trade.query.session.add(trade) Trade.query.session.add(trade)
Trade.query.session.flush() Trade.commit()
rc = client_get(client, f"{BASE_URI}/performance") rc = client_get(client, f"{BASE_URI}/performance")
assert_response(rc) assert_response(rc)