Files
kis_bot/scratch/delete_old_data.py

45 lines
1.6 KiB
Python

import sys
from kis_trader.database.db_manager import TradeDB
db = TradeDB()
try:
rows = db.conn.execute("SHOW TABLES").fetchall()
tables = [list(r.values())[0] for r in rows]
target_prefixes = (
'ws_ticks', 'ws_orderbook', 'ws_candles',
'ls_ws_ticks', 'ls_ws_orderbook', 'ls_ws_candles',
'ws_price_validation'
)
for table in tables:
if not table.startswith(target_prefixes):
continue
cols = [dict(r)["Field"] for r in db.conn.execute(f"SHOW COLUMNS FROM {table}").fetchall()]
time_col = None
for candidate in ('tick_time', 'ob_time', 'candle_time', 'timestamp', 'recv_ts'):
if candidate in cols:
time_col = candidate
break
if time_col:
# We want to delete data before 2026-08-08.
cutoff = '20260808'
# Print count before deletion
count_q = f"SELECT COUNT(*) as cnt FROM {table} WHERE {time_col} < %s"
count = db.conn.execute(count_q, (cutoff,)).fetchone()['cnt']
if count > 0:
print(f"Deleting {count} rows from {table} (using column {time_col})")
del_q = f"DELETE FROM {table} WHERE {time_col} < %s"
db.conn.execute(del_q, (cutoff,))
db.conn.commit()
else:
print(f"No rows to delete in {table} before {cutoff}")
else:
print(f"Warning: No time column found for {table}, columns: {cols}")
finally:
db.close()