Update src/chats.py
Browse files- src/chats.py +45 -24
src/chats.py
CHANGED
|
@@ -2,32 +2,53 @@ import sqlite3
|
|
| 2 |
import datetime
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Use /data for persistent storage
|
| 6 |
-
DB_FILE = "/data/chats.db"
|
| 7 |
|
| 8 |
def init_db():
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Rest of the code remains unchanged
|
| 33 |
def create_new_chat():
|
|
|
|
| 2 |
import datetime
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Use /data for persistent storage
|
| 6 |
+
DB_FILE = "/data/chats.db"
|
| 7 |
|
| 8 |
def init_db():
|
| 9 |
+
# Debug: Print the database path and check directory permissions
|
| 10 |
+
print(f"Database path: {DB_FILE}")
|
| 11 |
+
db_dir = os.path.dirname(DB_FILE)
|
| 12 |
+
print(f"Database directory: {db_dir}")
|
| 13 |
+
if not os.path.exists(db_dir):
|
| 14 |
+
print(f"Directory {db_dir} does not exist, attempting to create it")
|
| 15 |
+
try:
|
| 16 |
+
os.makedirs(db_dir, exist_ok=True)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Failed to create directory {db_dir}: {str(e)}")
|
| 19 |
+
raise RuntimeError(f"Cannot create directory {db_dir}: {str(e)}")
|
| 20 |
+
if not os.access(db_dir, os.W_OK):
|
| 21 |
+
print(f"Directory {db_dir} is not writable")
|
| 22 |
+
raise RuntimeError(f"Directory {db_dir} is not writable")
|
| 23 |
+
|
| 24 |
+
# Attempt to connect to the database
|
| 25 |
+
print(f"Attempting to connect to database at {DB_FILE}")
|
| 26 |
+
try:
|
| 27 |
+
with sqlite3.connect(DB_FILE) as conn:
|
| 28 |
+
print("Successfully connected to database")
|
| 29 |
+
c = conn.cursor()
|
| 30 |
+
c.execute('''
|
| 31 |
+
CREATE TABLE IF NOT EXISTS chats (
|
| 32 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 33 |
+
title TEXT,
|
| 34 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 35 |
+
)
|
| 36 |
+
''')
|
| 37 |
+
c.execute('''
|
| 38 |
+
CREATE TABLE IF NOT EXISTS messages (
|
| 39 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 40 |
+
chat_id INTEGER,
|
| 41 |
+
role TEXT,
|
| 42 |
+
content TEXT,
|
| 43 |
+
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 44 |
+
FOREIGN KEY(chat_id) REFERENCES chats(id)
|
| 45 |
+
)
|
| 46 |
+
''')
|
| 47 |
+
conn.commit()
|
| 48 |
+
print("Database tables created successfully")
|
| 49 |
+
except sqlite3.OperationalError as e:
|
| 50 |
+
print(f"SQLite error: {str(e)}")
|
| 51 |
+
raise
|
| 52 |
|
| 53 |
# Rest of the code remains unchanged
|
| 54 |
def create_new_chat():
|