1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00
Files
AdministrationItDepartmens/backend/app/database.py
MoonTestUse1 bdf4ae9d70 Fix database
2025-01-07 05:26:33 +06:00

28 lines
835 B
Python

"""Database configuration"""
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Определяем URL базы данных в зависимости от окружения
if os.getenv("TESTING"):
SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
else:
SQLALCHEMY_DATABASE_URL = "postgresql://postgres:postgres@postgres:5432/app"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
"""Get database session"""
db = SessionLocal()
try:
yield db
finally:
db.close()