update db migrate script to work for more changes
This commit is contained in:
parent
8ecdae67e1
commit
860b270e30
@ -66,6 +66,10 @@ def has_column(columns, searchname: str) -> bool:
|
|||||||
return len(list(filter(lambda x: x["name"] == searchname, columns))) == 1
|
return len(list(filter(lambda x: x["name"] == searchname, columns))) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def get_column_def(columns, column: str, default: str) -> str:
|
||||||
|
return default if not has_column(columns, column) else column
|
||||||
|
|
||||||
|
|
||||||
def check_migrate(engine) -> None:
|
def check_migrate(engine) -> None:
|
||||||
"""
|
"""
|
||||||
Checks if migration is necessary and migrates if necessary
|
Checks if migration is necessary and migrates if necessary
|
||||||
@ -74,17 +78,26 @@ def check_migrate(engine) -> None:
|
|||||||
|
|
||||||
cols = inspector.get_columns('trades')
|
cols = inspector.get_columns('trades')
|
||||||
|
|
||||||
if not has_column(cols, 'fee_open'):
|
# Check for latest column
|
||||||
|
if not has_column(cols, 'max_rate'):
|
||||||
|
open_rate_requested = get_column_def(cols, 'open_rate_requested', 'null')
|
||||||
|
close_rate_requested = get_column_def(cols, 'close_rate_requested', 'null')
|
||||||
|
stop_loss = get_column_def(cols, 'stop_loss', '0.0')
|
||||||
|
initial_stop_loss = get_column_def(cols, 'initial_stop_loss', '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("alter table trades rename to trades_bak")
|
||||||
# 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)
|
||||||
|
|
||||||
# Copy data back - following the correct schema
|
# Copy data back - following the correct schema
|
||||||
engine.execute("""insert into trades
|
engine.execute(f"""insert into trades
|
||||||
(id, exchange, pair, is_open, fee_open, fee_close, open_rate,
|
(id, exchange, pair, is_open, fee_open, fee_close, open_rate,
|
||||||
open_rate_requested, close_rate, close_rate_requested, close_profit,
|
open_rate_requested, close_rate, close_rate_requested, close_profit,
|
||||||
stake_amount, amount, open_date, close_date, open_order_id)
|
stake_amount, amount, open_date, close_date, open_order_id,
|
||||||
|
stop_loss, initial_stop_loss, max_rate
|
||||||
|
)
|
||||||
select id, lower(exchange),
|
select id, lower(exchange),
|
||||||
case
|
case
|
||||||
when instr(pair, '_') != 0 then
|
when instr(pair, '_') != 0 then
|
||||||
@ -94,9 +107,12 @@ def check_migrate(engine) -> None:
|
|||||||
end
|
end
|
||||||
pair,
|
pair,
|
||||||
is_open, fee fee_open, fee fee_close,
|
is_open, fee fee_open, fee fee_close,
|
||||||
open_rate, null open_rate_requested, close_rate,
|
open_rate, {open_rate_requested} open_rate_requested, close_rate,
|
||||||
null close_rate_requested, close_profit,
|
{close_rate_requested} close_rate_requested, close_profit,
|
||||||
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,
|
||||||
|
{max_rate} max_rate
|
||||||
|
|
||||||
from trades_bak
|
from trades_bak
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@ -104,11 +120,6 @@ def check_migrate(engine) -> None:
|
|||||||
inspector = inspect(engine)
|
inspector = inspect(engine)
|
||||||
cols = inspector.get_columns('trades')
|
cols = inspector.get_columns('trades')
|
||||||
|
|
||||||
if not has_column(cols, 'open_rate_requested'):
|
|
||||||
engine.execute("alter table trades add open_rate_requested float")
|
|
||||||
if not has_column(cols, 'close_rate_requested'):
|
|
||||||
engine.execute("alter table trades add close_rate_requested float")
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup() -> None:
|
def cleanup() -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -400,6 +400,9 @@ def test_migrate_old(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 == "bittrex"
|
assert trade.exchange == "bittrex"
|
||||||
|
assert trade.max_rate == 0.0
|
||||||
|
assert trade.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):
|
||||||
|
Loading…
Reference in New Issue
Block a user