Skip to content

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

chore(frontend): DiffSchema error in Sync Schema page (#10791)

Liu Ji5751186c0af1
diff --git a/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue b/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue
index 6667a543d4..0016612b2a 100644
--- a/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue
+++ b/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue
@@ -45,7 +45,8 @@
</span>
<div class="w-192 flex flex-row justify-start items-center relative">
<NSelect
- :value="state.changeHistory?.name"
+ :loading="isPreparingSchemaVersionOptions"
+ :value="state.changeHistoryName"
:options="schemaVersionOptions"
:placeholder="$t('change-history.select')"
:disabled="
@@ -75,7 +76,7 @@
import { computedAsync } from "@vueuse/core";
import { head, isNull, isUndefined } from "lodash-es";
import { NEllipsis, NSelect, SelectOption } from "naive-ui";
-import { computed, h, onMounted, reactive, watch } from "vue";
+import { computed, h, reactive, ref, watch } from "vue";
import { VNodeArrayChildren } from "vue";
import { useI18n } from "vue-i18n";
import {
@@ -85,10 +86,10 @@ import {
} from "@/components/v2";
import {
useChangeHistoryStore,
- useDBSchemaV1Store,
useDatabaseV1Store,
useSubscriptionV1Store,
useEnvironmentV1Store,
+ useDBSchemaV1Store,
} from "@/store";
import { UNKNOWN_ID } from "@/types";
import {
@@ -120,9 +121,7 @@ interface LocalState {
projectId?: string;
environmentId?: string;
databaseId?: string;
- changeHistory?: ChangeHistory;
- // conciseHistory is used for Oracle only.
- conciseHistory?: string;
+ changeHistoryName?: string;
}
const state = reactive<LocalState>({
@@ -142,6 +141,9 @@ const database = computed(() => {
return databaseStore.getDatabaseByUID(databaseId);
});
+const isPreparingSchemaVersionOptions = ref(false);
+const isFetchingChangeHistorySourceSchema = ref(false);
+
const hasSyncSchemaFeature = computed(() => {
return useSubscriptionV1Store().hasInstanceFeature(
"bb.feature.sync-schema-all-versions",
@@ -149,38 +151,6 @@ const hasSyncSchemaFeature = computed(() => {
);
});
-onMounted(async () => {
- if (props.selectState?.databaseId) {
- try {
- const database = await databaseStore.getOrFetchDatabaseByUID(
- props.selectState.databaseId || ""
- );
- const environment = await environmentStore.getOrFetchEnvironmentByName(
- database.effectiveEnvironment
- );
- state.projectId = props.selectState.projectId;
- state.databaseId = database.uid;
- state.environmentId = environment.uid;
- state.changeHistory = props.selectState.changeHistory;
- } catch (error) {
- // do nothing.
- }
- } else if (props.selectState?.projectId) {
- state.projectId = props.selectState.projectId;
- }
-});
-
-const fullViewChangeHistory = computedAsync(async () => {
- const name = state.changeHistory?.name;
- if (!name) {
- return undefined;
- }
- return changeHistoryStore.getOrFetchChangeHistoryByName(
- name,
- ChangeHistoryView.CHANGE_HISTORY_VIEW_FULL
- );
-}, undefined);
-
const allowedMigrationTypeList: ChangeHistory_Type[] = [
ChangeHistory_Type.BASELINE,
ChangeHistory_Type.MIGRATE,
@@ -309,9 +279,9 @@ const renderSchemaVersionLabel = (option: SelectOption) => {
return h("div", { class: "w-full flex justify-between" }, children);
};
const isMockLatestSchemaChangeHistorySelected = computed(() => {
- if (!state.changeHistory) return false;
+ if (!state.changeHistoryName) return false;
return (
- extractChangeHistoryUID(state.changeHistory.name) === String(UNKNOWN_ID)
+ extractChangeHistoryUID(state.changeHistoryName) === String(UNKNOWN_ID)
);
});
const fallbackSchemaVersionOption = (value: string): SelectOption => {
@@ -348,57 +318,115 @@ const handleSchemaVersionSelect = async (
state.showFeatureModal = true;
return;
}
- if (
- database.value &&
- instanceV1SupportsConciseSchema(database.value.instanceEntity)
- ) {
- const conciseHistory = await changeHistoryStore.fetchChangeHistory({
- name: changeHistory.name,
- view: ChangeHistoryView.CHANGE_HISTORY_VIEW_FULL,
- concise: true,
- });
- state.conciseHistory = conciseHistory.schema;
- }
- state.changeHistory = changeHistory;
+ state.changeHistoryName = changeHistory.name;
};
+const mergedChangeHistorySourceSchema = computedAsync(
+ async (): Promise<
+ | Pick<ChangeHistorySourceSchema, "changeHistory" | "conciseHistory">
+ | undefined
+ > => {
+ const { databaseId, changeHistoryName } = state;
+ if (!isValidId(databaseId)) {
+ return undefined;
+ }
+ if (!changeHistoryName) {
+ return undefined;
+ }
+
+ const database = databaseStore.getDatabaseByUID(databaseId);
+ if (database) {
+ if (isMockLatestSchemaChangeHistorySelected.value) {
+ // If database has no migration history, we will use its latest schema.
+ const schema = await databaseStore.fetchDatabaseSchema(
+ `${database.name}/schema`
+ );
+ const changeHistory = mockLatestSchemaChangeHistory(database, schema);
+ if (instanceV1SupportsConciseSchema(database.instanceEntity)) {
+ const conciseSchema = await databaseStore.fetchDatabaseSchema(
+ `${database.name}/schema`,
+ /* sdlFormat */ false,
+ /* concise */ instanceV1SupportsConciseSchema(
+ database.instanceEntity
+ )
+ );
+ const conciseHistory = conciseSchema.schema;
+ return {
+ changeHistory,
+ conciseHistory,
+ };
+ } else {
+ return {
+ changeHistory,
+ };
+ }
+ } else {
+ // Fetch the FULL view of changeHistory
+ const changeHistory = await changeHistoryStore.fetchChangeHistory({
+ name: changeHistoryName,
+ view: ChangeHistoryView.CHANGE_HISTORY_VIEW_FULL,
+ });
+
+ if (instanceV1SupportsConciseSchema(database.instanceEntity)) {
+ const conciseHistory = await changeHistoryStore.fetchChangeHistory({
+ name: changeHistoryName,
+ view: ChangeHistoryView.CHANGE_HISTORY_VIEW_FULL,
+ concise: true,
+ });
+ return {
+ changeHistory,
+ conciseHistory: conciseHistory.schema,
+ };
+ } else {
+ return {
+ changeHistory,
+ };
+ }
+ }
+ } else {
+ return undefined;
+ }
+ },
+ undefined,
+ { evaluating: isFetchingChangeHistorySourceSchema }
+);
+
watch(
() => [state.databaseId],
async () => {
const databaseId = state.databaseId;
if (!isValidId(databaseId)) {
- state.changeHistory = undefined;
+ state.changeHistoryName = undefined;
return;
}
const database = databaseStore.getDatabaseByUID(databaseId);
if (database) {
- const changeHistoryList = (
- await changeHistoryStore.getOrFetchChangeHistoryListOfDatabase(
- database.name
- )
- ).filter((changeHistory) =>
- allowedMigrationTypeList.includes(changeHistory.type)
- );
-
- if (changeHistoryList.length > 0) {
- // Default select the first migration history.
- state.changeHistory = head(changeHistoryList);
- } else {
- // If database has no migration history, we will use its latest schema.
- const schema = await databaseStore.fetchDatabaseSchema(
- `${database.name}/schema`
- );
- state.changeHistory = mockLatestSchemaChangeHistory(database, schema);
- const conciseSchema = await databaseStore.fetchDatabaseSchema(
- `${database.name}/schema`,
- /* sdlFormat */ false,
- /* concise */ instanceV1SupportsConciseSchema(database.instanceEntity)
+ try {
+ isPreparingSchemaVersionOptions.value = true;
+ const changeHistoryList = (
+ await changeHistoryStore.getOrFetchChangeHistoryListOfDatabase(
+ database.name
+ )
+ ).filter((changeHistory) =>
+ allowedMigrationTypeList.includes(changeHistory.type)
);
- state.conciseHistory = conciseSchema.schema;
+
+ if (changeHistoryList.length > 0) {
+ // Default select the first migration history.
+ state.changeHistoryName = head(changeHistoryList)?.name;
+ } else {
+ // If database has no migration history, we will use its latest schema.
+ state.changeHistoryName = mockLatestSchemaChangeHistory(
+ database,
+ undefined
+ ).name;
+ }
+ } finally {
+ isPreparingSchemaVersionOptions.value = false;
}
} else {
- state.changeHistory = undefined;
+ state.changeHistoryName = undefined;
}
}
);
@@ -408,25 +436,17 @@ watch(
() => state.projectId,
() => state.environmentId,
() => state.databaseId,
- () => state.changeHistory,
- fullViewChangeHistory,
- () => state.conciseHistory,
+ mergedChangeHistorySourceSchema,
+ isFetchingChangeHistorySourceSchema,
],
- ([
- projectId,
- environmentId,
- databaseId,
- basicViewChangeHistory,
- fullViewChangeHistory,
- conciseHistory,
- ]) => {
- const changeHistory = fullViewChangeHistory ?? basicViewChangeHistory;
+ ([projectId, environmentId, databaseId, source, isFetching]) => {
const params: ChangeHistorySourceSchema = {
projectId,
environmentId,
databaseId,
- changeHistory,
- conciseHistory,
+ changeHistory: source?.changeHistory,
+ conciseHistory: source?.conciseHistory,
+ isFetching,
};
emit("update", params);
},
@@ -434,6 +454,36 @@ watch(
immediate: true,
}
);
+
+watch(
+ [() => props.selectState?.databaseId, () => props.selectState?.projectId],
+ async ([databaseId, projectId]) => {
+ if (databaseId) {
+ try {
+ const database = await databaseStore.getOrFetchDatabaseByUID(
+ databaseId
+ );
+ const environment = await environmentStore.getOrFetchEnvironmentByName(
+ database.effectiveEnvironment
+ );
+ state.projectId = projectId;
+ state.databaseId = database.uid;
+ state.environmentId = environment.uid;
+ state.changeHistoryName = props.selectState?.changeHistory?.name;
+ } catch (error) {
+ // do nothing.
+ }
+ } else if (projectId) {
+ state.projectId = projectId;
+ state.databaseId = undefined;
+ state.environmentId = undefined;
+ state.changeHistoryName = undefined;
+ }
+ },
+ {
+ immediate: true,
+ }
+);
</script>
<style lang="postcss">
diff --git a/frontend/src/components/SyncDatabaseSchema/index.vue b/frontend/src/components/SyncDatabaseSchema/index.vue
index cd7180b137..1a456dfc7e 100644
--- a/frontend/src/components/SyncDatabaseSchema/index.vue
+++ b/frontend/src/components/SyncDatabaseSchema/index.vue
@@ -14,6 +14,7 @@
:allow-next="allowNext"
:finish-title="$t('database.sync-schema.preview-issue')"
pane-class="flex-1 overflow-y-auto"
+ :next-button-props="nextButtonProps"
@cancel="cancelSetup"
@update:current-index="tryChangeStep"
@finish="tryFinishSetup"
@@ -63,7 +64,7 @@
<script lang="ts" setup>
import dayjs from "dayjs";
import { isNull, isUndefined } from "lodash-es";
-import { NRadioGroup, NRadio, useDialog } from "naive-ui";
+import { NRadioGroup, NRadio, useDialog, ButtonProps } from "naive-ui";
import { computed, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
@@ -156,6 +157,7 @@ const allowNext = computed(() => {
if (state.currentStep === SELECT_SOURCE_SCHEMA) {
if (state.sourceSchemaType === "SCHEMA_HISTORY_VERSION") {
return (
+ !changeHistorySourceSchemaState.isFetching &&
isValidId(changeHistorySourceSchemaState.environmentId) &&
isValidId(changeHistorySourceSchemaState.databaseId) &&
!isUndefined(changeHistorySourceSchemaState.changeHistory)
@@ -184,6 +186,19 @@ const allowNext = computed(() => {
}
});
+const nextButtonProps = computed((): ButtonProps | undefined => {
+ if (state.currentStep === SELECT_SOURCE_SCHEMA) {
+ if (state.sourceSchemaType === "SCHEMA_HISTORY_VERSION") {
+ if (changeHistorySourceSchemaState.isFetching) {
+ return {
+ loading: true,
+ };
+ }
+ }
+ }
+ return undefined;
+});
+
const handleRawSQLStateChange = (state: RawSQLState) => {
Object.assign(rawSQLState, state);
};
diff --git a/frontend/src/components/SyncDatabaseSchema/types.ts b/frontend/src/components/SyncDatabaseSchema/types.ts
index 751334e628..dc607615a7 100644
--- a/frontend/src/components/SyncDatabaseSchema/types.ts
+++ b/frontend/src/components/SyncDatabaseSchema/types.ts
@@ -9,6 +9,7 @@ export interface ChangeHistorySourceSchema {
databaseId?: string;
changeHistory?: ChangeHistory;
conciseHistory?: string;
+ isFetching?: boolean;
}
export interface RawSQLState {
diff --git a/frontend/src/components/v2/Form/StepTab.vue b/frontend/src/components/v2/Form/StepTab.vue
index 83a4f579a9..fedb35a44c 100644
--- a/frontend/src/components/v2/Form/StepTab.vue
+++ b/frontend/src/components/v2/Form/StepTab.vue
@@ -38,6 +38,7 @@
<div class="flex items-center justify-between space-x-2">
<NButton
v-if="currentIndex != 0"
+ v-bind="backButtonProps"
@click.prevent="$emit('update:currentIndex', currentIndex - 1)"
>
<heroicons-outline:chevron-left
@@ -49,6 +50,7 @@
v-if="currentIndex == stepList.length - 1"
:disabled="!allowNext"
type="primary"
+ v-bind="finishButtonProps"
@click.prevent="$emit('finish')"
>
{{ $t(finishTitle) }}
@@ -57,6 +59,7 @@
v-else-if="!stepList[currentIndex].hideNext"
:disabled="!allowNext"
type="primary"
+ v-bind="nextButtonProps"
@click.prevent="$emit('update:currentIndex', currentIndex + 1)"
>
{{ $t("bbkit.common.next") }}
@@ -67,7 +70,7 @@
</template>
<script lang="ts" setup>
-import { NSteps, NStep, NButton } from "naive-ui";
+import { NSteps, NStep, NButton, ButtonProps } from "naive-ui";
import { VueClass } from "@/utils/types";
withDefaults(
@@ -80,6 +83,9 @@ withDefaults(
paneClass?: VueClass;
footerClass?: VueClass;
stepList: { title: string; description?: string; hideNext?: boolean }[];
+ backButtonProps?: ButtonProps;
+ nextButtonProps?: ButtonProps;
+ finishButtonProps?: ButtonProps;
}>(),
{
showCancel: true,
@@ -88,6 +94,9 @@ withDefaults(
finishTitle: "bbkit.common.finish",
paneClass: undefined,
footerClass: undefined,
+ backButtonProps: undefined,
+ nextButtonProps: undefined,
+ finishButtonProps: undefined,
}
);
diff --git a/frontend/src/utils/v1/changeHistory.ts b/frontend/src/utils/v1/changeHistory.ts
index 0b401201b6..b8ae230907 100644
--- a/frontend/src/utils/v1/changeHistory.ts
+++ b/frontend/src/utils/v1/changeHistory.ts
@@ -1,4 +1,5 @@
import { isEqual, isUndefined, orderBy, uniqBy } from "lodash-es";
+import Long from "long";
import { t } from "@/plugins/i18n";
import { useDBSchemaV1Store, useDatabaseV1Store } from "@/store";
import { ComposedDatabase, UNKNOWN_ID } from "@/types";
@@ -119,6 +120,9 @@ export const mockLatestSchemaChangeHistory = (
name: `${database.name}/changeHistories/${UNKNOWN_ID}`,
uid: String(UNKNOWN_ID),
schema: schema?.schema,
+ schemaSize: Long.fromNumber(
+ new TextEncoder().encode(schema?.schema).length
+ ),
version: "Latest version",
description: "the latest schema of database",
});