Skip to main content

second_brain_api/
repairs.rs

1//! One-off data repairs, each idempotent: a second run finds nothing left
2//! to change, so running one again after a partial failure is always safe.
3
4use std::sync::Arc;
5
6use axum::Json;
7use axum::extract::State;
8use axum::response::{IntoResponse, Response};
9use serde::Serialize;
10
11use crate::AppState;
12use crate::routes::internal_error;
13
14async fn run<T, F>(state: Arc<AppState>, repair: F) -> Response
15where
16    T: Serialize + Send + 'static,
17    F: FnOnce(&second_brain_core::sqlite_store::SqliteStore) -> anyhow::Result<T> + Send + 'static,
18{
19    let result = tokio::task::spawn_blocking(move || repair(&state.sqlite)).await;
20    match result {
21        Ok(Ok(outcome)) => Json(outcome).into_response(),
22        Ok(Err(e)) => internal_error(e),
23        Err(e) => internal_error(e),
24    }
25}
26
27pub async fn refile_nested_projects(State(state): State<Arc<AppState>>) -> Response {
28    run(state, |store| {
29        Ok(serde_json::json!({
30            "memories_refiled": store.refile_nested_transcript_projects()?
31        }))
32    })
33    .await
34}
35
36pub async fn restore_explicit(State(state): State<Arc<AppState>>) -> Response {
37    run(state, |store| store.restore_explicit_memories()).await
38}
39
40pub async fn refile_stray_projects(State(state): State<Arc<AppState>>) -> Response {
41    run(state, |store| store.refile_stray_project_slugs()).await
42}