Skip to content

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

chore: refactor database detail (#16578)

boojack7ba60cee9cd2
diff --git a/frontend/src/components/DatabaseDetail/DriftedDatabaseAlert.vue b/frontend/src/components/DatabaseDetail/DriftedDatabaseAlert.vue
new file mode 100644
index 0000000000..828747bb99
--- /dev/null
+++ b/frontend/src/components/DatabaseDetail/DriftedDatabaseAlert.vue
@@ -0,0 +1,150 @@
+<template>
+ <NAlert
+ v-if="database.drifted"
+ type="warning"
+ :title="$t('database.drifted.schema-drift-detected.self')"
+ >
+ <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>
+
+ <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>
+import { NAlert, NButton } from "naive-ui";
+import { reactive, ref, watch } from "vue";
+import { useI18n } from "vue-i18n";
+import { BBModal } from "@/bbkit";
+import { DiffEditor } from "@/components/MonacoEditor";
+import {
+ pushNotification,
+ useDatabaseV1Store,
+ useChangelogStore,
+} from "@/store";
+import { DEFAULT_PROJECT_NAME } from "@/types";
+import { ChangelogView } from "@/types/proto/v1/database_service";
+import type { ComposedDatabase } from "@/types";
+
+interface LocalState {
+ showSchemaDiffModal: boolean;
+}
+
+interface Props {
+ database: ComposedDatabase;
+}
+
+const props = defineProps<Props>();
+
+const { t } = useI18n();
+const databaseStore = useDatabaseV1Store();
+const changelogStore = useChangelogStore();
+
+const state = reactive<LocalState>({
+ showSchemaDiffModal: false,
+});
+
+const latestChangelogSchema = ref("-- Loading latest changelog schema...");
+const currentDatabaseSchema = ref("-- Loading current database schema...");
+
+const updateDatabaseDrift = async () => {
+ await databaseStore.updateDatabase({
+ database: {
+ ...props.database,
+ drifted: false,
+ },
+ updateMask: ["drifted"],
+ });
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("database.drifted.new-baseline.successfully-established"),
+ });
+};
+
+const fetchLatestChangelogSchema = async () => {
+ try {
+ const changelogs = await changelogStore.getOrFetchChangelogListOfDatabase(
+ props.database.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(
+ props.database.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 && props.database.drifted) {
+ fetchLatestChangelogSchema();
+ fetchCurrentDatabaseSchema();
+ }
+ }
+);
+</script>
\ No newline at end of file
diff --git a/frontend/src/views/DatabaseDetail/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
index ffffd1041a..01f8e24d43 100644
--- a/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
@@ -4,37 +4,7 @@
tabindex="0"
v-bind="$attrs"
>
- <NAlert
- v-if="database.drifted"
- type="warning"
- :title="$t('database.drifted.schema-drift-detected.self')"
- >
- <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>
+ <DriftedDatabaseAlert :database="database" />
<main class="flex-1 relative">
<!-- Highlight Panel -->
@@ -227,27 +197,6 @@
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>
@@ -255,8 +204,8 @@ import { useTitle } from "@vueuse/core";
import { useClipboard } from "@vueuse/core";
import dayjs from "dayjs";
import { ArrowRightLeftIcon, ClipboardCopyIcon } from "lucide-vue-next";
-import { NAlert, NButton, NTabPane, NTabs } from "naive-ui";
-import { computed, reactive, ref, watch } from "vue";
+import { NButton, NTabPane, NTabs } from "naive-ui";
+import { computed, reactive, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter, useRoute } from "vue-router";
import { BBModal } from "@/bbkit";
@@ -271,8 +220,8 @@ import {
SQLEditorButtonV1,
SchemaDiagramButton,
} from "@/components/DatabaseDetail";
+import DriftedDatabaseAlert from "@/components/DatabaseDetail/DriftedDatabaseAlert.vue";
import SyncDatabaseButton from "@/components/DatabaseDetail/SyncDatabaseButton.vue";
-import { DiffEditor } from "@/components/MonacoEditor";
import TransferOutDatabaseForm from "@/components/TransferOutDatabaseForm";
import { Drawer } from "@/components/v2";
import {
@@ -287,20 +236,13 @@ import {
useEnvironmentV1Store,
useDatabaseV1ByName,
pushNotification,
- useDatabaseV1Store,
- useChangelogStore,
} from "@/store";
import {
databaseNamePrefix,
instanceNamePrefix,
} from "@/store/modules/v1/common";
-import {
- UNKNOWN_PROJECT_NAME,
- unknownEnvironment,
- DEFAULT_PROJECT_NAME,
-} from "@/types";
+import { UNKNOWN_PROJECT_NAME, unknownEnvironment } 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,
@@ -324,7 +266,6 @@ interface LocalState {
showTransferDatabaseModal: boolean;
showIncorrectProjectModal: boolean;
showSchemaEditorModal: boolean;
- showSchemaDiffModal: boolean;
currentProjectName: string;
selectedIndex: number;
selectedTab: DatabaseHash;
@@ -338,14 +279,11 @@ 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",
@@ -474,66 +412,4 @@ const handleCopyDatabaseName = (name: string) => {
});
});
};
-
-const updateDatabaseDrift = async () => {
- await databaseStore.updateDatabase({
- database: {
- ...database.value,
- drifted: false,
- },
- updateMask: ["drifted"],
- });
- pushNotification({
- module: "bytebase",
- style: "SUCCESS",
- 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>