Update utils/database.py
Browse files- utils/database.py +120 -38
utils/database.py
CHANGED
|
@@ -50,6 +50,7 @@ def create_connection(db_file):
|
|
| 50 |
except Error as e:
|
| 51 |
st.error("Failed to connect to database. Please try again or contact support.")
|
| 52 |
return None
|
|
|
|
| 53 |
def create_connection(db_file: str) -> Optional[sqlite3.Connection]:
|
| 54 |
"""Create a database connection."""
|
| 55 |
try:
|
|
@@ -62,37 +63,25 @@ def create_connection(db_file: str) -> Optional[sqlite3.Connection]:
|
|
| 62 |
# utils/database.py
|
| 63 |
# Add this version of create_tables (replacing the existing one)
|
| 64 |
|
|
|
|
|
|
|
| 65 |
def create_tables(conn: sqlite3.Connection) -> None:
|
| 66 |
-
"""
|
| 67 |
-
Create all necessary tables in the database.
|
| 68 |
-
|
| 69 |
-
Args:
|
| 70 |
-
conn (sqlite3.Connection): SQLite database connection
|
| 71 |
-
"""
|
| 72 |
try:
|
| 73 |
with conn_lock:
|
| 74 |
-
|
| 75 |
-
conn.execute('''
|
| 76 |
-
CREATE TABLE IF NOT EXISTS documents (
|
| 77 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 78 |
-
name TEXT NOT NULL,
|
| 79 |
-
content TEXT NOT NULL,
|
| 80 |
-
upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 81 |
-
);
|
| 82 |
-
''')
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
|
|
|
| 86 |
CREATE TABLE IF NOT EXISTS collections (
|
| 87 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 88 |
name TEXT NOT NULL UNIQUE,
|
| 89 |
description TEXT,
|
| 90 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# Document-Collections relationship table
|
| 95 |
-
conn.execute('''
|
| 96 |
CREATE TABLE IF NOT EXISTS document_collections (
|
| 97 |
document_id INTEGER,
|
| 98 |
collection_id INTEGER,
|
|
@@ -100,11 +89,31 @@ def create_tables(conn: sqlite3.Connection) -> None:
|
|
| 100 |
PRIMARY KEY (document_id, collection_id),
|
| 101 |
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE,
|
| 102 |
FOREIGN KEY (collection_id) REFERENCES collections (id) ON DELETE CASCADE
|
| 103 |
-
)
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
CREATE TABLE IF NOT EXISTS queries (
|
| 109 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 110 |
query TEXT NOT NULL,
|
|
@@ -112,11 +121,9 @@ def create_tables(conn: sqlite3.Connection) -> None:
|
|
| 112 |
document_id INTEGER,
|
| 113 |
query_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 114 |
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
# Annotations table
|
| 119 |
-
conn.execute('''
|
| 120 |
CREATE TABLE IF NOT EXISTS annotations (
|
| 121 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 122 |
document_id INTEGER NOT NULL,
|
|
@@ -124,19 +131,94 @@ def create_tables(conn: sqlite3.Connection) -> None:
|
|
| 124 |
page_number INTEGER,
|
| 125 |
annotation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 126 |
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE
|
| 127 |
-
)
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
conn.commit()
|
| 136 |
|
| 137 |
except sqlite3.Error as e:
|
| 138 |
-
st.error(f"Error
|
| 139 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
def get_all_documents(conn: sqlite3.Connection) -> List[Dict]:
|
| 142 |
"""Get all documents with their metadata and collections."""
|
|
|
|
| 50 |
except Error as e:
|
| 51 |
st.error("Failed to connect to database. Please try again or contact support.")
|
| 52 |
return None
|
| 53 |
+
|
| 54 |
def create_connection(db_file: str) -> Optional[sqlite3.Connection]:
|
| 55 |
"""Create a database connection."""
|
| 56 |
try:
|
|
|
|
| 63 |
# utils/database.py
|
| 64 |
# Add this version of create_tables (replacing the existing one)
|
| 65 |
|
| 66 |
+
# utils/database.py
|
| 67 |
+
|
| 68 |
def create_tables(conn: sqlite3.Connection) -> None:
|
| 69 |
+
"""Create all necessary tables in the database."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
try:
|
| 71 |
with conn_lock:
|
| 72 |
+
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Force create collections tables first
|
| 75 |
+
collections_tables = [
|
| 76 |
+
'''
|
| 77 |
CREATE TABLE IF NOT EXISTS collections (
|
| 78 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 79 |
name TEXT NOT NULL UNIQUE,
|
| 80 |
description TEXT,
|
| 81 |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 82 |
+
)
|
| 83 |
+
''',
|
| 84 |
+
'''
|
|
|
|
|
|
|
| 85 |
CREATE TABLE IF NOT EXISTS document_collections (
|
| 86 |
document_id INTEGER,
|
| 87 |
collection_id INTEGER,
|
|
|
|
| 89 |
PRIMARY KEY (document_id, collection_id),
|
| 90 |
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE,
|
| 91 |
FOREIGN KEY (collection_id) REFERENCES collections (id) ON DELETE CASCADE
|
| 92 |
+
)
|
| 93 |
+
'''
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
# Execute collections tables creation separately
|
| 97 |
+
for table_sql in collections_tables:
|
| 98 |
+
try:
|
| 99 |
+
cursor.execute(table_sql)
|
| 100 |
+
conn.commit()
|
| 101 |
+
except sqlite3.Error as e:
|
| 102 |
+
st.error(f"Error creating collections table: {e}")
|
| 103 |
+
st.error(f"SQL that failed: {table_sql}")
|
| 104 |
+
raise
|
| 105 |
|
| 106 |
+
# Create other tables
|
| 107 |
+
other_tables = [
|
| 108 |
+
'''
|
| 109 |
+
CREATE TABLE IF NOT EXISTS documents (
|
| 110 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 111 |
+
name TEXT NOT NULL,
|
| 112 |
+
content TEXT NOT NULL,
|
| 113 |
+
upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 114 |
+
)
|
| 115 |
+
''',
|
| 116 |
+
'''
|
| 117 |
CREATE TABLE IF NOT EXISTS queries (
|
| 118 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 119 |
query TEXT NOT NULL,
|
|
|
|
| 121 |
document_id INTEGER,
|
| 122 |
query_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 123 |
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE
|
| 124 |
+
)
|
| 125 |
+
''',
|
| 126 |
+
'''
|
|
|
|
|
|
|
| 127 |
CREATE TABLE IF NOT EXISTS annotations (
|
| 128 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 129 |
document_id INTEGER NOT NULL,
|
|
|
|
| 131 |
page_number INTEGER,
|
| 132 |
annotation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 133 |
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE
|
| 134 |
+
)
|
| 135 |
+
'''
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
# Execute other tables creation
|
| 139 |
+
for table_sql in other_tables:
|
| 140 |
+
try:
|
| 141 |
+
cursor.execute(table_sql)
|
| 142 |
+
conn.commit()
|
| 143 |
+
except sqlite3.Error as e:
|
| 144 |
+
st.error(f"Error creating table: {e}")
|
| 145 |
+
st.error(f"SQL that failed: {table_sql}")
|
| 146 |
+
raise
|
| 147 |
+
|
| 148 |
+
# Create indices
|
| 149 |
+
indices = [
|
| 150 |
+
'CREATE INDEX IF NOT EXISTS idx_doc_name ON documents(name)',
|
| 151 |
+
'CREATE INDEX IF NOT EXISTS idx_collection_name ON collections(name)',
|
| 152 |
+
'CREATE INDEX IF NOT EXISTS idx_doc_collections ON document_collections(collection_id)'
|
| 153 |
+
]
|
| 154 |
+
|
| 155 |
+
# Execute indices creation
|
| 156 |
+
for index_sql in indices:
|
| 157 |
+
try:
|
| 158 |
+
cursor.execute(index_sql)
|
| 159 |
+
conn.commit()
|
| 160 |
+
except sqlite3.Error as e:
|
| 161 |
+
st.error(f"Error creating index: {e}")
|
| 162 |
+
st.error(f"SQL that failed: {index_sql}")
|
| 163 |
|
| 164 |
+
# Verify collections table was created
|
| 165 |
+
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='collections'")
|
| 166 |
+
if not cursor.fetchone():
|
| 167 |
+
st.error("Failed to create collections table despite no errors")
|
| 168 |
+
raise Exception("Collections table creation failed silently")
|
| 169 |
|
| 170 |
conn.commit()
|
| 171 |
|
| 172 |
except sqlite3.Error as e:
|
| 173 |
+
st.error(f"Error in create_tables: {e}")
|
| 174 |
raise
|
| 175 |
+
except Exception as e:
|
| 176 |
+
st.error(f"Unexpected error in create_tables: {e}")
|
| 177 |
+
raise
|
| 178 |
+
|
| 179 |
+
def force_recreate_collections_tables(conn: sqlite3.Connection) -> bool:
|
| 180 |
+
"""Force recreate collections tables if they're missing."""
|
| 181 |
+
try:
|
| 182 |
+
with conn_lock:
|
| 183 |
+
cursor = conn.cursor()
|
| 184 |
+
|
| 185 |
+
# Drop existing tables if they exist
|
| 186 |
+
cursor.execute("DROP TABLE IF EXISTS document_collections")
|
| 187 |
+
cursor.execute("DROP TABLE IF EXISTS collections")
|
| 188 |
+
|
| 189 |
+
# Create collections table
|
| 190 |
+
cursor.execute('''
|
| 191 |
+
CREATE TABLE collections (
|
| 192 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 193 |
+
name TEXT NOT NULL UNIQUE,
|
| 194 |
+
description TEXT,
|
| 195 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 196 |
+
)
|
| 197 |
+
''')
|
| 198 |
+
|
| 199 |
+
# Create document_collections table
|
| 200 |
+
cursor.execute('''
|
| 201 |
+
CREATE TABLE document_collections (
|
| 202 |
+
document_id INTEGER,
|
| 203 |
+
collection_id INTEGER,
|
| 204 |
+
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 205 |
+
PRIMARY KEY (document_id, collection_id),
|
| 206 |
+
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE,
|
| 207 |
+
FOREIGN KEY (collection_id) REFERENCES collections (id) ON DELETE CASCADE
|
| 208 |
+
)
|
| 209 |
+
''')
|
| 210 |
+
|
| 211 |
+
# Create indices
|
| 212 |
+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_collection_name ON collections(name)')
|
| 213 |
+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_doc_collections ON document_collections(collection_id)')
|
| 214 |
+
|
| 215 |
+
conn.commit()
|
| 216 |
+
return True
|
| 217 |
+
|
| 218 |
+
except sqlite3.Error as e:
|
| 219 |
+
st.error(f"Error recreating collections tables: {e}")
|
| 220 |
+
return False
|
| 221 |
+
|
| 222 |
|
| 223 |
def get_all_documents(conn: sqlite3.Connection) -> List[Dict]:
|
| 224 |
"""Get all documents with their metadata and collections."""
|