-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdatabase.py
More file actions
277 lines (230 loc) · 9.92 KB
/
database.py
File metadata and controls
277 lines (230 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Database Models and Configuration
Centralized database models using SQLAlchemy ORM.
Supports SQLite for development and PostgreSQL for production.
"""
import os
from datetime import datetime
from typing import Optional
from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime, Boolean, JSON
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
from contextlib import contextmanager
import logging
logger = logging.getLogger(__name__)
# Base class for all models
Base = declarative_base()
class Message(Base):
"""Chat message log model."""
__tablename__ = 'messages'
id = Column(Integer, primary_key=True, autoincrement=True)
timestamp = Column(DateTime, default=datetime.utcnow, nullable=False, index=True)
user_message = Column(Text, nullable=False)
bot_response = Column(Text)
provider = Column(String(50), index=True)
model = Column(String(100))
tokens_used = Column(Integer)
metadata_ = Column('metadata', JSON) # Rename to avoid SQLAlchemy reserved word
def to_dict(self):
"""Convert model to dictionary."""
return {
'id': self.id,
'timestamp': self.timestamp.isoformat() if self.timestamp else None,
'user_message': self.user_message,
'bot_response': self.bot_response,
'provider': self.provider,
'model': self.model,
'tokens_used': self.tokens_used,
'metadata': self.metadata_
}
class APIToken(Base):
"""API token model."""
__tablename__ = 'api_tokens'
id = Column(Integer, primary_key=True, autoincrement=True)
token_hash = Column(String(64), unique=True, nullable=False, index=True)
description = Column(String(255), nullable=False)
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
expires_at = Column(DateTime, nullable=True)
is_active = Column(Boolean, default=True, nullable=False)
last_used_at = Column(DateTime, nullable=True)
def to_dict(self):
"""Convert model to dictionary."""
now = datetime.utcnow()
is_expired = self.expires_at and self.expires_at < now
return {
'id': self.id,
'description': self.description,
'created_at': self.created_at.isoformat() if self.created_at else None,
'expires_at': self.expires_at.isoformat() if self.expires_at else None,
'is_active': self.is_active,
'is_expired': is_expired,
'last_used_at': self.last_used_at.isoformat() if self.last_used_at else None
}
class DatabaseManager:
"""
Database manager with automatic SQLite/PostgreSQL switching.
Environment Variables:
DATABASE_URL: PostgreSQL connection string (e.g., postgresql://user:pass@host/db)
If not set, uses SQLite with local db/babblebeaver.db file
"""
def __init__(self, database_url: Optional[str] = None):
"""
Initialize database manager.
Args:
database_url: Override database URL (optional)
"""
self.database_url = database_url or os.getenv('DATABASE_URL')
# Determine database type and configure connection
if self.database_url and self.database_url.startswith('postgresql://'):
self.db_type = 'postgresql'
self.engine = create_engine(
self.database_url,
pool_size=10,
max_overflow=20,
pool_pre_ping=True, # Verify connections before using
echo=False
)
logger.info("Using PostgreSQL database")
else:
self.db_type = 'sqlite'
# Create db directory if it doesn't exist
db_dir = 'db'
os.makedirs(db_dir, exist_ok=True)
sqlite_path = f'{db_dir}/babblebeaver.db'
self.database_url = f'sqlite:///{sqlite_path}'
self.engine = create_engine(
self.database_url,
connect_args={'check_same_thread': False}, # SQLite specific
echo=False
)
logger.info(f"Using SQLite database: {sqlite_path}")
# Create session factory
self.SessionLocal = sessionmaker(
autocommit=False,
autoflush=False,
bind=self.engine
)
# Create tables
self._create_tables()
def _create_tables(self):
"""Create all tables if they don't exist."""
try:
Base.metadata.create_all(bind=self.engine)
logger.info("Database tables created/verified")
except Exception as e:
logger.error(f"Error creating database tables: {e}")
raise
@contextmanager
def get_session(self) -> Session:
"""
Context manager for database sessions.
Usage:
with db_manager.get_session() as session:
user = session.query(User).first()
"""
session = self.SessionLocal()
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
def migrate_from_old_sqlite(self, old_db_path: str = 'chatbot.db'):
"""
Migrate data from old SQLite database to new schema.
Args:
old_db_path: Path to old chatbot.db file
"""
if not os.path.exists(old_db_path):
logger.info(f"Old database {old_db_path} not found. No migration needed.")
return
import sqlite3
logger.info(f"Migrating data from {old_db_path}...")
# Connect to old database
old_conn = sqlite3.connect(old_db_path)
old_conn.row_factory = sqlite3.Row
old_cursor = old_conn.cursor()
try:
# Migrate messages
old_cursor.execute("SELECT * FROM messages ORDER BY id")
messages = old_cursor.fetchall()
with self.get_session() as session:
migrated_count = 0
for row in messages:
# Check if message already exists
existing = session.query(Message).filter_by(id=row['id']).first()
if existing:
continue
# Parse metadata if it's a JSON string
metadata = row['metadata']
if isinstance(metadata, str):
import json
try:
metadata = json.loads(metadata)
except:
metadata = {}
message = Message(
id=row['id'],
timestamp=datetime.fromisoformat(row['timestamp']) if row['timestamp'] else None,
user_message=row['user_message'],
bot_response=row['bot_response'],
provider=row['provider'],
model=row['model'],
tokens_used=row['tokens_used'],
metadata=metadata
)
session.add(message)
migrated_count += 1
logger.info(f"Migrated {migrated_count} messages")
# Migrate tokens (if old tokens table exists)
try:
old_cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='tokens'")
if old_cursor.fetchone():
old_cursor.execute("SELECT * FROM tokens")
tokens = old_cursor.fetchall()
with self.get_session() as session:
token_count = 0
for row in tokens:
existing = session.query(APIToken).filter_by(id=row['id']).first()
if existing:
continue
token = APIToken(
id=row['id'],
token_hash=row['token_hash'],
description=row['description'],
created_at=datetime.fromisoformat(row['created_at']) if row['created_at'] else None,
expires_at=datetime.fromisoformat(row['expires_at']) if row['expires_at'] else None,
is_active=bool(row['is_active']),
last_used_at=datetime.fromisoformat(row['last_used_at']) if row['last_used_at'] else None
)
session.add(token)
token_count += 1
logger.info(f"Migrated {token_count} API tokens")
except sqlite3.Error:
logger.info("No tokens table in old database")
except Exception as e:
logger.error(f"Migration error: {e}")
raise
finally:
old_conn.close()
logger.info("Migration completed successfully")
# Global database manager instance
db_manager = DatabaseManager()
def get_db_session():
"""
Dependency for FastAPI endpoints to get database session.
Usage in FastAPI:
@app.get("/messages")
def get_messages(db: Session = Depends(get_db_session)):
messages = db.query(Message).all()
return messages
"""
with db_manager.get_session() as session:
yield session
# Convenience function for migrations
def run_migration():
"""Run database migration from old SQLite database."""
db_manager.migrate_from_old_sqlite('chatbot.db')
db_manager.migrate_from_old_sqlite('db/tokens.db')