feat: 平台预设字典+凭证加密+分库备份+Agent采集+移动端优先前端重做
This commit is contained in:
+41
-8
@@ -1,4 +1,8 @@
|
||||
"""SQLite 数据库连接与初始化"""
|
||||
"""SQLite 数据库连接与初始化(分库)
|
||||
|
||||
- assets.db :Provider / Asset / VPSDetail / DomainDetail / AIAccount
|
||||
- metrics.db:MetricPoint / ServerInfo / SecurityCheck / EventLog
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
@@ -9,20 +13,49 @@ BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
DATA_DIR = BASE_DIR / "data"
|
||||
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
DB_URL = f"sqlite:///{DATA_DIR / 'vps_manager.db'}"
|
||||
ASSETS_DB_URL = f"sqlite:///{DATA_DIR / 'assets.db'}"
|
||||
METRICS_DB_URL = f"sqlite:///{DATA_DIR / 'metrics.db'}"
|
||||
|
||||
engine = create_engine(DB_URL, echo=False, connect_args={"check_same_thread": False})
|
||||
assets_engine = create_engine(
|
||||
ASSETS_DB_URL, echo=False, connect_args={"check_same_thread": False}
|
||||
)
|
||||
metrics_engine = create_engine(
|
||||
METRICS_DB_URL, echo=False, connect_args={"check_same_thread": False}
|
||||
)
|
||||
|
||||
# 兼容旧代码:默认 engine 指向资产库
|
||||
engine = assets_engine
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
"""创建所有表(幂等,可重复调用)"""
|
||||
# 显式导入模型,确保其注册到 SQLModel.metadata
|
||||
"""分库建表(幂等,可重复调用)"""
|
||||
from app.models.asset import AIAccount, Asset, DomainDetail, VPSDetail # noqa: F401
|
||||
from app.models.monitor import ( # noqa: F401
|
||||
EventLog,
|
||||
MetricPoint,
|
||||
SecurityCheck,
|
||||
ServerInfo,
|
||||
)
|
||||
from app.models.provider import Provider # noqa: F401
|
||||
|
||||
SQLModel.metadata.create_all(engine)
|
||||
asset_models = [Provider, Asset, VPSDetail, DomainDetail, AIAccount]
|
||||
metric_models = [MetricPoint, ServerInfo, SecurityCheck, EventLog]
|
||||
|
||||
SQLModel.metadata.create_all(
|
||||
assets_engine, tables=[m.__table__ for m in asset_models]
|
||||
)
|
||||
SQLModel.metadata.create_all(
|
||||
metrics_engine, tables=[m.__table__ for m in metric_models]
|
||||
)
|
||||
|
||||
|
||||
def get_session() -> Generator[Session, None, None]:
|
||||
"""FastAPI 依赖:提供数据库会话(请求结束后自动关闭)"""
|
||||
with Session(engine) as session:
|
||||
"""资产库会话(默认)"""
|
||||
with Session(assets_engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
def get_metrics_session() -> Generator[Session, None, None]:
|
||||
"""监控 / 日志库会话"""
|
||||
with Session(metrics_engine) as session:
|
||||
yield session
|
||||
|
||||
Reference in New Issue
Block a user