Update sequences for postgres

This commit is contained in:
Matthias 2022-01-21 12:55:35 +01:00
parent 19948a6f89
commit c265f39323

View File

@ -28,30 +28,35 @@ def get_backup_name(tabs, backup_prefix: str):
return table_back_name return table_back_name
def get_last_sequence_ids(engine, inspector): def get_last_sequence_ids(engine, trade_back_name, order_back_name):
order_id: int = None order_id: int = None
trade_id: int = None trade_id: int = None
if engine.name == 'postgresql': if engine.name == 'postgresql':
with engine.begin() as connection: with engine.begin() as connection:
x = connection.execute( trade_id = connection.execute(text("select nextval('trades_id_seq')")).fetchone()[0]
text("select sequencename, last_value from pg_sequences")).fetchall() order_id = connection.execute(text("select nextval('orders_id_seq')")).fetchone()[0]
ts = [s[1]for s in x if s[0].startswith('trades_id') and s[1] is not None] with engine.begin() as connection:
os = [s[1] for s in x if s[0].startswith('orders_id') and s[1] is not None] connection.execute(text(
trade_id = max(ts) f"ALTER SEQUENCE orders_id_seq rename to {order_back_name}_id_seq_bak"))
order_id = max(os) connection.execute(text(
f"ALTER SEQUENCE trades_id_seq rename to {trade_back_name}_id_seq_bak"))
return order_id, trade_id return order_id, trade_id
def set_sequence_ids(engine, order_id, trade_id): def set_sequence_ids(engine, order_id, trade_id):
if engine.name == 'postgresql': if engine.name == 'postgresql':
pass with engine.begin() as connection:
if order_id:
connection.execute(text(f"ALTER SEQUENCE orders_id_seq RESTART WITH {order_id}"))
if trade_id:
connection.execute(text(f"ALTER SEQUENCE trades_id_seq RESTART WITH {trade_id}"))
def migrate_trades_and_orders_table( def migrate_trades_and_orders_table(
decl_base, inspector, engine, decl_base, inspector, engine,
table_back_name: str, cols: List, trade_back_name: str, cols: List,
order_back_name: str): order_back_name: str):
fee_open = get_column_def(cols, 'fee_open', 'fee') fee_open = get_column_def(cols, 'fee_open', 'fee')
fee_open_cost = get_column_def(cols, 'fee_open_cost', 'null') fee_open_cost = get_column_def(cols, 'fee_open_cost', 'null')
@ -88,14 +93,14 @@ def migrate_trades_and_orders_table(
# Schema migration necessary # Schema migration necessary
with engine.begin() as connection: with engine.begin() as connection:
connection.execute(text(f"alter table trades rename to {table_back_name}")) connection.execute(text(f"alter table trades rename to {trade_back_name}"))
with engine.begin() as connection: with engine.begin() as connection:
# drop indexes on backup table in new session # drop indexes on backup table in new session
for index in inspector.get_indexes(table_back_name): for index in inspector.get_indexes(trade_back_name):
connection.execute(text(f"drop index {index['name']}")) connection.execute(text(f"drop index {index['name']}"))
trade_id, order_id = get_last_sequence_ids(engine, inspector) order_id, trade_id = get_last_sequence_ids(engine, trade_back_name, order_back_name)
drop_orders_table(inspector, engine, order_back_name) drop_orders_table(inspector, engine, order_back_name)
@ -130,7 +135,7 @@ def migrate_trades_and_orders_table(
{sell_order_status} sell_order_status, {sell_order_status} sell_order_status,
{strategy} strategy, {buy_tag} buy_tag, {timeframe} timeframe, {strategy} strategy, {buy_tag} buy_tag, {timeframe} timeframe,
{open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs {open_trade_value} open_trade_value, {close_profit_abs} close_profit_abs
from {table_back_name} from {trade_back_name}
""")) """))
migrate_orders_table(decl_base, engine, order_back_name, cols) migrate_orders_table(decl_base, engine, order_back_name, cols)
@ -192,6 +197,7 @@ def check_migrate(engine, decl_base, previous_tables) -> None:
# Check if migration necessary # Check if migration necessary
# Migrates both trades and orders table! # Migrates both trades and orders table!
if not has_column(cols, 'buy_tag'):
logger.info(f'Running database migration for trades - backup: {table_back_name}') logger.info(f'Running database migration for trades - backup: {table_back_name}')
migrate_trades_and_orders_table( migrate_trades_and_orders_table(
decl_base, inspector, engine, table_back_name, cols, order_table_bak_name) decl_base, inspector, engine, table_back_name, cols, order_table_bak_name)