function check_and_fix_corruption
Scans a SQLite database for corrupted chat_session_id values in the text_sections table and automatically fixes them by setting invalid entries to NULL.
/tf/active/vicechatdev/vice_ai/direct_corruption_checker.py
7 - 82
moderate
Purpose
This function performs database maintenance by identifying and correcting corrupted chat_session_id values that may have been stored as empty strings, '{}', '[]', or 'null' strings. It provides detailed reporting of corruption status, counts affected rows, performs the fix operation, and verifies the results. This is useful for data integrity maintenance in applications using the vice_ai documents database.
Source Code
def check_and_fix_corruption():
db_path = '/tf/active/vice_ai/documents.db'
print("š Checking for corrupted chat_session_id values...")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
# Get ALL rows with their raw chat_session_id values
cursor.execute("""
SELECT id, title, chat_session_id,
CASE
WHEN chat_session_id IS NULL THEN 'NULL'
WHEN chat_session_id = '' THEN 'EMPTY_STRING'
WHEN chat_session_id = '{}' THEN 'CORRUPTED_BRACES'
WHEN chat_session_id = '[]' THEN 'CORRUPTED_BRACKETS'
WHEN chat_session_id = 'null' THEN 'CORRUPTED_NULL_STRING'
ELSE 'VALID'
END as status
FROM text_sections
ORDER BY id
""")
all_rows = cursor.fetchall()
print(f"š Total text_sections: {len(all_rows)}")
status_counts = {}
corrupted_rows = []
for row in all_rows:
section_id, title, chat_session_id, status = row
if status not in status_counts:
status_counts[status] = 0
status_counts[status] += 1
if status.startswith('CORRUPTED'):
corrupted_rows.append((section_id, chat_session_id, title[:30]))
print(f" š“ FOUND CORRUPTION: {section_id} -> '{chat_session_id}' ({status})")
print(f"\nš Status Summary:")
for status, count in status_counts.items():
print(f" {status}: {count}")
if corrupted_rows:
print(f"\nš§ Fixing {len(corrupted_rows)} corrupted values...")
# Fix the corruption by setting to NULL
cursor.execute("""
UPDATE text_sections
SET chat_session_id = NULL
WHERE chat_session_id IN ('{}', '[]', 'null', '')
""")
rows_updated = cursor.rowcount
conn.commit()
print(f"ā
Updated {rows_updated} rows - set corrupted values to NULL")
# Verify
cursor.execute("""
SELECT COUNT(*)
FROM text_sections
WHERE chat_session_id IN ('{}', '[]', 'null', '')
""")
remaining = cursor.fetchone()[0]
print(f"ā
Verification: {remaining} corrupted values remaining")
else:
print("ā
No corrupted values found")
except Exception as e:
print(f"ā Error: {e}")
conn.rollback()
finally:
conn.close()
Return Value
This function does not return any value (implicitly returns None). It performs side effects by modifying the database and printing status messages to stdout.
Dependencies
sqlite3
Required Imports
import sqlite3
Usage Example
import sqlite3
def check_and_fix_corruption():
db_path = '/tf/active/vice_ai/documents.db'
print("š Checking for corrupted chat_session_id values...")
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
cursor.execute("""
SELECT id, title, chat_session_id,
CASE
WHEN chat_session_id IS NULL THEN 'NULL'
WHEN chat_session_id = '' THEN 'EMPTY_STRING'
WHEN chat_session_id = '{}' THEN 'CORRUPTED_BRACES'
WHEN chat_session_id = '[]' THEN 'CORRUPTED_BRACKETS'
WHEN chat_session_id = 'null' THEN 'CORRUPTED_NULL_STRING'
ELSE 'VALID'
END as status
FROM text_sections
ORDER BY id
""")
all_rows = cursor.fetchall()
print(f"š Total text_sections: {len(all_rows)}")
status_counts = {}
corrupted_rows = []
for row in all_rows:
section_id, title, chat_session_id, status = row
if status not in status_counts:
status_counts[status] = 0
status_counts[status] += 1
if status.startswith('CORRUPTED'):
corrupted_rows.append((section_id, chat_session_id, title[:30]))
print(f" š“ FOUND CORRUPTION: {section_id} -> '{chat_session_id}' ({status})")
print(f"\nš Status Summary:")
for status, count in status_counts.items():
print(f" {status}: {count}")
if corrupted_rows:
print(f"\nš§ Fixing {len(corrupted_rows)} corrupted values...")
cursor.execute("""
UPDATE text_sections
SET chat_session_id = NULL
WHERE chat_session_id IN ('{}', '[]', 'null', '')
""")
rows_updated = cursor.rowcount
conn.commit()
print(f"ā
Updated {rows_updated} rows - set corrupted values to NULL")
cursor.execute("""
SELECT COUNT(*)
FROM text_sections
WHERE chat_session_id IN ('{}', '[]', 'null', '')
""")
remaining = cursor.fetchone()[0]
print(f"ā
Verification: {remaining} corrupted values remaining")
else:
print("ā
No corrupted values found")
except Exception as e:
print(f"ā Error: {e}")
conn.rollback()
finally:
conn.close()
# Run the function
check_and_fix_corruption()
Best Practices
- Always backup the database before running this function as it performs UPDATE operations
- The function uses hardcoded database path '/tf/active/vice_ai/documents.db' - consider parameterizing for reusability
- The function commits changes automatically - ensure you want automatic fixes rather than manual review
- Error handling includes rollback on exceptions, but successful operations are committed immediately
- The function prints verbose output - redirect stdout if running in production environments
- Consider running in a transaction with manual commit if you want to review changes before applying
- The function identifies specific corruption patterns: '{}', '[]', 'null', and empty strings - add more patterns if needed
- Connection is properly closed in finally block to prevent resource leaks
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function check_specific_corruption 92.3% similar
-
function direct_fix 89.7% similar
-
function cleanup_corrupted_chat_session_ids 84.8% similar
-
function raw_cleanup_database 80.6% similar
-
function inspect_database 78.5% similar