support multiple db transitions by keeping the backup-table dynamic
This commit is contained in:
parent
860b270e30
commit
d5ad066f8d
@ -77,6 +77,13 @@ def check_migrate(engine) -> None:
|
||||
inspector = inspect(engine)
|
||||
|
||||
cols = inspector.get_columns('trades')
|
||||
tabs = inspector.get_table_names()
|
||||
table_back_name = 'trades_bak'
|
||||
i = 0
|
||||
while table_back_name in tabs:
|
||||
i += 1
|
||||
table_back_name = f'trades_bak{i}'
|
||||
logger.info(f'trying {table_back_name}')
|
||||
|
||||
# Check for latest column
|
||||
if not has_column(cols, 'max_rate'):
|
||||
@ -87,7 +94,7 @@ def check_migrate(engine) -> None:
|
||||
max_rate = get_column_def(cols, 'max_rate', '0.0')
|
||||
|
||||
# Schema migration necessary
|
||||
engine.execute("alter table trades rename to trades_bak")
|
||||
engine.execute(f"alter table trades rename to {table_back_name}")
|
||||
# let SQLAlchemy create the schema as required
|
||||
_DECL_BASE.metadata.create_all(engine)
|
||||
|
||||
@ -112,8 +119,7 @@ def check_migrate(engine) -> None:
|
||||
stake_amount, amount, open_date, close_date, open_order_id,
|
||||
{stop_loss} stop_loss, {initial_stop_loss} initial_stop_loss,
|
||||
{max_rate} max_rate
|
||||
|
||||
from trades_bak
|
||||
from {table_back_name}
|
||||
""")
|
||||
|
||||
# Reread columns - the above recreated the table!
|
||||
|
@ -7,6 +7,7 @@ from sqlalchemy import create_engine
|
||||
|
||||
from freqtrade import constants, OperationalException
|
||||
from freqtrade.persistence import Trade, init, clean_dry_run_db
|
||||
from freqtrade.tests.conftest import log_has
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
@ -405,7 +406,7 @@ def test_migrate_old(mocker, default_conf, fee):
|
||||
assert trade.initial_stop_loss == 0.0
|
||||
|
||||
|
||||
def test_migrate_new(mocker, default_conf, fee):
|
||||
def test_migrate_new(mocker, default_conf, fee, caplog):
|
||||
"""
|
||||
Test Database migration (starting with new pairformat)
|
||||
"""
|
||||
@ -442,6 +443,9 @@ def test_migrate_new(mocker, default_conf, fee):
|
||||
# Create table using the old format
|
||||
engine.execute(create_table_old)
|
||||
engine.execute(insert_table_old)
|
||||
|
||||
# fake previous backup
|
||||
engine.execute("create table trades_bak as select * from trades")
|
||||
# Run init to test migration
|
||||
init(default_conf)
|
||||
|
||||
@ -456,6 +460,10 @@ def test_migrate_new(mocker, default_conf, fee):
|
||||
assert trade.stake_amount == default_conf.get("stake_amount")
|
||||
assert trade.pair == "ETC/BTC"
|
||||
assert trade.exchange == "binance"
|
||||
assert trade.max_rate == 0.0
|
||||
assert trade.stop_loss == 0.0
|
||||
assert trade.initial_stop_loss == 0.0
|
||||
assert log_has("trying trades_bak1", caplog.record_tuples)
|
||||
|
||||
|
||||
def test_adjust_stop_loss(limit_buy_order, limit_sell_order, fee):
|
||||
|
Loading…
Reference in New Issue
Block a user