support multiple db transitions by keeping the backup-table dynamic

This commit is contained in:
xmatthias 2018-06-27 20:15:25 +02:00
parent 860b270e30
commit d5ad066f8d
2 changed files with 18 additions and 4 deletions

View File

@ -77,6 +77,13 @@ def check_migrate(engine) -> None:
inspector = inspect(engine) inspector = inspect(engine)
cols = inspector.get_columns('trades') 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 # Check for latest column
if not has_column(cols, 'max_rate'): 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') max_rate = get_column_def(cols, 'max_rate', '0.0')
# Schema migration necessary # 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 # let SQLAlchemy create the schema as required
_DECL_BASE.metadata.create_all(engine) _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, stake_amount, amount, open_date, close_date, open_order_id,
{stop_loss} stop_loss, {initial_stop_loss} initial_stop_loss, {stop_loss} stop_loss, {initial_stop_loss} initial_stop_loss,
{max_rate} max_rate {max_rate} max_rate
from {table_back_name}
from trades_bak
""") """)
# Reread columns - the above recreated the table! # Reread columns - the above recreated the table!

View File

@ -7,6 +7,7 @@ from sqlalchemy import create_engine
from freqtrade import constants, OperationalException from freqtrade import constants, OperationalException
from freqtrade.persistence import Trade, init, clean_dry_run_db from freqtrade.persistence import Trade, init, clean_dry_run_db
from freqtrade.tests.conftest import log_has
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -405,7 +406,7 @@ def test_migrate_old(mocker, default_conf, fee):
assert trade.initial_stop_loss == 0.0 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) 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 # Create table using the old format
engine.execute(create_table_old) engine.execute(create_table_old)
engine.execute(insert_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 # Run init to test migration
init(default_conf) 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.stake_amount == default_conf.get("stake_amount")
assert trade.pair == "ETC/BTC" assert trade.pair == "ETC/BTC"
assert trade.exchange == "binance" 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): def test_adjust_stop_loss(limit_buy_order, limit_sell_order, fee):