Files
kis_trader/scratch/delete_old_data_final.py

54 lines
2.0 KiB
Python

import sys
import time
from database import TradeDB
db = TradeDB()
try:
# TradeDB in database.py inherits or delegates to _MariaDBConn, which has .execute(sql)
tables = [list(r.values())[0] for r in db.execute("SHOW TABLES").fetchall()]
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.execute(f"SHOW COLUMNS FROM {table}").fetchall()]
time_col = None
for candidate in ('tick_time', 'ob_time', 'candle_time', 'timestamp', 'recv_ts', 'datetime'):
if candidate in cols:
time_col = candidate
break
if time_col:
cutoff = '20260808'
# Print count before deletion
count_q = f"SELECT COUNT(*) as cnt FROM {table} WHERE {time_col} < %s"
count = db.execute(count_q, (cutoff,)).fetchone()['cnt']
if count > 0:
print(f"Deleting {count} rows from {table} (using column {time_col})")
deleted = 0
while True:
del_q = f"DELETE FROM {table} WHERE {time_col} < %s LIMIT 100000"
cur = db.execute(del_q, (cutoff,))
rows_affected = cur.rowcount
if rows_affected <= 0:
break
deleted += rows_affected
print(f" ... deleted {deleted}/{count} rows in {table}")
time.sleep(0.1)
print(f"Finished deleting from {table}.")
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()