import sqlite3, sys

db_path = '/root/search.db'

# Try WAL mode cleanup first
import os
for f in [db_path + '-wal', db_path + '-shm', db_path + '-journal']:
    if os.path.exists(f):
        os.remove(f)

# Connect with explicit settings
conn = sqlite3.connect(db_path, timeout=60)
conn.execute("PRAGMA busy_timeout=30000")
conn.execute("PRAGMA journal_mode=WAL")
print("Journal mode:", conn.execute("PRAGMA journal_mode").fetchone()[0])

# Test write
try:
    conn.execute("INSERT INTO gov_raw(site_name,title,page_url,publish_date) VALUES('test_py','test_py','test_py','2024-01-01')")
    conn.commit()
    print("WRITE OK")
    conn.execute("DELETE FROM gov_raw WHERE site_name='test_py'")
    conn.commit()
    print("DELETE OK")
except Exception as e:
    print(f"Error: {e}")
    # Try to get more info
    try:
        conn.execute("PRAGMA integrity_check")
        rows = conn.fetchall()
        print(f"Integrity: {rows}")
    except Exception as e2:
        print(f"Integrity check error: {e2}")
finally:
    conn.close()
