Files
org-parking/fix_db_index.py

62 lines
1.8 KiB
Python

import sqlite3
import os
import time
# Function to find the db file
def find_db():
candidates = [
'data/parking.db',
'/home/ssalemi/org-parking/data/parking.db',
'./data/parking.db'
]
for path in candidates:
if os.path.exists(path):
return path
return None
db_path = find_db()
if not db_path:
print("Error: Could not find data/parking.db. Make sure you are in the project root.")
exit(1)
print(f"Target Database: {db_path}")
print("Attempting to fix 'parking_exclusions' index...")
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Turn off foreign keys temporarily to avoid issues during schema modification if needed
cursor.execute("PRAGMA foreign_keys=OFF")
# 1. Drop the existing unique index
print("Dropping index idx_exclusion_office_user...")
try:
cursor.execute("DROP INDEX IF EXISTS idx_exclusion_office_user")
print("Index dropped successfully.")
except Exception as e:
print(f"Warning during drop: {e}")
# 2. Recreate it as non-unique
print("Creating non-unique index idx_exclusion_office_user...")
try:
cursor.execute("CREATE INDEX idx_exclusion_office_user ON parking_exclusions (office_id, user_id)")
print("Index created successfully.")
except Exception as e:
print(f"Error creating index: {e}")
exit(1)
conn.commit()
conn.close()
print("\nSUCCESS: Database updated. You can now define multiple exclusions per user.")
except sqlite3.OperationalError as e:
if "locked" in str(e):
print("\nERROR: Database is LOCKED.")
print("Please STOP the running application (Docker) and try again.")
else:
print(f"\nError: {e}")
except Exception as e:
print(f"\nUnexpected Error: {e}")