Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: sync instance button (#13939)
ecmadao7d846c9b9bf1
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.goindex 1ff44cb433..a4d159d6c2 100644--- a/backend/api/v1/instance_service.go+++ b/backend/api/v1/instance_service.go@@ -586,7 +586,7 @@ func (s *InstanceService) BatchSyncInstances(ctx context.Context, request *v1pb.updatedInstance, newDatabases, err := s.schemaSyncer.SyncInstance(ctx, instance)if err != nil {- return nil, err+ return nil, status.Errorf(codes.Internal, "failed to sync instance, %v", err)}if r.EnableFullSync {// Sync all databases in the instance asynchronously.diff --git a/frontend/src/components/Instance/InstanceSyncButton.vue b/frontend/src/components/Instance/InstanceSyncButton.vuenew file mode 100644index 0000000000..03e031676f--- /dev/null+++ b/frontend/src/components/Instance/InstanceSyncButton.vue@@ -0,0 +1,122 @@+<template>+ <NDropdown+ v-if="allowSyncInstance"+ :trigger="'click'"+ :options="syncInstnceOptions"+ :render-label="renderDropdownLabel"+ :disabled="disabled || syncingSchema"+ @select="syncSchema"+ >+ <NButton+ icon-placement="right"+ :loading="syncingSchema"+ :quaternary="quaternary"+ :disabled="disabled"+ :size="size"+ :type="type"+ >+ <template #icon>+ <ChevronDownIcon class="w-4" />+ </template>+ <template v-if="syncingSchema">+ {{ $t("instance.syncing") }}+ </template>+ <template v-else>+ {{ $t("instance.sync.self") }}+ </template>+ </NButton>+ </NDropdown>+</template>++<script lang="tsx" setup>+import { ChevronDownIcon } from "lucide-vue-next";+import type { DropdownOption } from "naive-ui";+import { NButton, NDropdown, NTooltip } from "naive-ui";+import type { ClientError } from "nice-grpc-web";+import { computed, ref } from "vue";+import { useI18n } from "vue-i18n";+import { pushNotification } from "@/store";+import { hasWorkspacePermissionV2 } from "@/utils";++withDefaults(+ defineProps<{+ type: "default" | "primary";+ size: "small" | "medium";+ disabled?: boolean;+ quaternary?: boolean;+ }>(),+ {+ type: "default",+ size: "medium",+ disabled: false,+ quaternary: false,+ }+);++const emit = defineEmits<{+ (name: "sync-schema", enableFullSync: boolean): void;+}>();++const { t } = useI18n();+const syncingSchema = ref<boolean>(false);++type SyncInstanceOption = "sync-all" | "sync-new";++const renderDropdownLabel = (option: DropdownOption) => {+ if (option.key === "sync-new") {+ return <span>{option.label}</span>;+ }+ return (+ <NTooltip placement="top-start">+ {{+ trigger: () => option.label,+ default: () => (+ <span class="text-sm text-nowrap">+ {t("instance.sync.sync-all-tip")}+ </span>+ ),+ }}+ </NTooltip>+ );+};++const syncInstnceOptions = computed((): DropdownOption[] => {+ return [+ {+ key: "sync-all",+ label: t("instance.sync.sync-all"),+ },+ {+ key: "sync-new",+ label: t("instance.sync.sync-new"),+ },+ ];+});++const allowSyncInstance = computed(() => {+ return hasWorkspacePermissionV2("bb.instances.sync");+});++const syncSchema = async (option: SyncInstanceOption) => {+ try {+ syncingSchema.value = true;+ if (option === "sync-all") {+ pushNotification({+ module: "bytebase",+ style: "SUCCESS",+ title: t("instance.sync.sync-all-tip"),+ });+ }+ emit("sync-schema", option === "sync-all" /* enable full sync */);+ } catch (error) {+ pushNotification({+ module: "bytebase",+ style: "CRITICAL",+ title: t("db.failed-to-sync-schema"),+ description: (error as ClientError).details,+ });+ } finally {+ syncingSchema.value = false;+ }+};+</script>diff --git a/frontend/src/components/v2/Model/Instance/InstanceV1Table/InstanceOperations.vue b/frontend/src/components/v2/Model/Instance/InstanceV1Table/InstanceOperations.vueindex d0eb62379f..d5606f8d10 100644--- a/frontend/src/components/v2/Model/Instance/InstanceV1Table/InstanceOperations.vue+++ b/frontend/src/components/v2/Model/Instance/InstanceV1Table/InstanceOperations.vue@@ -10,14 +10,16 @@}}<div class="flex items-center"><template v-for="action in actions" :key="action.text">+ <component :is="action.render()" v-if="action.render" /><NButton+ v-elsequaternarysize="small"type="primary":disabled="action.disabled"@click="action.click">- <template #icon>+ <template v-if="action.icon" #icon><component :is="action.icon" class="h-4 w-4" /></template><span class="text-sm">{{ action.text }}</span>@@ -32,22 +34,24 @@/></template>-<script setup lang="ts">-import { GraduationCapIcon, RefreshCwIcon } from "lucide-vue-next";+<script setup lang="tsx">+import { GraduationCapIcon } from "lucide-vue-next";import { NButton } from "naive-ui";import type { VNode } from "vue";import { computed, h, reactive } from "vue";import { useI18n } from "vue-i18n";+import InstanceSyncButton from "@/components/Instance/InstanceSyncButton.vue";import InstanceAssignment from "@/components/InstanceAssignment.vue";import { useInstanceV1Store, pushNotification } from "@/store";import type { ComposedInstance } from "@/types";import { hasWorkspacePermissionV2 } from "@/utils";interface Action {- icon: VNode;+ icon?: VNode;+ render?: () => VNode;text: string;- disabled: boolean;- click: () => void;+ disabled?: boolean;+ click?: () => void;}interface LocalState {@@ -71,13 +75,16 @@ const actions = computed((): Action[] => {// We'll always show the sync button, even if the user doesn't have the permission to sync.// If the user doesn't have the permission, the button will be disabled.{- icon: h(RefreshCwIcon),- text: t("common.sync"),- disabled:- props.instanceList.length < 1 ||- state.loading ||- !hasWorkspacePermissionV2("bb.instances.sync"),- click: syncSchema,+ render: () => (+ <InstanceSyncButton+ size={"small"}+ type={"primary"}+ quaternary={true}+ disabled={props.instanceList.length < 1}+ onSync-schema={syncSchema}+ />+ ),+ text: "",},];@@ -92,25 +99,15 @@ const actions = computed((): Action[] => {return list;});-const syncSchema = async () => {- try {- state.loading = true;- await instanceStore.batchSyncInstances(- props.instanceList.map((instance) => instance.name)- );- pushNotification({- module: "bytebase",- style: "INFO",- title: t("db.start-to-sync-schema"),- });- } catch {- pushNotification({- module: "bytebase",- style: "CRITICAL",- title: t("db.failed-to-sync-schema"),- });- } finally {- state.loading = false;- }+const syncSchema = async (enableFullSync: boolean) => {+ await instanceStore.batchSyncInstances(+ props.instanceList.map((instance) => instance.name),+ enableFullSync+ );+ pushNotification({+ module: "bytebase",+ style: "INFO",+ title: t("db.start-to-sync-schema"),+ });};</script>diff --git a/frontend/src/store/modules/v1/instance.ts b/frontend/src/store/modules/v1/instance.tsindex ab954189e6..591c4923e4 100644--- a/frontend/src/store/modules/v1/instance.ts+++ b/frontend/src/store/modules/v1/instance.ts@@ -85,9 +85,12 @@ export const useInstanceV1Store = defineStore("instance_v1", () => {enableFullSync,});};- const batchSyncInstances = async (instanceNameList: string[]) => {+ const batchSyncInstances = async (+ instanceNameList: string[],+ enableFullSync: boolean+ ) => {await instanceServiceClient.batchSyncInstances({- requests: instanceNameList.map((name) => ({ name })),+ requests: instanceNameList.map((name) => ({ name, enableFullSync })),});};const fetchInstanceByName = async (name: string, silent = false) => {diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vueindex e03fc0fa4d..9945bcd92d 100644--- a/frontend/src/views/InstanceDetail.vue+++ b/frontend/src/views/InstanceDetail.vue@@ -12,26 +12,10 @@<NTabs><template #suffix><div class="space-x-2">- <NDropdown- v-if="allowSyncInstance"- :trigger="'click'"- :options="syncInstnceOptions"- :render-label="renderDropdownLabel"- :disabled="state.syncingSchema"- @select="syncSchema"- >- <NButton icon-placement="right" :loading="state.syncingSchema">- <template #icon>- <ChevronDownIcon class="w-4" />- </template>- <template v-if="state.syncingSchema">- {{ $t("instance.syncing") }}- </template>- <template v-else>- {{ $t("instance.sync.self") }}- </template>- </NButton>- </NDropdown>+ <InstanceSyncButton+ v-if="instance.state === State.ACTIVE"+ @sync-schema="syncSchema"+ /><NButtonv-if="allowCreateDatabase"type="primary"@@ -81,16 +65,14 @@<script lang="tsx" setup>import { useTitle } from "@vueuse/core";-import { ChevronDownIcon } from "lucide-vue-next";-import type { DropdownOption } from "naive-ui";-import { NButton, NTabPane, NTabs, NDropdown, NTooltip } from "naive-ui";-import type { ClientError } from "nice-grpc-web";+import { NButton, NTabPane, NTabs } from "naive-ui";import { computed, reactive } from "vue";import { useI18n } from "vue-i18n";import { useRouter } from "vue-router";import ArchiveBanner from "@/components/ArchiveBanner.vue";import { CreateDatabasePrepPanel } from "@/components/CreateDatabasePrepForm";import { EngineIcon } from "@/components/Icon";+import InstanceSyncButton from "@/components/Instance/InstanceSyncButton.vue";import {InstanceForm,Form as InstanceFormBody,@@ -116,7 +98,6 @@ import { DatabaseChangeMode } from "@/types/proto/v1/setting_service";import {instanceV1HasCreateDatabase,instanceV1Name,- hasWorkspacePermissionV2,hasWorkspaceLevelProjectPermissionInAnyProject,wrapRefAsPromise,autoDatabaseRoute,@@ -165,39 +146,6 @@ const environment = computed(() => {);});-type syncInstanceKey = "sync-all" | "sync-new";--const renderDropdownLabel = (option: DropdownOption) => {- if (option.key === "sync-new") {- return <span>{option.label}</span>;- }- return (- <NTooltip placement="top-start">- {{- trigger: () => option.label,- default: () => (- <span class="text-sm text-nowrap">- {t("instance.sync.sync-all-tip")}- </span>- ),- }}- </NTooltip>- );-};--const syncInstnceOptions = computed((): DropdownOption[] => {- return [- {- key: "sync-all",- label: t("instance.sync.sync-all"),- },- {- key: "sync-new",- label: t("instance.sync.sync-new"),- },- ];-});-const { databaseList, listCache, ready } = useDatabaseV1List(instance.value.name);@@ -206,13 +154,6 @@ const instanceRoleList = computed(() => {return instance.value.roles;});-const allowSyncInstance = computed(() => {- return (- instance.value.state === State.ACTIVE &&- hasWorkspacePermissionV2("bb.instances.sync")- );-});-const allowCreateDatabase = computed(() => {return (databaseChangeMode.value === DatabaseChangeMode.PIPELINE &&@@ -222,50 +163,25 @@ const allowCreateDatabase = computed(() => {);});-const syncSchema = async (key: syncInstanceKey) => {- state.syncingSchema = true;- try {- await instanceV1Store.syncInstance(- instance.value,- key === "sync-all" /* enable full sync */- );- if (key === "sync-all") {- pushNotification({- module: "bytebase",- style: "SUCCESS",- title: t("instance.sync.sync-all-tip"),- });- }- // Remove the database list cache for the instance.- listCache.cacheMap.delete(listCache.getCacheKey(instance.value.name));- await wrapRefAsPromise(ready, true);- // Clear the db schema metadata cache entities.- // So we will re-fetch new values when needed.- const dbSchemaStore = useDBSchemaV1Store();- databaseList.value.forEach((database) =>- dbSchemaStore.removeCache(database.name)- );- pushNotification({- module: "bytebase",- style: "SUCCESS",- title: t(- "instance.successfully-synced-schema-for-instance-instance-value-name",- [instance.value.title]- ),- });- } catch (error) {- pushNotification({- module: "bytebase",- style: "CRITICAL",- title: t(- "instance.failed-to-sync-schema-for-instance-instance-value-name",- [instance.value.title]- ),- description: (error as ClientError).details,- });- } finally {- state.syncingSchema = false;- }+const syncSchema = async (enableFullSync: boolean) => {+ await instanceV1Store.syncInstance(instance.value, enableFullSync);+ // Remove the database list cache for the instance.+ listCache.cacheMap.delete(listCache.getCacheKey(instance.value.name));+ await wrapRefAsPromise(ready, true);+ // Clear the db schema metadata cache entities.+ // So we will re-fetch new values when needed.+ const dbSchemaStore = useDBSchemaV1Store();+ databaseList.value.forEach((database) =>+ dbSchemaStore.removeCache(database.name)+ );+ pushNotification({+ module: "bytebase",+ style: "SUCCESS",+ title: t(+ "instance.successfully-synced-schema-for-instance-instance-value-name",+ [instance.value.title]+ ),+ });};const createDatabase = () => {