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.9 KB
Raw Blame
/**
 * Pure decision helper for the PostgreSQL `CREATE INDEX CONCURRENTLY` checkbox
 * in the table structure editor (PR #6361).
 *
 * Concurrent builds are scoped to *new* indexes on *non-partitioned* tables:
 * editing an existing index would require a `DROP INDEX CONCURRENTLY` +
 * `CREATE INDEX CONCURRENTLY` replace flow (not implemented), and PostgreSQL
 * rejects `CREATE INDEX CONCURRENTLY` on partitioned parent tables. The core
 * SQL builder enforces the same scope as a hard error, so this only decides
 * whether the option is actionable in the UI.
 */
export type ConcurrentIndexUnavailableReason = "existing" | "partitioned" | "unknown" | "primary" | "markedForDrop" | "unsupported";

export interface ConcurrentIndexAvailability {
  enabled: boolean;
  reason?: ConcurrentIndexUnavailableReason;
}

export interface ConcurrentIndexAvailabilityInput {
  /** The draft edits an index that already exists on the table. */
  hasOriginal: boolean;
  isPrimary: boolean;
  markedForDrop: boolean;
  /** `relkind = 'p'` partitioned parent table. */
  isPartitionedParent: boolean;
  /**
   * The partition status could not be verified (probe failed). Fail closed:
   * do not offer Concurrent when we cannot rule out a partitioned parent.
   */
  partitionStatusKnown: boolean;
  supportsIndexConcurrent: boolean;
  supportsCreateIndex: boolean;
}

export function getConcurrentIndexAvailability(input: ConcurrentIndexAvailabilityInput): ConcurrentIndexAvailability {
  if (input.markedForDrop) return { enabled: false, reason: "markedForDrop" };
  if (input.isPrimary) return { enabled: false, reason: "primary" };
  if (input.hasOriginal) return { enabled: false, reason: "existing" };
  if (input.isPartitionedParent) return { enabled: false, reason: "partitioned" };
  if (!input.partitionStatusKnown) return { enabled: false, reason: "unknown" };
  if (!input.supportsIndexConcurrent || !input.supportsCreateIndex) return { enabled: false, reason: "unsupported" };
  return { enabled: true };
}

/**
 * Index names referenced by `CREATE [UNIQUE] INDEX CONCURRENTLY` statements,
 * used to detect same-name INVALID leftovers before applying a concurrent
 * build. The PG builder emits the index name as a double-quoted identifier
 * (optionally schema-qualified); when the identifier-quote preference is off,
 * the structure editor dequotes safe names before these statements reach this
 * check, so unquoted names are accepted as well.
 */
export function concurrentIndexNamesInStatements(statements: string[]): string[] {
  const names: string[] = [];
  for (const sql of statements) {
    const match = sql.match(/CREATE\s+(?:UNIQUE\s+)?INDEX\s+CONCURRENTLY\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:(?:"[^"]+"|[A-Za-z_][A-Za-z0-9_$]*)\s*\.\s*)?(?:"([^"]+)"|([A-Za-z_][A-Za-z0-9_$]*))/i);
    const name = match?.[1] ?? match?.[2];
    if (name) names.push(name);
  }
  return names;
}

/**
 * Report index drafts whose Concurrent availability is no longer `enabled`.
 * Definitive states (partitioned parent, existing index, capability loss, ...)
 * clear the stale flag. A transiently unknown partition status preserves the
 * user's intent so a later successful probe can restore the concurrent SQL.
 * Callers must gate save/SQL generation on every reported invalidation.
 */
export function normalizeUnsupportedConcurrentIndexes<IIndex extends { id: string; concurrently?: boolean }>(indexes: readonly IIndex[], availabilityResolver: (index: IIndex) => ConcurrentIndexAvailability): { indexes: IIndex[]; invalidatedIds: string[] } {
  const invalidatedIds: string[] = [];
  const normalized = indexes.map((index) => {
    if (!index.concurrently) return index;
    const availability = availabilityResolver(index);
    if (availability.enabled) return index;
    invalidatedIds.push(index.id);
    if (availability.reason === "unknown") return index;
    return { ...index, concurrently: false };
  });
  return { indexes: normalized, invalidatedIds };
}