Extend safe schema patches for legacy bot models
Этот коммит содержится в:
+31
-27
@@ -29,32 +29,36 @@ def init_db() -> None:
|
||||
|
||||
|
||||
def apply_safe_schema_patches() -> None:
|
||||
"""Tiny compatibility layer for the early MVP.
|
||||
|
||||
SQLAlchemy create_all creates missing tables but does not alter existing ones.
|
||||
During rapid MVP iterations this keeps old server databases from breaking
|
||||
when a new column is added.
|
||||
"""
|
||||
inspector = inspect(engine)
|
||||
if 'bot_state' in inspector.get_table_names():
|
||||
existing = {column['name'] for column in inspector.get_columns('bot_state')}
|
||||
patches = []
|
||||
if 'live_acknowledged' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN live_acknowledged BOOLEAN NOT NULL DEFAULT FALSE")
|
||||
if 'emergency_stop' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN emergency_stop BOOLEAN NOT NULL DEFAULT FALSE")
|
||||
if 'trade_mode' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN trade_mode VARCHAR(16) NOT NULL DEFAULT 'demo'")
|
||||
if 'enabled' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN enabled BOOLEAN NOT NULL DEFAULT FALSE")
|
||||
if 'trade_style_mode' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN trade_style_mode VARCHAR(32) NOT NULL DEFAULT 'balanced'")
|
||||
if 'min_signal_score' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN min_signal_score FLOAT NOT NULL DEFAULT 65.0")
|
||||
if 'max_open_positions' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN max_open_positions INTEGER NOT NULL DEFAULT 3")
|
||||
if 'max_quote_per_trade' not in existing:
|
||||
patches.append("ALTER TABLE bot_state ADD COLUMN max_quote_per_trade FLOAT NOT NULL DEFAULT 100.0")
|
||||
table_names = inspector.get_table_names()
|
||||
if 'bot_state' in table_names:
|
||||
_add_missing_columns('bot_state', {
|
||||
'live_acknowledged': "BOOLEAN NOT NULL DEFAULT FALSE",
|
||||
'emergency_stop': "BOOLEAN NOT NULL DEFAULT FALSE",
|
||||
'trade_mode': "VARCHAR(16) NOT NULL DEFAULT 'demo'",
|
||||
'enabled': "BOOLEAN NOT NULL DEFAULT FALSE",
|
||||
'trade_style_mode': "VARCHAR(32) NOT NULL DEFAULT 'balanced'",
|
||||
'min_signal_score': "FLOAT NOT NULL DEFAULT 65.0",
|
||||
'max_open_positions': "INTEGER NOT NULL DEFAULT 3",
|
||||
'max_quote_per_trade': "FLOAT NOT NULL DEFAULT 100.0",
|
||||
})
|
||||
if 'demo_positions' in table_names:
|
||||
_add_missing_columns('demo_positions', {
|
||||
'side': "VARCHAR(8) NOT NULL DEFAULT 'long'",
|
||||
'current_price': "FLOAT NOT NULL DEFAULT 0.0",
|
||||
'take_profit': "FLOAT NOT NULL DEFAULT 0.0",
|
||||
'stop_loss': "FLOAT NOT NULL DEFAULT 0.0",
|
||||
'unrealized_pnl': "FLOAT NOT NULL DEFAULT 0.0",
|
||||
'unrealized_pnl_pct': "FLOAT NOT NULL DEFAULT 0.0",
|
||||
'opened_at': "TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL",
|
||||
'closed_at': "TIMESTAMP WITH TIME ZONE NULL",
|
||||
})
|
||||
|
||||
|
||||
def _add_missing_columns(table_name: str, columns: dict[str, str]) -> None:
|
||||
inspector = inspect(engine)
|
||||
existing = {column['name'] for column in inspector.get_columns(table_name)}
|
||||
with engine.begin() as conn:
|
||||
for patch in patches:
|
||||
conn.execute(text(patch))
|
||||
for name, ddl in columns.items():
|
||||
if name not in existing:
|
||||
conn.execute(text(f'ALTER TABLE {table_name} ADD COLUMN {name} {ddl}'))
|
||||
|
||||
Ссылка в новой задаче
Block a user