Initial commit
This commit is contained in:
22
services/ingest-service/app/infrastructure/database.py
Normal file
22
services/ingest-service/app/infrastructure/database.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import os
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
# Для Alembic используем синхронный движок с psycopg2
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://postgres:postgres@localhost:5432/ingest_db")
|
||||
# Преобразуем asyncpg URL в psycopg2 для синхронного движка
|
||||
SYNC_DATABASE_URL = DATABASE_URL.replace("postgresql+asyncpg://", "postgresql://")
|
||||
|
||||
engine = create_engine(SYNC_DATABASE_URL)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user