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。

5.6 KB
Raw Blame
<script setup lang="ts">
import { computed } from "vue";
import { useI18n } from "vue-i18n";
import { schemaDiffObjectSelectionState, type SchemaDiffObject } from "@/lib/schema/schemaDiff";
import { schemaDiffRoutineKey, summarizeSchemaDiffRoutineTextDiff, type SchemaDiffRoutineTextDiffStats } from "@/lib/schema/schemaDiffRoutine";

const props = defineProps<{
  objects: SchemaDiffObject[];
  viewingObjectId?: string | null;
  emptyText?: string;
  /** When false, hide deploy checkboxes (compare/copy-only dialects). */
  selectable?: boolean;
}>();

const emit = defineEmits<{
  (e: "toggle-selection", object: SchemaDiffObject, selected: boolean): void;
  (e: "view-diff", object: SchemaDiffObject): void;
}>();

const { t } = useI18n();

const showSelection = computed(() => props.selectable !== false);
const emptyLabel = computed(() => props.emptyText || t("diff.noDifferences"));
const gridClass = computed(() => (showSelection.value ? "grid-cols-[28px_minmax(0,1.2fr)_minmax(0,1.2fr)_minmax(120px,0.9fr)]" : "grid-cols-[minmax(0,1.2fr)_minmax(0,1.2fr)_minmax(120px,0.9fr)]"));

const rows = computed(() =>
  props.objects.map((object) => {
    const stats = summarizeSchemaDiffRoutineTextDiff(object.sourceDdl, object.targetDdl);
    return {
      object,
      sourceLabel: object.operationType === "delete" ? "" : displayName(object, "source"),
      targetLabel: object.operationType === "create" ? "" : displayName(object, "target"),
      stats,
      selection: schemaDiffObjectSelectionState(object),
    };
  }),
);

function displayName(object: SchemaDiffObject, side: "source" | "target"): string {
  const name = side === "source" ? (object.sourceName ?? object.name) : (object.targetName ?? object.name);
  if (object.routineType === "PACKAGE" || object.routineType === "PACKAGE_BODY") return `${object.routineType.replaceAll("_", " ")} ${name}`;
  return schemaDiffRoutineKey(name, object.arguments ?? "");
}

function hasStats(stats: SchemaDiffRoutineTextDiffStats): boolean {
  return stats.added !== 0 || stats.removed !== 0 || stats.modified !== 0;
}

function onCheckboxChange(object: SchemaDiffObject, event: Event) {
  if (object.operationType === "none") return;
  emit("toggle-selection", object, (event.target as HTMLInputElement).checked);
}

function onRowActivate(object: SchemaDiffObject) {
  emit("view-diff", object);
}
</script>

<template>
  <div class="min-w-0">
    <div class="grid gap-2 border-b px-2 py-1.5 text-xs font-medium text-muted-foreground" :class="gridClass">
      <div v-if="showSelection" />
      <div>{{ t("diff.sourceObject") }}</div>
      <div>{{ t("diff.targetObject") }}</div>
      <div>{{ t("diff.routineDiffPoints") }}</div>
    </div>

    <div v-if="rows.length === 0" class="px-3 py-8 text-center text-xs text-muted-foreground">
      {{ emptyLabel }}
    </div>

    <div v-else class="divide-y divide-border/40">
      <div
        v-for="row in rows"
        :key="row.object.id"
        role="button"
        tabindex="0"
        class="grid cursor-pointer items-center gap-2 px-2 py-1.5 text-xs outline-none focus-visible:bg-accent/40"
        :class="[gridClass, viewingObjectId === row.object.id ? 'bg-primary/10' : 'hover:bg-accent/30']"
        @click="onRowActivate(row.object)"
        @keydown.enter.prevent="onRowActivate(row.object)"
        @keydown.space.prevent="onRowActivate(row.object)"
      >
        <input
          v-if="showSelection"
          type="checkbox"
          class="accent-primary justify-self-center"
          :checked="row.selection.checked"
          :indeterminate="row.selection.indeterminate"
          :disabled="row.object.operationType === 'none'"
          :aria-label="displayName(row.object, 'source') || displayName(row.object, 'target')"
          @click.stop
          @change="onCheckboxChange(row.object, $event)"
        />
        <div class="min-w-0 truncate font-mono" :title="row.sourceLabel || undefined">
          <span v-if="row.sourceLabel" :class="row.object.operationType === 'create' ? 'text-green-600 dark:text-green-400' : ''">{{ row.sourceLabel }}</span>
          <span v-else class="text-muted-foreground">—</span>
        </div>
        <div class="min-w-0 truncate font-mono" :title="row.targetLabel || undefined">
          <span v-if="row.targetLabel" :class="row.object.operationType === 'delete' ? 'text-red-500 line-through' : ''">{{ row.targetLabel }}</span>
          <span v-else class="text-muted-foreground">—</span>
        </div>
        <div class="min-w-0 truncate tabular-nums" :title="hasStats(row.stats) ? t('diff.routineDiffStats', { added: row.stats.added, removed: row.stats.removed, modified: row.stats.modified }) : undefined">
          <span v-if="row.object.routineComparisonState === 'equal'" class="text-muted-foreground">{{ t("diff.routineEqual") }}</span>
          <span v-else-if="row.object.routineComparisonState === 'unreadable'" class="text-amber-700 dark:text-amber-300">{{ t("diff.routineUnreadable") }}</span>
          <span v-else-if="row.object.routineComparisonState === 'conflict'" class="text-destructive">{{ t("diff.routineConflict") }}</span>
          <template v-else-if="hasStats(row.stats)">
            <span class="text-green-600 dark:text-green-400">+{{ row.stats.added }}</span>
            <span class="text-muted-foreground"> </span>
            <span class="text-red-500 dark:text-red-400">−{{ row.stats.removed }}</span>
            <span class="text-muted-foreground"> </span>
            <span class="text-amber-600 dark:text-amber-400">~{{ row.stats.modified }}</span>
          </template>
          <span v-else class="text-muted-foreground">—</span>
        </div>
      </div>
    </div>
  </div>
</template>