Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

2.4 KB
Raw Blame
use axum::{
    extract::{Query, State},
    http::StatusCode,
    response::IntoResponse,
    Json,
};
use dbx_core::storage::{MigrationPreflight, MigrationReport};
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;

use crate::state::WebState;

#[derive(Debug, Default, Deserialize)]
pub struct MigrationStatusQuery {
    #[serde(default)]
    retry: bool,
}

pub async fn status(
    State(state): State<Arc<WebState>>,
    Query(query): Query<MigrationStatusQuery>,
) -> Result<Json<MigrationPreflight>, (StatusCode, Json<serde_json::Value>)> {
    let status = if query.retry {
        state.app.storage.retry_data_migration_inspection().await
    } else {
        state.app.storage.inspect_data_migration().await
    };
    status.map(Json).map_err(error_response)
}

pub async fn start(
    State(state): State<Arc<WebState>>,
) -> Result<Json<MigrationReport>, (StatusCode, Json<serde_json::Value>)> {
    state.migration_ready.store(false, std::sync::atomic::Ordering::Release);
    match state.app.storage.start_data_migration().await {
        Ok(report) => {
            let ready =
                state.app.storage.inspect_data_migration().await.map(|status| status.is_ready()).unwrap_or(false);
            if ready {
                let allow_managed = !state.password_disabled && state.password_hash.read().await.is_some();
                if let Err(error) = state.web_mcp.reload(&state.app.storage, allow_managed).await {
                    log::error!("Web MCP remained disabled after data migration: {error}");
                }
            }
            state.migration_ready.store(ready, std::sync::atomic::Ordering::Release);
            Ok(Json(report))
        }
        Err(error) => {
            state.migration_ready.store(false, std::sync::atomic::Ordering::Release);
            Err(error_response(error))
        }
    }
}

pub async fn retry(
    State(state): State<Arc<WebState>>,
) -> Result<Json<MigrationReport>, (StatusCode, Json<serde_json::Value>)> {
    start(State(state)).await
}

pub async fn cleanup_backups(
    State(state): State<Arc<WebState>>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
    state.app.storage.cleanup_migration_backups().await.map_err(error_response)?;
    Ok(StatusCode::NO_CONTENT)
}

fn error_response(error: String) -> (StatusCode, Json<serde_json::Value>) {
    (StatusCode::BAD_REQUEST, Json(json!({"error": error})))
}