File size: 1,514 Bytes
dd6cc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Task 1: Soft-delete units with topic IN (physics, thermodynamics, mechanics)."""
import argparse
import sqlite3
import sys

DB_PATH = "math_wiki.db"
OFF_TOPIC = ("physics", "thermodynamics", "mechanics")


def main(dry_run: bool) -> None:
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row

    rows = conn.execute(
        f"SELECT id, topic, subtopic, content FROM wiki_units "
        f"WHERE topic IN ({','.join('?'*len(OFF_TOPIC))}) AND deleted=0",
        OFF_TOPIC,
    ).fetchall()

    print(f"Found {len(rows)} off-topic units to soft-delete:")
    for r in rows:
        print(f"  [{r['topic']}] {r['id']}: {r['content'][:70]!r}")

    if dry_run:
        print("\nDRY RUN — no changes made.")
        return

    ids = [r["id"] for r in rows]
    conn.execute(
        f"UPDATE wiki_units SET deleted=1 WHERE id IN ({','.join('?'*len(ids))})",
        ids,
    )
    conn.commit()

    before = conn.execute(
        f"SELECT COUNT(*) FROM wiki_units WHERE topic IN ({','.join('?'*len(OFF_TOPIC))}) AND deleted=0",
        OFF_TOPIC,
    ).fetchone()[0]
    after_deleted = conn.execute("SELECT COUNT(*) FROM wiki_units WHERE deleted=1").fetchone()[0]

    print(f"\nDone. Off-topic active rows remaining: {before} (expected 0)")
    print(f"Total deleted rows: {after_deleted}")
    conn.close()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--dry-run", action="store_true")
    args = parser.parse_args()
    main(args.dry_run)