mirror of
https://github.com/P-Aimon-Pen/ROFMAD
synced 2026-06-21 13:45:02 +00:00
98 lines
3.1 KiB
Rust
Executable File
98 lines
3.1 KiB
Rust
Executable File
// src/sql/context.rs
|
|
use sqlx::query;
|
|
use sqlx::{Pool, Sqlite, sqlite::SqlitePoolOptions};
|
|
use std::error::Error;
|
|
use uuid::Uuid;
|
|
use crate::utilities::tag_info;
|
|
use super::database::initialize_db;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct ProjectContext {
|
|
pub workflow_path: String,
|
|
pub db_path: String,
|
|
pub run_id: String,
|
|
pub pool: Pool<Sqlite>,
|
|
}
|
|
|
|
impl ProjectContext {
|
|
// Context: Project initialization system for creating new database-backed contexts
|
|
// Operation: Creates new database, initializes schema, generates run ID, and sets up connection pool
|
|
pub async fn new(name: &str, directory: &str) -> Result<Self, Box<dyn Error + Send + Sync>> {
|
|
let db_path = format!("{}/{}.db", directory.trim(), name.trim());
|
|
let conn_str = format!("sqlite://{}?mode=rwc", db_path);
|
|
|
|
let pool = SqlitePoolOptions::new()
|
|
.max_connections(20)
|
|
.connect(&conn_str)
|
|
.await?;
|
|
initialize_db(&pool).await?;
|
|
let _ = sqlx::query("PRAGMA journal_mode = WAL").execute(&pool).await;
|
|
|
|
let run_id = Uuid::new_v4().to_string();
|
|
let ctx = ProjectContext {
|
|
workflow_path: String::new(),
|
|
db_path: db_path.clone(),
|
|
run_id: run_id.clone(),
|
|
pool: pool.clone(),
|
|
};
|
|
|
|
// Reset all started flags for new project
|
|
ctx.reset_all_started_flags().await?;
|
|
|
|
// store run_id in metadata for resume
|
|
let _ = query(
|
|
"INSERT OR REPLACE INTO metadata (key, value) VALUES ('run_id', ?1)",
|
|
)
|
|
.bind(&run_id)
|
|
.execute(&pool)
|
|
.await;
|
|
Ok(ctx)
|
|
}
|
|
|
|
// Context: Project resumption system for continuing work with existing database
|
|
// Operation: Connects to existing database, retrieves run ID and workflow path, validates state
|
|
pub async fn resume(path: &str) -> Result<Self, Box<dyn Error + Send + Sync>> {
|
|
let conn_str = format!("sqlite://{}?mode=rwc", path.trim());
|
|
let pool = SqlitePoolOptions::new()
|
|
.max_connections(20)
|
|
.connect(&conn_str)
|
|
.await?;
|
|
initialize_db(&pool).await?;
|
|
let _ = query("PRAGMA journal_mode = WAL").execute(&pool).await;
|
|
|
|
// fetch the stored run_id
|
|
let (run_id,): (String,) = sqlx::query_as(
|
|
"SELECT value FROM metadata WHERE key = 'run_id'"
|
|
)
|
|
.fetch_one(&pool)
|
|
.await?;
|
|
|
|
// fetch the stored workflow_path with fallback
|
|
let workflow_path: String = sqlx::query_scalar(
|
|
"SELECT workflow_path FROM settings WHERE run_id = ?"
|
|
)
|
|
.bind(&run_id)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap_or_else(|_| String::new());
|
|
|
|
let ctx = ProjectContext {
|
|
workflow_path,
|
|
db_path: path.to_string(),
|
|
run_id: run_id.clone(),
|
|
pool: pool.clone(),
|
|
};
|
|
|
|
|
|
tag_info(&format!("Resumed DB at {}", path));
|
|
|
|
// If workflow_path is empty, warn the user
|
|
if ctx.workflow_path.is_empty() {
|
|
tag_info(" No workflow path found in database - you may need to specify it manually");
|
|
} else {
|
|
tag_info(&format!("Using workflow: {}", ctx.workflow_path));
|
|
}
|
|
|
|
Ok(ctx)
|
|
}
|
|
} |