Skip to content

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

feat(frontend): migration history detail improvement (#1604)

Liu Ji3b9789b7d5dd
diff --git a/frontend/src/bbkit/BBModal.vue b/frontend/src/bbkit/BBModal.vue
index b5a623b101..6cdb19a261 100644
--- a/frontend/src/bbkit/BBModal.vue
+++ b/frontend/src/bbkit/BBModal.vue
@@ -11,7 +11,7 @@
>
<div class="relative -mt-4 -ml-4 flex items-center justify-between">
<div class="ml-4 text-xl text-main">
- <component :is="renderTitle" />
+ <slot name="title"><component :is="renderTitle" /></slot>
<component :is="renderSubtitle" />
</div>
<button
@@ -62,7 +62,7 @@ export default defineComponent({
name: "BBModal",
props: {
title: {
- required: true,
+ default: "",
type: String,
},
subtitle: {
diff --git a/frontend/src/components/DatabaseMigrationHistoryPanel.vue b/frontend/src/components/DatabaseMigrationHistoryPanel.vue
index 95ea19640c..f19df7514b 100644
--- a/frontend/src/components/DatabaseMigrationHistoryPanel.vue
+++ b/frontend/src/components/DatabaseMigrationHistoryPanel.vue
@@ -73,9 +73,9 @@
import {
computed,
defineComponent,
+ onBeforeMount,
PropType,
reactive,
- watchEffect,
} from "vue";
import MigrationHistoryTable from "../components/MigrationHistoryTable.vue";
import {
@@ -148,7 +148,7 @@ export default defineComponent({
});
};
- watchEffect(prepareMigrationHistoryList);
+ onBeforeMount(prepareMigrationHistoryList);
const isCurrentUserDBAOrOwner = computed((): boolean => {
return isDBAOrOwner(currentUser.value.role);
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 8d1bb58011..f5317954d0 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -713,13 +713,15 @@
"workflow": "Workflow",
"commit-info": "by {author} at {time}",
"no-schema-change": "this migration has no schema change",
- "schema-drift": "Schema drift detected",
+ "schema-drift": "Schema drift",
+ "schema-drift-detected": "A schema drift was detected.",
+ "view-drift": "View drift",
"before-left-schema-choice": "Compare the schema",
"left-schema-choice-prev-history-schema": "after prev migration",
"left-schema-choice-current-history-schema-prev": "before this migration",
"after-left-schema-choice": "and after this migration",
"show-diff": "Show diff",
- "left-vs-right": "Prev (left) vs This version (right)",
+ "left-vs-right": "Prev {prevLink} vs This version",
"schema-snapshot-after-migration": "The schema snapshot recorded after applying this migration",
"list-limit": "For database having migration history, we list up to 5 most recent histories below. You can click the database name to view all histories.",
"no-history-in-project": "Do not find migration history from any database in this project.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index f592aa6683..db79a3c9db 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -713,13 +713,15 @@
"workflow": "工作流",
"commit-info": "由 {author} 于 {time}",
"no-schema-change": "本次变更没有任何 Schema 的改动",
- "schema-drift": "上次变更后的 Schema 与本次变更前不一致",
+ "schema-drift": "Schema 偏差",
+ "schema-drift-detected": "上次变更后的 Schema 与本次变更前不一致。",
+ "view-drift": "查看偏差",
"before-left-schema-choice": "比较",
"left-schema-choice-prev-history-schema": "上次变更后",
"left-schema-choice-current-history-schema-prev": "本次变更前",
"after-left-schema-choice": "和本次变更后的 Schema",
"show-diff": "显示差异",
- "left-vs-right": "之前 (左) vs 当前版本 (右)",
+ "left-vs-right": "之前{prevLink} vs 当前版本",
"schema-snapshot-after-migration": "完成这次变更后的 schema 快照",
"list-limit": "对于有变更历史的数据库,我们会在下面最多列出5条记录。您可以点击数据库名称去进一步查看全部的记录。",
"no-history-in-project": "这个项目下没有发现任何数据库有变更历史。",
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 532f08fe5b..52302cfe7c 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -753,7 +753,9 @@ const routes: Array<RouteRecordRaw> = [
allowBookmark: true,
},
component: () => import("../views/MigrationHistoryDetail.vue"),
- props: true,
+ props: (to) => ({
+ key: to.fullPath, // force refresh the component when slug changed
+ }),
},
],
},
diff --git a/frontend/src/views/MigrationHistoryDetail.vue b/frontend/src/views/MigrationHistoryDetail.vue
index b6a3b9836c..bdcf45850c 100644
--- a/frontend/src/views/MigrationHistoryDetail.vue
+++ b/frontend/src/views/MigrationHistoryDetail.vue
@@ -124,76 +124,55 @@
</button>
</a>
- <div v-if="hasDrift" class="flex flex-row items-center space-x-2 mt-2">
- <div class="text-sm font-normal text-accent">
- ({{ $t("migration-history.schema-drift") }})
+ <div v-if="hasDrift" class="flex items-center space-x-2 mt-2">
+ <div class="flex items-center text-sm font-normal">
+ <heroicons-outline:exclamation-circle
+ class="w-5 h-5 mr-0.5 text-error"
+ />
+ <span>{{ $t("migration-history.schema-drift-detected") }}</span>
</div>
- <span class="textinfolabel">
- {{ $t("migration-history.before-left-schema-choice") }}
- </span>
- <div>
- <BBSelect
- :selected-item="state.leftSelected"
- :item-list="['previousHistorySchema', 'currentHistorySchemaPrev']"
- @select-item="
- (value) => {
- state.leftSelected = value;
- state.leftSchema =
- state.leftSelected === 'previousHistorySchema'
- ? previousHistorySchema
- : migrationHistory.schemaPrev;
- state.showDiff = state.leftSchema !== state.rightSchema;
- }
- "
- >
- <template #menuItem="{ item: value }">
- {{
- value === "previousHistorySchema"
- ? $t(
- "migration-history.left-schema-choice-prev-history-schema"
- )
- : $t(
- "migration-history.left-schema-choice-current-history-schema-prev"
- )
- }}
- </template>
- </BBSelect>
+ <div class="normal-link text-sm" @click="state.viewDrift = true">
+ {{ $t("migration-history.view-drift") }}
</div>
- <span class="textinfolabel">
- {{ $t("migration-history.after-left-schema-choice") }}
- </span>
</div>
<div class="flex flex-row items-center space-x-2 mt-2">
<BBSwitch
- v-if="state.leftSchema !== state.rightSchema"
+ v-if="allowShowDiff"
:label="$t('migration-history.show-diff')"
:value="state.showDiff"
- @toggle="
- (on: any) => {
- state.showDiff = on;
- }
- "
+ @toggle="state.showDiff = $event"
/>
<div class="textinfolabel">
- {{
- state.showDiff
- ? $t("migration-history.left-vs-right")
- : $t("migration-history.schema-snapshot-after-migration")
- }}
+ <i18n-t
+ v-if="state.showDiff"
+ tag="span"
+ keypath="migration-history.left-vs-right"
+ >
+ <template #prevLink>
+ <router-link
+ v-if="previousHistory"
+ class="normal-link"
+ :to="previousHistoryLink"
+ >
+ ({{ previousHistory.version }})
+ </router-link>
+ </template>
+ </i18n-t>
+ <template v-else>
+ {{ $t("migration-history.schema-snapshot-after-migration") }}
+ </template>
</div>
- <div
- v-if="state.leftSchema === state.rightSchema"
- class="text-sm font-normal text-accent"
- >
+ <div v-if="!allowShowDiff" class="text-sm font-normal text-accent">
({{ $t("migration-history.no-schema-change") }})
</div>
</div>
+
<code-diff
v-if="state.showDiff"
class="mt-4 w-full"
- :old-string="state.leftSchema"
- :new-string="state.rightSchema"
+ :old-string="migrationHistory.schemaPrev"
+ :new-string="migrationHistory.schema"
output-format="side-by-side"
/>
<highlight-code-block
@@ -203,40 +182,69 @@
/>
</div>
</main>
+
+ <BBModal
+ v-if="previousHistory && state.viewDrift"
+ @close="state.viewDrift = false"
+ >
+ <template #title>
+ <span>{{ $t("migration-history.schema-drift") }}</span>
+ <span class="mx-2">-</span>
+ <i18n-t tag="span" keypath="migration-history.left-vs-right">
+ <template #prevLink>
+ <router-link class="normal-link" :to="previousHistoryLink">
+ ({{ previousHistory.version }})
+ </router-link>
+ </template>
+ </i18n-t>
+ </template>
+
+ <div class="space-y-4">
+ <code-diff
+ class="w-full"
+ :old-string="previousHistory.schema"
+ :new-string="migrationHistory.schema"
+ output-format="side-by-side"
+ />
+ <div class="flex justify-end px-4">
+ <button
+ type="button"
+ class="btn-primary"
+ @click.prevent="state.viewDrift = false"
+ >
+ {{ $t("common.close") }}
+ </button>
+ </div>
+ </div>
+ </BBModal>
</div>
</template>
<script lang="ts">
-import { computed, reactive, defineComponent } from "vue";
+import { computed, reactive, defineComponent, onMounted } from "vue";
import { toClipboard } from "@soerenmartius/vue3-clipboard";
import { CodeDiff } from "v-code-diff";
import MigrationHistoryStatusIcon from "../components/MigrationHistoryStatusIcon.vue";
-import { idFromSlug, nanosecondsToString } from "../utils";
+import {
+ idFromSlug,
+ nanosecondsToString,
+ migrationHistorySlug,
+} from "../utils";
import {
MigrationHistory,
MigrationHistoryPayload,
VCSPushEvent,
} from "../types";
-import { BBSelect } from "../bbkit";
import { pushNotification, useDatabaseStore, useInstanceStore } from "@/store";
-type LeftSchemaSelected =
- | "previousHistorySchema" // schema after last migration
- | "currentHistorySchemaPrev"; // schema before this migration
-
interface LocalState {
showDiff: boolean;
- leftSelected: LeftSchemaSelected;
- // leftSchema is the schema snapshot at the left side of the diff.
- // Default to migrationHistory.schemaPrev. If drift is detected, it can be selected to be lastRecordedSchema.
- leftSchema: string;
- // rightSchema is the schema snapshot at the right side of the diff. Always migrationHistory.schema.
- rightSchema: string;
+ viewDrift: boolean;
}
export default defineComponent({
name: "MigrationHistoryDetail",
- components: { CodeDiff, MigrationHistoryStatusIcon, BBSelect },
+ components: { CodeDiff, MigrationHistoryStatusIcon },
props: {
databaseSlug: {
required: true,
@@ -256,6 +264,13 @@ export default defineComponent({
const migrationHistoryId = idFromSlug(props.migrationHistorySlug);
+ onMounted(() => {
+ instanceStore.fetchMigrationHistory({
+ instanceId: database.value.instance.id,
+ databaseName: database.value.name,
+ });
+ });
+
// get all migration histories before (include) the one of given id, ordered by descending version.
const prevMigrationHistoryList = computed((): MigrationHistory[] => {
const migrationHistoryList =
@@ -273,6 +288,7 @@ export default defineComponent({
return migrationHistoryList.filter((mh) => mh.id <= migrationHistoryId);
});
+ // migrationHistory is the latest migration NOW.
const migrationHistory = computed((): MigrationHistory => {
if (prevMigrationHistoryList.value.length > 0) {
return prevMigrationHistoryList.value[0];
@@ -280,12 +296,21 @@ export default defineComponent({
return instanceStore.getMigrationHistoryById(migrationHistoryId)!;
});
- // previousHistorySchema is the schema snapshot of the last migration history before the one of given id.
+ // previousHistory is the last migration history before the one of given id.
// Only referenced if hasDrift is true.
- const previousHistorySchema = computed(
- (): string => prevMigrationHistoryList.value[1].schema
- );
+ const previousHistory = computed((): MigrationHistory | undefined => {
+ return prevMigrationHistoryList.value[1];
+ });
+ // "Show diff" feature is enabled when current migration has changed the schema.
+ const allowShowDiff = computed((): boolean => {
+ return (
+ migrationHistory.value.schema !== migrationHistory.value.schemaPrev
+ );
+ });
+
+ // A schema drift is detected when the schema AFTER previousHistory has been
+ // changed unexpectedly BEFORE current migrationHistory.
const hasDrift = computed((): boolean => {
if (migrationHistory.value.type === "BASELINE") {
return false;
@@ -293,16 +318,22 @@ export default defineComponent({
return (
prevMigrationHistoryList.value.length > 1 && // no drift if no previous migration history
- previousHistorySchema.value !== migrationHistory.value.schemaPrev
+ previousHistory.value?.schema !== migrationHistory.value.schemaPrev
);
});
const state = reactive<LocalState>({
- showDiff:
- migrationHistory.value.schema != migrationHistory.value.schemaPrev,
- leftSelected: "currentHistorySchemaPrev",
- leftSchema: migrationHistory.value.schemaPrev,
- rightSchema: migrationHistory.value.schema,
+ showDiff: allowShowDiff.value, // "Show diff" is turned on by default if available.
+ viewDrift: false,
+ });
+
+ const previousHistoryLink = computed(() => {
+ const previous = previousHistory.value;
+ if (!previous) return "";
+ return `/db/${props.databaseSlug}/history/${migrationHistorySlug(
+ previous.id,
+ previous.version
+ )}`;
});
const pushEvent = computed((): VCSPushEvent | undefined => {
@@ -354,8 +385,10 @@ export default defineComponent({
nanosecondsToString,
database,
migrationHistory,
- previousHistorySchema,
+ previousHistory,
+ allowShowDiff,
hasDrift,
+ previousHistoryLink,
pushEvent,
vcsBranch,
vcsBranchUrl,