Skip to content

Commit 1f04314

Browse files
committed
Add script to keep 8 random prospects
Introduce a maintenance script (app/api/prospects/sql/keep_random_8_prospects.py) that selects 8 random prospect IDs, prints them, then deletes all other prospect records and associated llm rows using a direct DB connection from app.utils.db.get_db_connection_direct. The script modifies sys.path to locate the app package and commits the destructive changes; intended for one-off cleanup/maintenance.
1 parent 05e6e5b commit 1f04314

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import sys
3+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../..')))
4+
from app.utils.db import get_db_connection_direct
5+
6+
if __name__ == "__main__":
7+
conn = get_db_connection_direct()
8+
cur = conn.cursor()
9+
10+
# Step 1: Select 8 random ids
11+
cur.execute("SELECT id FROM prospects ORDER BY RANDOM() LIMIT 8;")
12+
ids = [row[0] for row in cur.fetchall()]
13+
print(f"Keeping these 8 ids: {ids}")
14+
15+
# Step 2: Cascade delete llm records, then delete prospects
16+
if ids:
17+
format_strings = ','.join(['%s'] * len(ids))
18+
# First, delete llm records referencing prospects that will be deleted
19+
delete_llm_sql = f"DELETE FROM llm WHERE prospect_id NOT IN ({format_strings});"
20+
cur.execute(delete_llm_sql, ids)
21+
# Now delete the prospects
22+
delete_sql = f"DELETE FROM prospects WHERE id NOT IN ({format_strings});"
23+
cur.execute(delete_sql, ids)
24+
conn.commit()
25+
print(f"Deleted all prospects except ids: {ids}")
26+
else:
27+
print("No records found in prospects table.")
28+
29+
cur.close()
30+
conn.close()

0 commit comments

Comments
 (0)