2026-03-16 11:39:39 +08:00
|
|
|
import os
|
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
|
|
|
|
|
DATABASE_URL = os.getenv(
|
|
|
|
|
"DATABASE_URL",
|
|
|
|
|
"postgresql://postgres:postgres@localhost:5432/zodiac"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
engine = create_engine(
|
|
|
|
|
DATABASE_URL,
|
|
|
|
|
pool_pre_ping=True,
|
|
|
|
|
pool_size=10,
|
|
|
|
|
max_overflow=20,
|
2026-03-23 10:56:23 +08:00
|
|
|
pool_recycle=3600,
|
|
|
|
|
pool_timeout=30,
|
2026-03-16 11:39:39 +08:00
|
|
|
echo=False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_db():
|
2026-03-18 13:32:52 +08:00
|
|
|
db = SessionLocal(expire_on_commit=False)
|
2026-03-16 11:39:39 +08:00
|
|
|
try:
|
|
|
|
|
yield db
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|