Skip to content

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

chore: use naive-ui select in baseline schema selector (#9360)

Liu Jie67a4a6c460a
diff --git a/frontend/src/components/Branch/BaselineSchemaSelector.vue b/frontend/src/components/Branch/BaselineSchemaSelector.vue
index 6923e9a97d..059e76ee21 100644
--- a/frontend/src/components/Branch/BaselineSchemaSelector.vue
+++ b/frontend/src/components/Branch/BaselineSchemaSelector.vue
@@ -23,6 +23,7 @@
:environment="state.environmentId"
:project="projectId"
:database="state.databaseId"
+ :fallback-option="false"
@update:database="handleDatabaseSelect"
/>
</div>
@@ -30,41 +31,24 @@
<span class="flex w-40 items-center shrink-0 text-sm">
{{ $t("schema-designer.schema-version") }}
</span>
- <div
- class="w-192 flex flex-row justify-start items-center relative"
- :class="isValidId(props.projectId) ? '' : 'opacity-50'"
- >
- <BBSelect
- class="w-full"
- :disabled="props.readonly"
- :selected-item="state.changeHistory"
- :item-list="
- databaseChangeHistoryList(state.databaseId as string)
- "
+ <div class="w-192 flex flex-row justify-start items-center relative">
+ <NSelect
+ :value="state.changeHistory?.name"
+ :options="schemaVersionOptions"
:placeholder="$t('change-history.select')"
- :show-prefix-item="databaseChangeHistoryList(state.databaseId as string).length > 0"
- @select-item="(changeHistory: ChangeHistory) => handleSchemaVersionSelect(changeHistory)"
- >
- <template
- #menuItem="{ item, index }: { item: ChangeHistory, index: number }"
- >
- <div class="flex justify-between mr-2">
- <FeatureBadge
- v-if="index > 0"
- feature="bb.feature.sync-schema-all-versions"
- custom-class="mr-1"
- :instance="database?.instanceEntity"
- />
- <NEllipsis class="flex-1 pr-2" :tooltip="false">
- {{ item.version }} -
- {{ item.description }}
- </NEllipsis>
- <span class="text-control-light">
- {{ humanizeDate(item.updateTime) }}
- </span>
- </div>
- </template>
- </BBSelect>
+ :disabled="
+ $props.readonly ||
+ !isValidId(props.projectId) ||
+ schemaVersionOptions.length === 0
+ "
+ :render-label="renderSchemaVersionLabel"
+ :fallback-option="
+ isMockLatestSchemaChangeHistorySelected
+ ? fallbackSchemaVersionOption
+ : false
+ "
+ @update:value="handleSchemaVersionSelect"
+ />
</div>
</div>
</div>
@@ -72,8 +56,10 @@
<script lang="ts" setup>
import { head, isNull, isUndefined } from "lodash-es";
-import { NEllipsis } from "naive-ui";
-import { computed, reactive, watch } from "vue";
+import { NEllipsis, SelectOption } from "naive-ui";
+import { computed, h, reactive, watch } from "vue";
+import { useI18n } from "vue-i18n";
+import HumanizeDate from "@/components/misc/HumanizeDate.vue";
import { EnvironmentSelect, DatabaseSelect } from "@/components/v2";
import {
useChangeHistoryStore,
@@ -88,6 +74,10 @@ import {
ChangeHistory_Type,
DatabaseMetadataView,
} from "@/types/proto/v1/database_service";
+import {
+ extractChangeHistoryUID,
+ mockLatestSchemaChangeHistory,
+} from "@/utils";
const props = defineProps<{
projectId?: string;
@@ -106,6 +96,7 @@ interface LocalState {
changeHistory?: ChangeHistory;
}
+const { t } = useI18n();
const state = reactive<LocalState>({});
const databaseStore = useDatabaseV1Store();
const dbSchemaStore = useDBSchemaV1Store();
@@ -130,6 +121,97 @@ const prepareChangeHistoryList = async () => {
});
};
+const schemaVersionOptions = computed(() => {
+ const { databaseId } = state;
+ if (!databaseId || databaseId === String(UNKNOWN_ID)) {
+ return [];
+ }
+ const changeHistories = databaseChangeHistoryList(databaseId);
+ if (changeHistories.length === 0) return [];
+ const options: SelectOption[] = [
+ {
+ value: "PLACEHOLDER",
+ label: t("change-history.select"),
+ disabled: true,
+ style: "cursor: default",
+ },
+ ];
+ options.push(
+ ...changeHistories.map<SelectOption>((changeHistory, index) => {
+ return {
+ changeHistory,
+ index,
+ value: changeHistory.name,
+ label: changeHistory.name,
+ class: "bb-baseline-schema-select-option",
+ };
+ })
+ );
+ return options;
+});
+const renderSchemaVersionLabel = (option: SelectOption) => {
+ if (
+ option.value === "PLACEHOLDER" ||
+ option.disabled ||
+ !option.changeHistory
+ ) {
+ return option.label;
+ }
+ const changeHistory = option.changeHistory as ChangeHistory;
+ const { version, description, updateTime } = changeHistory;
+
+ const children = [
+ h(
+ NEllipsis,
+ { class: "flex-1 pr-2", tooltip: false },
+ {
+ default: () => `${version} - ${description}`,
+ }
+ ),
+ ];
+ if (updateTime) {
+ children.push(
+ h(HumanizeDate, {
+ date: updateTime,
+ class: "text-control-light",
+ })
+ );
+ }
+
+ return h("div", { class: "w-full flex justify-between" }, children);
+};
+const isMockLatestSchemaChangeHistorySelected = computed(() => {
+ if (!state.changeHistory) return false;
+ return (
+ extractChangeHistoryUID(state.changeHistory.name) === String(UNKNOWN_ID)
+ );
+});
+const fallbackSchemaVersionOption = (value: string): SelectOption => {
+ if (extractChangeHistoryUID(value) === String(UNKNOWN_ID)) {
+ const { databaseId } = state;
+ if (databaseId && databaseId !== String(UNKNOWN_ID)) {
+ const db = databaseStore.getDatabaseByUID(databaseId);
+ const changeHistory = mockLatestSchemaChangeHistory(db);
+ return {
+ changeHistory,
+ value: changeHistory.name,
+ label: changeHistory.name,
+ };
+ }
+ }
+ return {
+ value: "PLACEHOLDER",
+ disabled: true,
+ label: t("change-history.select"),
+ style: "cursor: default",
+ };
+};
+
+const handleSchemaVersionSelect = (name: string, option: SelectOption) => {
+ const changeHistory = option.changeHistory as ChangeHistory;
+ state.changeHistory = changeHistory;
+};
+
watch(
() => props.databaseId,
async (databaseId) => {
@@ -180,14 +262,10 @@ watch(
const schema = await databaseStore.fetchDatabaseSchema(
`${database.value.name}/schema`
);
- state.changeHistory = {
- name: `${database.value.name}/changeHistories/${UNKNOWN_ID}`,
- uid: String(UNKNOWN_ID),
- updateTime: new Date(),
- schema: schema.schema,
- version: "Latest version",
- description: "the latest schema of database",
- } as ChangeHistory;
+ state.changeHistory = mockLatestSchemaChangeHistory(
+ database.value,
+ schema
+ );
} else {
state.changeHistory = head(list);
}
@@ -252,10 +330,6 @@ const databaseChangeHistoryList = (databaseId: string) => {
return list;
};
-const handleSchemaVersionSelect = (changeHistory: ChangeHistory) => {
- state.changeHistory = changeHistory;
-};
-
watch(
() => state,
() => {
@@ -266,3 +340,8 @@ watch(
{ deep: true }
);
</script>
+<style lang="postcss">
+.bb-baseline-schema-select-option .n-base-select-option__content {
+ @apply w-full;
+}
+</style>
diff --git a/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue b/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue
index a8c88c5b21..ab556c7024 100644
--- a/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue
+++ b/frontend/src/components/SyncDatabaseSchema/DatabaseSchemaSelector.vue
@@ -53,7 +53,6 @@
? fallbackSchemaVersionOption
: false
"
- class="bb-schema-version-select"
@update:value="handleSchemaVersionSelect"
/>
</div>
@@ -86,14 +85,16 @@ import {
useSubscriptionV1Store,
useEnvironmentV1Store,
} from "@/store";
-import { ComposedDatabase, UNKNOWN_ID } from "@/types";
+import { UNKNOWN_ID } from "@/types";
import {
ChangeHistory,
ChangeHistoryView,
ChangeHistory_Type,
- DatabaseSchema,
} from "@/types/proto/v1/database_service";
-import { extractChangeHistoryUID } from "@/utils";
+import {
+ extractChangeHistoryUID,
+ mockLatestSchemaChangeHistory,
+} from "@/utils";
import FeatureBadge from "../FeatureGuard/FeatureBadge.vue";
import HumanizeDate from "../misc/HumanizeDate.vue";
import { ChangeHistorySourceSchema } from "./types";
@@ -334,7 +335,7 @@ const fallbackSchemaVersionOption = (value: string): SelectOption => {
const db = databaseStore.getDatabaseByUID(databaseId);
const changeHistory = mockLatestSchemaChangeHistory(db);
return {
- changeHistory: mockLatestSchemaChangeHistory(db),
+ changeHistory,
index: 0,
value: changeHistory.name,
label: changeHistory.name,
@@ -396,19 +397,6 @@ watch(
}
);
-const mockLatestSchemaChangeHistory = (
- database: ComposedDatabase,
- schema: DatabaseSchema | undefined = undefined
-) => {
- return ChangeHistory.fromPartial({
- name: `${database.name}/changeHistories/${UNKNOWN_ID}`,
- uid: String(UNKNOWN_ID),
- schema: schema?.schema,
- version: "Latest version",
- description: "the latest schema of database",
- });
-};
-
watch(
() => [state, fullViewChangeHistoryCache.value],
() => {
diff --git a/frontend/src/utils/v1/changeHistory.ts b/frontend/src/utils/v1/changeHistory.ts
index a11b34f7a2..2b13764374 100644
--- a/frontend/src/utils/v1/changeHistory.ts
+++ b/frontend/src/utils/v1/changeHistory.ts
@@ -2,10 +2,12 @@ import { isEqual, isUndefined, orderBy, uniqBy } from "lodash-es";
import slug from "slug";
import { t } from "@/plugins/i18n";
import { useDBSchemaV1Store, useDatabaseV1Store } from "@/store";
+import { ComposedDatabase, UNKNOWN_ID } from "@/types";
import { AffectedTable, EmptyAffectedTable } from "@/types/changeHistory";
import {
ChangeHistory,
ChangeHistory_Type,
+ DatabaseSchema,
} from "@/types/proto/v1/database_service";
import { extractDatabaseResourceName } from "./database";
@@ -117,3 +119,16 @@ export const getHistoryChangeType = (type: ChangeHistory_Type) => {
return "-";
}
};
+
+export const mockLatestSchemaChangeHistory = (
+ database: ComposedDatabase,
+ schema: DatabaseSchema | undefined = undefined
+) => {
+ return ChangeHistory.fromPartial({
+ name: `${database.name}/changeHistories/${UNKNOWN_ID}`,
+ uid: String(UNKNOWN_ID),
+ schema: schema?.schema,
+ version: "Latest version",
+ description: "the latest schema of database",
+ });
+};
diff --git a/frontend/src/views/branch/BranchDetail.vue b/frontend/src/views/branch/BranchDetail.vue
index c72bc93f5c..5966edf0d1 100644
--- a/frontend/src/views/branch/BranchDetail.vue
+++ b/frontend/src/views/branch/BranchDetail.vue
@@ -1,10 +1,10 @@
<template>
<template v-if="ready">
<template v-if="isCreating">
- <BranchCreateView :project-id="project?.uid" />
+ <BranchCreateView :project-id="project?.uid" v-bind="$attrs" />
</template>
<template v-else-if="branch">
- <BranchDetailView :branch="branch" />
+ <BranchDetailView :branch="branch" v-bind="$attrs" />
</template>
</template>
</template>