Files
aitbc/apps/exchange/database.py
aitbc fb460816e4
All checks were successful
API Endpoint Tests / test-api-endpoints (push) Successful in 38s
Documentation Validation / validate-docs (push) Successful in 10s
Integration Tests / test-service-integration (push) Successful in 57s
Python Tests / test-python (push) Successful in 1m32s
Security Scanning / security-scan (push) Successful in 1m7s
fix: standardize exchange database path to use centralized data directory with environment variable
🔧 Database Path Standardization:
• Change DATABASE_URL environment variable to EXCHANGE_DATABASE_URL
• Update default database path from ./exchange.db to /var/lib/aitbc/data/exchange/exchange.db
• Apply consistent path resolution across all exchange database connections
• Update database.py, seed_market.py, and simple_exchange_api.py with new path
• Maintain backward compatibility through
2026-03-30 13:34:20 +02:00

50 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Database configuration for the AITBC Trade Exchange
"""
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.pool import StaticPool
from models import Base
# Database configuration
DATABASE_URL = os.getenv("EXCHANGE_DATABASE_URL", "sqlite:////var/lib/aitbc/data/exchange/exchange.db")
# Create engine
if DATABASE_URL.startswith("sqlite"):
engine = create_engine(
DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
echo=False # Set to True for SQL logging
)
else:
engine = create_engine(DATABASE_URL, echo=False)
# Create session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create tables
def init_db():
"""Initialize database tables"""
Base.metadata.create_all(bind=engine)
def get_db() -> Session:
"""Get database session"""
db = SessionLocal()
try:
yield db
finally:
db.close()
# Dependency for FastAPI
def get_db_session():
"""Get database session for FastAPI dependency"""
db = SessionLocal()
try:
return db
finally:
pass # Don't close here, let the caller handle it