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): def migrate_orders_table(decl_base, inspector, engine, table_back_name: str, cols: List):
# Schema migration necessary # 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 # drop indexes on backup table
for index in inspector.get_indexes(table_back_name): 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 # let SQLAlchemy create the schema as required
decl_base.metadata.create_all(engine) decl_base.metadata.create_all(engine)
with engine.begin() as connection:
engine.execute(f""" connection.execute(text(f"""
insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status, insert into orders ( id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id,
symbol, order_type, side, price, amount, filled, average, remaining, cost, order_date, status, symbol, order_type, side, price, amount, filled, average, remaining, cost,
order_filled_date, order_update_date) order_date, order_filled_date, order_update_date)
select id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id, status, select id, ft_trade_id, ft_order_side, ft_pair, ft_is_open, order_id,
symbol, order_type, side, price, amount, filled, null average, remaining, cost, order_date, status, symbol, order_type, side, price, amount, filled, null average, remaining, cost,
order_filled_date, order_update_date order_date, order_filled_date, order_update_date
from {table_back_name} from {table_back_name}
""") """))
def check_migrate(engine, decl_base, previous_tables) -> None: 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() caplog.clear()
# Drop latest column # 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) inspector = inspect(engine)
with engine.begin() as connection:
for index in inspector.get_indexes('orders_bak'): 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 # Recreate table
engine.execute(""" connection.execute(text("""
CREATE TABLE orders ( CREATE TABLE orders (
id INTEGER NOT NULL, id INTEGER NOT NULL,
ft_trade_id INTEGER, 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), CONSTRAINT _order_pair_order_id UNIQUE (ft_pair, order_id),
FOREIGN KEY(ft_trade_id) REFERENCES trades (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, 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, symbol, order_type, side, price, amount, filled, remaining, cost, order_date,
order_filled_date, order_update_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, symbol, order_type, side, price, amount, filled, remaining, cost, order_date,
order_filled_date, order_update_date order_filled_date, order_update_date
from orders_bak from orders_bak
""") """))
# Run init to test migration # Run init to test migration
init_db(default_conf['db_url'], default_conf['dry_run']) init_db(default_conf['db_url'], default_conf['dry_run'])