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}")
# drop indexes on backup table with engine.begin() as connection:
for index in inspector.get_indexes(table_back_name): connection.execute(text(f"alter table orders rename to {table_back_name}"))
engine.execute(f"drop index {index['name']}") # drop indexes on backup table
for index in inspector.get_indexes(table_back_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,47 +631,49 @@ 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)
for index in inspector.get_indexes('orders_bak'): with engine.begin() as connection:
engine.execute(f"drop index {index['name']}") for index in inspector.get_indexes('orders_bak'):
# Recreate table connection.execute(text(f"drop index {index['name']}"))
engine.execute(""" # Recreate table
CREATE TABLE orders ( connection.execute(text("""
id INTEGER NOT NULL, CREATE TABLE orders (
ft_trade_id INTEGER, id INTEGER NOT NULL,
ft_order_side VARCHAR NOT NULL, ft_trade_id INTEGER,
ft_pair VARCHAR NOT NULL, ft_order_side VARCHAR NOT NULL,
ft_is_open BOOLEAN NOT NULL, ft_pair VARCHAR NOT NULL,
order_id VARCHAR NOT NULL, ft_is_open BOOLEAN NOT NULL,
status VARCHAR, order_id VARCHAR NOT NULL,
symbol VARCHAR, status VARCHAR,
order_type VARCHAR, symbol VARCHAR,
side VARCHAR, order_type VARCHAR,
price FLOAT, side VARCHAR,
amount FLOAT, price FLOAT,
filled FLOAT, amount FLOAT,
remaining FLOAT, filled FLOAT,
cost FLOAT, remaining FLOAT,
order_date DATETIME, cost FLOAT,
order_filled_date DATETIME, order_date DATETIME,
order_update_date DATETIME, order_filled_date DATETIME,
PRIMARY KEY (id), order_update_date DATETIME,
CONSTRAINT _order_pair_order_id UNIQUE (ft_pair, order_id), PRIMARY KEY (id),
FOREIGN KEY(ft_trade_id) REFERENCES trades (id) 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, 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)
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, 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
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'])