2026-03-23 11:08:52 +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,
|
2026-03-24 22:30:53 +08:00
|
|
|
pool_size=20,
|
|
|
|
|
max_overflow=40,
|
2026-03-23 11:08:52 +08:00
|
|
|
pool_recycle=3600,
|
|
|
|
|
pool_timeout=30,
|
|
|
|
|
echo=False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_db():
|
|
|
|
|
db = SessionLocal(expire_on_commit=False)
|
|
|
|
|
try:
|
|
|
|
|
yield db
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|