-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerifyTool.py
More file actions
45 lines (40 loc) · 1.4 KB
/
VerifyTool.py
File metadata and controls
45 lines (40 loc) · 1.4 KB
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
#!/usr/bin/env python3
import sqlite3
import os
import shutil
# Path to your SQLite database
DB_PATH = os.path.abspath(os.path.join(os.getcwd(), "web_index.db"))
BAK_PATH = os.path.abspath(os.path.join(os.getcwd(), "web_index.bak.db"))
def verify_or_rotate():
"""
If web_index.db exists, run PRAGMA integrity_check.
If the result isn’t “ok,” rename web_index.db → web_index.bak.db
so a fresh DB will be created on next startup.
"""
if not os.path.exists(DB_PATH):
# No DB to check
return
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("PRAGMA integrity_check;")
res = cursor.fetchone()
conn.close()
if res[0].lower() != "ok":
print(f"[WARN] integrity_check failed: {res[0]}")
if os.path.exists(BAK_PATH):
os.remove(BAK_PATH)
shutil.move(DB_PATH, BAK_PATH)
print(f"[INFO] Rotated corrupt DB to: {BAK_PATH}")
else:
# Integrity OK
pass
except sqlite3.DatabaseError as e:
# If we can’t even do integrity_check, rotate anyway
print(f"[ERROR] integrity_check error: {e}")
if os.path.exists(BAK_PATH):
os.remove(BAK_PATH)
shutil.move(DB_PATH, BAK_PATH)
print(f"[INFO] Rotated corrupt DB to: {BAK_PATH}")
if __name__ == "__main__":
verify_or_rotate()