Extend safe schema patches for legacy bot models
Этот коммит содержится в:
+31
-27
@@ -29,32 +29,36 @@ def init_db() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def apply_safe_schema_patches() -> 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)
|
inspector = inspect(engine)
|
||||||
if 'bot_state' in inspector.get_table_names():
|
table_names = inspector.get_table_names()
|
||||||
existing = {column['name'] for column in inspector.get_columns('bot_state')}
|
if 'bot_state' in table_names:
|
||||||
patches = []
|
_add_missing_columns('bot_state', {
|
||||||
if 'live_acknowledged' not in existing:
|
'live_acknowledged': "BOOLEAN NOT NULL DEFAULT FALSE",
|
||||||
patches.append("ALTER TABLE bot_state ADD COLUMN live_acknowledged BOOLEAN NOT NULL DEFAULT FALSE")
|
'emergency_stop': "BOOLEAN NOT NULL DEFAULT FALSE",
|
||||||
if 'emergency_stop' not in existing:
|
'trade_mode': "VARCHAR(16) NOT NULL DEFAULT 'demo'",
|
||||||
patches.append("ALTER TABLE bot_state ADD COLUMN emergency_stop BOOLEAN NOT NULL DEFAULT FALSE")
|
'enabled': "BOOLEAN NOT NULL DEFAULT FALSE",
|
||||||
if 'trade_mode' not in existing:
|
'trade_style_mode': "VARCHAR(32) NOT NULL DEFAULT 'balanced'",
|
||||||
patches.append("ALTER TABLE bot_state ADD COLUMN trade_mode VARCHAR(16) NOT NULL DEFAULT 'demo'")
|
'min_signal_score': "FLOAT NOT NULL DEFAULT 65.0",
|
||||||
if 'enabled' not in existing:
|
'max_open_positions': "INTEGER NOT NULL DEFAULT 3",
|
||||||
patches.append("ALTER TABLE bot_state ADD COLUMN enabled BOOLEAN NOT NULL DEFAULT FALSE")
|
'max_quote_per_trade': "FLOAT NOT NULL DEFAULT 100.0",
|
||||||
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 'demo_positions' in table_names:
|
||||||
if 'min_signal_score' not in existing:
|
_add_missing_columns('demo_positions', {
|
||||||
patches.append("ALTER TABLE bot_state ADD COLUMN min_signal_score FLOAT NOT NULL DEFAULT 65.0")
|
'side': "VARCHAR(8) NOT NULL DEFAULT 'long'",
|
||||||
if 'max_open_positions' not in existing:
|
'current_price': "FLOAT NOT NULL DEFAULT 0.0",
|
||||||
patches.append("ALTER TABLE bot_state ADD COLUMN max_open_positions INTEGER NOT NULL DEFAULT 3")
|
'take_profit': "FLOAT NOT NULL DEFAULT 0.0",
|
||||||
if 'max_quote_per_trade' not in existing:
|
'stop_loss': "FLOAT NOT NULL DEFAULT 0.0",
|
||||||
patches.append("ALTER TABLE bot_state ADD COLUMN max_quote_per_trade FLOAT NOT NULL DEFAULT 100.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:
|
with engine.begin() as conn:
|
||||||
for patch in patches:
|
for name, ddl in columns.items():
|
||||||
conn.execute(text(patch))
|
if name not in existing:
|
||||||
|
conn.execute(text(f'ALTER TABLE {table_name} ADD COLUMN {name} {ddl}'))
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user