Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: view schema diff for drifted databases (#16577)

boojackd5f24ca445d4
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 4b7017db7b..7e9646de3c 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1031,7 +1031,7 @@
"schema-migration": "Schema Migration",
"schema-migration-description": "Database structural changes (DDL) including tables, columns, indexes, constraints and schema alterations",
"data-change": "Data Change",
- "data-change-description": "Database content modifications (DML) including inserts, updates, deletes and data transformations",
+ "data-change-description": "Database content modifications (DML) including inserts, updates, deletes and data transformations",
"select-targets": "Select Targets",
"overview": {
"description": "View and manage all specifications in this plan",
@@ -1606,7 +1606,8 @@
"new-baseline": {
"self": "New baseline",
"successfully-established": "Successfully established new baseline"
- }
+ },
+ "view-diff": "View diff"
}
},
"project": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 47fe8eb6e9..40d2e7a23f 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1606,7 +1606,8 @@
"new-baseline": {
"self": "Nueva línea base",
"successfully-established": "Se estableció con éxito una nueva línea base"
- }
+ },
+ "view-diff": "Ver diferencia"
}
},
"project": {
@@ -2801,4 +2802,4 @@
"last-visit": "Última visita",
"changelog-for-version": "Registro de cambios para {version}"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 7eb14567b7..e8a7f0acbf 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -1606,7 +1606,8 @@
"new-baseline": {
"self": "新しいベースライン",
"successfully-established": "新しいベースラインを確立しました"
- }
+ },
+ "view-diff": "差分を表示"
}
},
"project": {
@@ -2801,4 +2802,4 @@
"last-visit": "最終訪問",
"changelog-for-version": "{version} の変更履歴"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 2277816320..3fe42df023 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -1606,7 +1606,8 @@
"new-baseline": {
"self": "Đường cơ sở mới",
"successfully-established": "Đã thiết lập thành công đường cơ sở mới"
- }
+ },
+ "view-diff": "Xem sự khác biệt"
}
},
"project": {
@@ -2801,4 +2802,4 @@
"last-visit": "Lần truy cập cuối",
"changelog-for-version": "Nhật ký thay đổi cho phiên bản {version}"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index a53cc9900f..23e30b9e14 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1606,7 +1606,8 @@
"new-baseline": {
"self": "建立新基线",
"successfully-established": "成功建立新的基线"
- }
+ },
+ "view-diff": "查看差异"
}
},
"project": {
@@ -2801,4 +2802,4 @@
"last-visit": "上次访问",
"changelog-for-version": "{version} 的更新日志"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/store/modules/v1/database.ts b/frontend/src/store/modules/v1/database.ts
index efaaf7ef29..c76d5267fe 100644
--- a/frontend/src/store/modules/v1/database.ts
+++ b/frontend/src/store/modules/v1/database.ts
@@ -255,7 +255,7 @@ export const useDatabaseV1Store = defineStore("database_v1", () => {
};
const fetchDatabaseSchema = async (name: string, sdlFormat = false) => {
const schema = await databaseServiceClient.getDatabaseSchema({
- name,
+ name: `${name}/schema`,
sdlFormat,
});
return schema;
diff --git a/frontend/src/views/DatabaseDetail/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
index 5680f80a0d..ffffd1041a 100644
--- a/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
@@ -4,19 +4,37 @@
tabindex="0"
v-bind="$attrs"
>
- <BBAttention
+ <NAlert
v-if="database.drifted"
type="warning"
:title="$t('database.drifted.schema-drift-detected.self')"
- :description="$t('database.drifted.schema-drift-detected.description')"
- :link="'https://docs.bytebase.com/change-database/drift-detection/?source=console'"
- :action-text="
- database.project !== DEFAULT_PROJECT_NAME
- ? $t('database.drifted.new-baseline.self')
- : undefined
- "
- @click="updateDatabaseDrift"
- />
+ >
+ <div class="flex items-center justify-between gap-4">
+ <div class="flex-1">
+ {{ $t("database.drifted.schema-drift-detected.description") }}
+ <a
+ href="https://docs.bytebase.com/change-database/drift-detection/?source=console"
+ target="_blank"
+ class="text-accent hover:underline ml-1"
+ >
+ {{ $t("common.learn-more") }}
+ </a>
+ </div>
+ <div class="flex justify-end items-center gap-2">
+ <NButton size="small" @click="state.showSchemaDiffModal = true">
+ {{ $t("database.drifted.view-diff") }}
+ </NButton>
+ <NButton
+ v-if="database.project !== DEFAULT_PROJECT_NAME"
+ size="small"
+ type="primary"
+ @click="updateDatabaseDrift"
+ >
+ {{ $t("database.drifted.new-baseline.self") }}
+ </NButton>
+ </div>
+ </div>
+ </NAlert>
<main class="flex-1 relative">
<!-- Highlight Panel -->
@@ -209,6 +227,27 @@
alter-type="SINGLE_DB"
@close="state.showSchemaEditorModal = false"
/>
+
+ <BBModal
+ v-if="state.showSchemaDiffModal"
+ :title="$t('database.drifted.view-diff')"
+ header-class="!border-0"
+ container-class="!pt-0"
+ @close="state.showSchemaDiffModal = false"
+ >
+ <div
+ style="width: calc(100vw - 9rem); height: calc(100vh - 10rem)"
+ class="relative"
+ >
+ <DiffEditor
+ class="w-full h-full border rounded-md overflow-clip"
+ :original="latestChangelogSchema"
+ :modified="currentDatabaseSchema"
+ :readonly="true"
+ language="sql"
+ />
+ </div>
+ </BBModal>
</template>
<script lang="ts" setup>
@@ -216,11 +255,11 @@ import { useTitle } from "@vueuse/core";
import { useClipboard } from "@vueuse/core";
import dayjs from "dayjs";
import { ArrowRightLeftIcon, ClipboardCopyIcon } from "lucide-vue-next";
-import { NButton, NTabPane, NTabs } from "naive-ui";
-import { computed, reactive, watch } from "vue";
+import { NAlert, NButton, NTabPane, NTabs } from "naive-ui";
+import { computed, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter, useRoute } from "vue-router";
-import { BBAttention, BBModal } from "@/bbkit";
+import { BBModal } from "@/bbkit";
import SchemaEditorModal from "@/components/AlterSchemaPrepForm/SchemaEditorModal.vue";
import DatabaseChangelogPanel from "@/components/Database/DatabaseChangelogPanel.vue";
import DatabaseOverviewPanel from "@/components/Database/DatabaseOverviewPanel.vue";
@@ -233,6 +272,7 @@ import {
SchemaDiagramButton,
} from "@/components/DatabaseDetail";
import SyncDatabaseButton from "@/components/DatabaseDetail/SyncDatabaseButton.vue";
+import { DiffEditor } from "@/components/MonacoEditor";
import TransferOutDatabaseForm from "@/components/TransferOutDatabaseForm";
import { Drawer } from "@/components/v2";
import {
@@ -248,6 +288,7 @@ import {
useDatabaseV1ByName,
pushNotification,
useDatabaseV1Store,
+ useChangelogStore,
} from "@/store";
import {
databaseNamePrefix,
@@ -259,6 +300,7 @@ import {
DEFAULT_PROJECT_NAME,
} from "@/types";
import { State } from "@/types/proto/v1/common";
+import { ChangelogView } from "@/types/proto/v1/database_service";
import { DatabaseChangeMode } from "@/types/proto/v1/setting_service";
import {
instanceV1HasAlterSchema,
@@ -282,6 +324,7 @@ interface LocalState {
showTransferDatabaseModal: boolean;
showIncorrectProjectModal: boolean;
showSchemaEditorModal: boolean;
+ showSchemaDiffModal: boolean;
currentProjectName: string;
selectedIndex: number;
selectedTab: DatabaseHash;
@@ -296,11 +339,13 @@ const props = defineProps<{
const { t } = useI18n();
const router = useRouter();
const databaseStore = useDatabaseV1Store();
+const changelogStore = useChangelogStore();
const state = reactive<LocalState>({
showTransferDatabaseModal: false,
showIncorrectProjectModal: false,
showSchemaEditorModal: false,
+ showSchemaDiffModal: false,
currentProjectName: UNKNOWN_PROJECT_NAME,
selectedIndex: 0,
selectedTab: "overview",
@@ -444,4 +489,51 @@ const updateDatabaseDrift = async () => {
title: t("database.drifted.new-baseline.successfully-established"),
});
};
+
+const latestChangelogSchema = ref("-- Loading latest changelog schema...");
+const currentDatabaseSchema = ref("-- Loading current database schema...");
+
+const fetchLatestChangelogSchema = async () => {
+ try {
+ const changelogs = await changelogStore.getOrFetchChangelogListOfDatabase(
+ database.value.name,
+ 1, // Only get the latest one
+ ChangelogView.CHANGELOG_VIEW_FULL
+ );
+ if (changelogs.length > 0) {
+ const latestChangelog = changelogs[0];
+ latestChangelogSchema.value =
+ latestChangelog.schema || "-- No schema found in latest changelog";
+ } else {
+ latestChangelogSchema.value = "-- No changelogs found for this database";
+ }
+ } catch (error) {
+ console.error("Failed to fetch changelog:", error);
+ latestChangelogSchema.value = "-- Failed to load changelog schema";
+ }
+};
+
+const fetchCurrentDatabaseSchema = async () => {
+ try {
+ const schemaResponse = await databaseStore.fetchDatabaseSchema(
+ database.value.name
+ );
+ currentDatabaseSchema.value =
+ schemaResponse.schema || "-- No schema available";
+ } catch (error) {
+ console.error("Failed to fetch database schema:", error);
+ currentDatabaseSchema.value = "-- Failed to load database schema";
+ }
+};
+
+// Fetch data when modal is opened
+watch(
+ () => state.showSchemaDiffModal,
+ (show) => {
+ if (show && database.value.drifted) {
+ fetchLatestChangelogSchema();
+ fetchCurrentDatabaseSchema();
+ }
+ }
+);
</script>