Update orders-migrations to work with new sqlalchemy syntax

This commit is contained in:
Matthias 2021-05-23 08:34:58 +02:00
parent 17f74f7da8
commit b82f7a2dfd
2 changed files with 56 additions and 52 deletions

View File

@ -128,23 +128,25 @@ def migrate_open_orders_to_trades(engine):
def migrate_orders_table(decl_base, inspector, engine, table_back_name: str, cols: List):
# Schema migration necessary
engine.execute(f"alter table orders rename to {table_back_name}")
with engine.begin() as connection:
connection.execute(text(f"alter table orders rename to {table_back_name}"))
# drop indexes on backup table
for index in inspector.get_indexes(table_back_name):
engine.execute(f"drop index {index['name']}")
connection.execute(text(f"drop index {index['name']}"))
# let SQLAlchemy create the schema as required
decl_base.metadata.create_all(engine)
engine.execute(f"""
insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status,
symbol, order_type, side, price, amount, filled, average, remaining, cost, order_date,
order_filled_date, order_update_date)
select id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status,
symbol, order_type, side, price, amount, filled, null average, remaining, cost, order_date,
order_filled_date, order_update_date
with engine.begin() as connection:
connection.execute(text(f"""
insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id,
status, symbol, order_type, side, price, amount, filled, average, remaining, cost,
order_date, order_filled_date, order_update_date)
select id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id,
status, symbol, order_type, side, price, amount, filled, null average, remaining, cost,
order_date, order_filled_date, order_update_date
from {table_back_name}
""")
"""))
def check_migrate(engine, decl_base, previous_tables) -> None:

View File

@ -631,13 +631,15 @@ def test_migrate_new(mocker, default_conf, fee, caplog):
caplog.clear()
# Drop latest column
engine.execute("alter table orders rename to orders_bak")
with engine.begin() as connection:
connection.execute(text("alter table orders rename to orders_bak"))
inspector = inspect(engine)
with engine.begin() as connection:
for index in inspector.get_indexes('orders_bak'):
engine.execute(f"drop index {index['name']}")
connection.execute(text(f"drop index {index['name']}"))
# Recreate table
engine.execute("""
connection.execute(text("""
CREATE TABLE orders (
id INTEGER NOT NULL,
ft_trade_id INTEGER,
@ -661,9 +663,9 @@ def test_migrate_new(mocker, default_conf, fee, caplog):
CONSTRAINT _order_pair_order_id UNIQUE (ft_pair, order_id),
FOREIGN KEY(ft_trade_id) REFERENCES trades (id)
)
""")
"""))
engine.execute("""
connection.execute(text("""
insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status,
symbol, order_type, side, price, amount, filled, remaining, cost, order_date,
order_filled_date, order_update_date)
@ -671,7 +673,7 @@ def test_migrate_new(mocker, default_conf, fee, caplog):
symbol, order_type, side, price, amount, filled, remaining, cost, order_date,
order_filled_date, order_update_date
from orders_bak
""")
"""))
# Run init to test migration
init_db(default_conf['db_url'], default_conf['dry_run'])