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。

3.8 KB
Raw Blame
import { useProductionSafetyStore } from "@/stores/productionSafetyStore";
import { assessProductionSql, productionContextForDatabase, productionDatabases } from "@/lib/database/productionSafety";
import { ensureReadOnlyWriteAccess } from "@/lib/database/readOnlyWriteAccess";
import { assessProductionSqlInBackground } from "./productionSafetyWorker";
import type { ConnectionConfig } from "@/types/database";

export interface ProductionSqlExecutionGuardOptions<T> {
  connection?: ConnectionConfig;
  database?: string | null;
  sql: string;
  source?: string;
  /** Allows the owning editor to cancel only its pending confirmation on close/switch. */
  scopeId?: string;
  execute: () => Promise<T>;
  /** Offload large generated scripts while preserving the same safety assessment. */
  analyzeInBackground?: boolean;
}

export async function executeWithProductionSqlGuard<T>(options: ProductionSqlExecutionGuardOptions<T>): Promise<T | undefined> {
  const backgroundAssessment = options.analyzeInBackground && options.sql.length > 64 * 1024 ? await assessProductionSqlInBackground({ sql: options.sql, connection: options.connection, database: options.database }) : undefined;
  if (!(await ensureReadOnlyWriteAccess({ connection: options.connection, sql: options.sql, source: options.source, treatAsMutation: backgroundAssessment?.isMutation }))) {
    return undefined;
  }
  const assessment = backgroundAssessment ?? assessProductionSql(options.sql, options.connection, options.database);
  if (assessment.active && assessment.isMutation) {
    // Centralize production write confirmation so secondary tool surfaces cannot
    // bypass the same explicit review step used by the SQL editor.
    const confirmed = await useProductionSafetyStore().requestConfirmation({
      sql: options.sql,
      connectionName: options.connection?.name,
      database: options.database ?? undefined,
      productionDatabases: assessment.databases,
      source: options.source,
      scopeId: options.scopeId,
    });
    if (!confirmed) return undefined;
  }
  return options.execute();
}

export interface ProductionContextExecutionGuardOptions<T> {
  connection?: ConnectionConfig;
  database?: string | null;
  /** Review text shown in the production confirmation dialog (SQL or shell preview). */
  reviewText: string;
  source?: string;
  /** Use every production marker for connection-wide mutations; omitted keeps the existing database scope. */
  scope?: "database" | "connection";
  execute: () => Promise<T>;
}

/**
 * Gate non-SQL mutations (Mongo shell commands, structured editors, etc.) using
 * connection/database production scope rather than SQL risk classification.
 */
export async function executeWithProductionContextGuard<T>(options: ProductionContextExecutionGuardOptions<T>): Promise<T | undefined> {
  if (!(await ensureReadOnlyWriteAccess({ connection: options.connection, sql: options.reviewText, source: options.source, treatAsMutation: true }))) {
    return undefined;
  }
  const productionContext =
    options.scope === "connection"
      ? (() => {
          const databases = productionDatabases(options.connection);
          const active = Boolean(options.connection?.is_production || databases.length > 0);
          return { active, reason: active ? ("connection" as const) : undefined, databases };
        })()
      : productionContextForDatabase(options.connection, options.database);
  if (productionContext.active) {
    const confirmed = await useProductionSafetyStore().requestConfirmation({
      sql: options.reviewText,
      connectionName: options.connection?.name,
      database: options.scope === "connection" ? undefined : (options.database ?? undefined),
      productionDatabases: productionContext.databases,
      source: options.source,
    });
    if (!confirmed) return undefined;
  }
  return options.execute();
}