Add sequence migration

This commit is contained in:
Matthias 2022-05-10 07:13:51 +02:00
parent 269630e755
commit 31cce741ac
2 changed files with 21 additions and 1 deletions

View File

@ -1,8 +1,12 @@
import logging
from typing import Any, Dict
from sqlalchemy import func
from freqtrade.configuration.config_setup import setup_utils_configuration
from freqtrade.enums.runmode import RunMode
from freqtrade.persistence.migrations import set_sequence_ids
from freqtrade.persistence.trade_model import Order
logger = logging.getLogger(__name__)
@ -19,6 +23,7 @@ def start_convert_db(args: Dict[str, Any]) -> None:
init_db(config['db_url'], False)
session_target = Trade._session
init_db(config['db_url_from'], False)
logger.info("Starting db migration.")
# print(f"{id(sessionA)=}, {id(sessionB)=}")
trade_count = 0
@ -30,6 +35,7 @@ def start_convert_db(args: Dict[str, Any]) -> None:
make_transient(o)
session_target.add(trade)
session_target.commit()
for pairlock in PairLock.query:
@ -37,4 +43,15 @@ def start_convert_db(args: Dict[str, Any]) -> None:
make_transient(pairlock)
session_target.add(pairlock)
session_target.commit()
# Update sequences
max_trade_id = session_target.query(func.max(Trade.id)).scalar()
max_order_id = session_target.query(func.max(Order.id)).scalar()
max_pairlock_id = session_target.query(func.max(PairLock.id)).scalar()
set_sequence_ids(session_target.get_bind(),
trade_id=max_trade_id,
order_id=max_order_id,
pairlock_id=max_pairlock_id)
logger.info(f"Migrated {trade_count} Trades, and {pairlock_count} Pairlocks.")

View File

@ -46,7 +46,7 @@ def get_last_sequence_ids(engine, trade_back_name, order_back_name):
return order_id, trade_id
def set_sequence_ids(engine, order_id, trade_id):
def set_sequence_ids(engine, order_id, trade_id, pairlock_id=None):
if engine.name == 'postgresql':
with engine.begin() as connection:
@ -54,6 +54,9 @@ def set_sequence_ids(engine, order_id, trade_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}"))
if pairlock_id:
connection.execute(
text(f"ALTER SEQUENCE pairlocks_id_seq RESTART WITH {pairlock_id}"))
def drop_index_on_table(engine, inspector, table_bak_name):