-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
51 lines (43 loc) · 1.53 KB
/
Copy pathdatabase.py
File metadata and controls
51 lines (43 loc) · 1.53 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
"""
Database connection and setup module
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base
from config import Config
class DatabaseManager:
"""Manages database connections and operations"""
def __init__(self):
self.engine = None
self.SessionLocal = None
def create_connection(self):
"""Create database connection"""
try:
self.engine = create_engine(Config.DATABASE_URL)
self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine)
print(f"Database connection created: {Config.DATABASE_URL}")
return True
except Exception as e:
print(f"Error creating database connection: {e}")
return False
def create_tables(self):
"""Create all database tables"""
try:
Base.metadata.create_all(bind=self.engine)
print("Database tables created successfully")
return True
except Exception as e:
print(f"Error creating tables: {e}")
return False
def get_session(self):
"""Get database session"""
if not self.SessionLocal:
self.create_connection()
return self.SessionLocal()
def close_connection(self):
"""Close database connection"""
if self.engine:
self.engine.dispose()
print("Database connection closed")
# Global database manager instance
db_manager = DatabaseManager()