You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
851 B
36 lines
851 B
"""
|
|
数据库配置和连接管理
|
|
"""
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
import os
|
|
|
|
# 数据库URL配置
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./code_scanner.db")
|
|
|
|
# 创建数据库引擎
|
|
engine = create_engine(
|
|
DATABASE_URL,
|
|
connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {}
|
|
)
|
|
|
|
# 创建会话工厂
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
# 创建基础模型类
|
|
Base = declarative_base()
|
|
|
|
def get_db():
|
|
"""获取数据库会话"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
def init_db():
|
|
"""初始化数据库表"""
|
|
from app.models import project, scan, vulnerability
|
|
Base.metadata.create_all(bind=engine)
|