feat: 统一数据库和migration系统

This commit is contained in:
杨明明 2025-08-08 18:37:02 +08:00
parent b543935a9f
commit ba121dfb3b
3 changed files with 16 additions and 21 deletions

View File

@ -520,12 +520,11 @@ impl AppState {
// 获取配置管理器
let config_manager = self.get_config_manager().await?;
// 获取数据库路径
// 获取数据库实例
let database = self.get_database();
let db_path = database.get_db_path().unwrap_or_else(|| "mixvideo.db".to_string());
// 创建服务管理器
let service_manager = Arc::new(ServiceManager::new(config_manager.as_ref().clone(), db_path));
let service_manager = Arc::new(ServiceManager::new(config_manager.as_ref().clone(), database));
// 初始化服务管理器
service_manager.initialize().await?;

View File

@ -40,8 +40,8 @@ pub struct ServiceManager {
impl ServiceManager {
/// 创建新的服务管理器
pub fn new(config_manager: ConfigManager, db_path: String) -> Self {
let repository = Arc::new(ComfyUIRepository::new(db_path));
pub fn new(config_manager: ConfigManager, database: Arc<crate::infrastructure::database::Database>) -> Self {
let repository = Arc::new(ComfyUIRepository::new(database));
Self {
comfyui_manager: Arc::new(RwLock::new(None)),

View File

@ -5,6 +5,7 @@ use anyhow::{Result, anyhow};
use rusqlite::{Connection, params, Row};
use serde_json;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{info, warn, error, debug};
use crate::data::models::comfyui::{
@ -14,32 +15,27 @@ use crate::data::models::comfyui::{
/// ComfyUI 数据仓库
pub struct ComfyUIRepository {
db_path: String,
database: Arc<crate::infrastructure::database::Database>,
}
impl ComfyUIRepository {
/// 创建新的数据仓库实例
pub fn new(db_path: String) -> Self {
Self { db_path }
pub fn new(database: Arc<crate::infrastructure::database::Database>) -> Self {
Self { database }
}
/// 获取数据库连接
fn get_connection(&self) -> Result<Connection> {
let conn = Connection::open(&self.db_path)?;
// 启用外键约束
conn.execute("PRAGMA foreign_keys = ON", [])?;
Ok(conn)
fn get_connection(&self) -> Result<std::sync::MutexGuard<Connection>> {
let conn = self.database.get_connection();
conn.lock().map_err(|e| anyhow!("获取数据库连接失败: {}", e))
}
/// 初始化数据库表
/// 初始化数据库表现在通过主数据库的migration系统处理
pub fn initialize(&self) -> Result<()> {
let conn = self.get_connection()?;
// 读取并执行迁移脚本
let migration_sql = include_str!("../migrations/comfyui_tables.sql");
conn.execute_batch(migration_sql)?;
info!("ComfyUI 数据库表初始化完成");
// ComfyUI表现在通过主数据库的migration系统创建
// 这里只需要确保连接可用
let _conn = self.get_connection()?;
info!("ComfyUI 数据仓库初始化完成");
Ok(())
}