Skip to content

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

refactor: improve database group handling and permissions (#18252)

boojack085cc6ffd2b0
diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.go
index d0de29d606..5ad2fe4934 100644
--- a/backend/api/v1/plan_service.go
+++ b/backend/api/v1/plan_service.go
@@ -1262,6 +1262,9 @@ func getPlanDeployment(ctx context.Context, s *store.Store, specs []*storepb.Pla
if err != nil {
return nil, errors.Wrapf(err, "failed to get database group")
}
+ if databaseGroup == nil {
+ return nil, errors.Errorf("database group %q not found", name)
+ }
matchedDatabases, err := utils.GetMatchedDatabasesInDatabaseGroup(ctx, databaseGroup, allDatabases)
if err != nil {
diff --git a/backend/api/v1/plan_service_plan_check.go b/backend/api/v1/plan_service_plan_check.go
index 1e743aeb02..1fe5a0d3b7 100644
--- a/backend/api/v1/plan_service_plan_check.go
+++ b/backend/api/v1/plan_service_plan_check.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
+ "connectrpc.com/connect"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
@@ -102,6 +103,10 @@ func getPlanCheckRunsFromChangeDatabaseConfigDatabaseGroupTarget(ctx context.Con
databaseGroup, err := getDatabaseGroupByName(ctx, s, target, v1pb.DatabaseGroupView_DATABASE_GROUP_VIEW_FULL)
if err != nil {
+ // If database group was deleted, skip plan checks for it.
+ if connect.CodeOf(err) == connect.CodeNotFound {
+ return nil, nil
+ }
return nil, errors.Wrapf(err, "failed to get database group %q", target)
}
if len(databaseGroup.MatchedDatabases) == 0 {
diff --git a/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue b/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue
index 25813a2833..b3e6eed906 100644
--- a/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue
+++ b/frontend/src/components/DatabaseGroup/DatabaseGroupForm.vue
@@ -46,7 +46,7 @@
</div>
</div>
- <div v-if="!isCreating" class="py-6 border-t">
+ <div v-if="!isCreating && allowDelete" class="py-6 border-t">
<BBButtonConfirm
:type="'DELETE'"
:button-text="$t('database-group.delete-group')"
@@ -109,6 +109,7 @@ import type { Project } from "@/types/proto-es/v1/project_service_pb";
import {
batchConvertCELStringToParsedExpr,
batchConvertParsedExprToCELString,
+ hasProjectPermissionV2,
} from "@/utils";
import { ResourceIdField } from "../v2";
import MatchedDatabaseView from "./MatchedDatabaseView.vue";
@@ -144,6 +145,10 @@ const router = useRouter();
const isCreating = computed(() => props.databaseGroup === undefined);
+const allowDelete = computed(() => {
+ return hasProjectPermissionV2(props.project, "bb.projects.update");
+});
+
onMounted(async () => {
const databaseGroup = props.databaseGroup;
if (!databaseGroup) {
diff --git a/frontend/src/components/Plan/components/RolloutView/taskPermissions.ts b/frontend/src/components/Plan/components/RolloutView/taskPermissions.ts
index 437add04f0..ad8d06659f 100644
--- a/frontend/src/components/Plan/components/RolloutView/taskPermissions.ts
+++ b/frontend/src/components/Plan/components/RolloutView/taskPermissions.ts
@@ -6,6 +6,7 @@ import {
useProjectIamPolicyStore,
} from "@/store";
import { roleNamePrefix, userNamePrefix } from "@/store/modules/v1/common";
+import { isValidDatabaseName } from "@/types";
import type { Issue } from "@/types/proto-es/v1/issue_service_pb";
import { Issue_Type } from "@/types/proto-es/v1/issue_service_pb";
import { PolicyType } from "@/types/proto-es/v1/org_policy_service_pb";
@@ -57,8 +58,10 @@ export const canRolloutTasks = (tasks: Task[], issue?: Issue): boolean => {
return tasks.every((task) => {
// Get database from task target
const database = databaseStore.getDatabaseByName(task.target);
- if (!database || !database.effectiveEnvironment) {
- return false;
+ // If database is not in cache or is unknown, skip environment policy check
+ // and allow rollout (user already lacks bb.taskRuns.create, so this is a fallback).
+ if (!isValidDatabaseName(database.name) || !database.effectiveEnvironment) {
+ return true;
}
// Get rollout policy for the environment
diff --git a/frontend/src/components/Plan/components/SpecDetailView/AllTargetsDrawer.vue b/frontend/src/components/Plan/components/SpecDetailView/AllTargetsDrawer.vue
index d6e54f548d..29e52e217e 100644
--- a/frontend/src/components/Plan/components/SpecDetailView/AllTargetsDrawer.vue
+++ b/frontend/src/components/Plan/components/SpecDetailView/AllTargetsDrawer.vue
@@ -37,20 +37,7 @@
<DatabaseDisplay :database="target" show-environment />
</template>
<template v-else-if="isValidDatabaseGroupName(target)">
- <DatabaseGroupIcon
- class="w-4 h-4 text-control-light shrink-0"
- />
- <DatabaseGroupName
- :database-group="
- dbGroupStore.getDBGroupByName(target)
- "
- :link="false"
- :plain="true"
- class="text-sm"
- />
- <NTag size="tiny" round :bordered="false">
- {{ $t("plan.targets.type.database-group") }}
- </NTag>
+ <DatabaseGroupTargetDisplay :target="target" />
</template>
<template v-else>
<!-- Unknown resource -->
@@ -73,19 +60,18 @@
</template>
<script setup lang="ts">
-import { NTag } from "naive-ui";
import { computed, reactive, watch } from "vue";
import { BBSpin } from "@/bbkit";
-import DatabaseGroupIcon from "@/components/DatabaseGroupIcon.vue";
import { Drawer, DrawerContent, SearchBox } from "@/components/v2";
-import DatabaseGroupName from "@/components/v2/Model/DatabaseGroupName.vue";
import {
batchGetOrFetchDatabases,
useDatabaseV1Store,
useDBGroupStore,
} from "@/store";
import { isValidDatabaseGroupName, isValidDatabaseName } from "@/types";
+import { usePlanContext } from "../../logic/context";
import DatabaseDisplay from "../common/DatabaseDisplay.vue";
+import DatabaseGroupTargetDisplay from "./DatabaseGroupTargetDisplay.vue";
interface Props {
show: boolean;
@@ -104,12 +90,23 @@ defineEmits<{
const databaseStore = useDatabaseV1Store();
const dbGroupStore = useDBGroupStore();
+const { plan } = usePlanContext();
const state = reactive<LocalState>({
searchText: "",
isLoading: false,
});
+// Get databases from deployment mapping for a database group
+const getDatabasesForGroup = (groupName: string): string[] => {
+ const deployment = plan.value.deployment;
+ if (!deployment) return [];
+ const mapping = deployment.databaseGroupMappings.find(
+ (m: { databaseGroup: string }) => m.databaseGroup === groupName
+ );
+ return mapping?.databases ?? [];
+};
+
const filteredTargets = computed(() => {
if (!state.searchText) {
return props.targets;
@@ -141,6 +138,9 @@ const loadAllTargets = async () => {
if (isValidDatabaseGroupName(target)) {
// If target is a valid database group name
dbGroupTargets.push(target);
+ // Also collect databases from deployment mapping for this group
+ const mappedDatabases = getDatabasesForGroup(target);
+ databaseTargets.push(...mappedDatabases);
} else if (isValidDatabaseName(target)) {
databaseTargets.push(target);
}
diff --git a/frontend/src/components/Plan/components/SpecDetailView/DatabaseGroupTargetDisplay.vue b/frontend/src/components/Plan/components/SpecDetailView/DatabaseGroupTargetDisplay.vue
new file mode 100644
index 0000000000..142b24a565
--- /dev/null
+++ b/frontend/src/components/Plan/components/SpecDetailView/DatabaseGroupTargetDisplay.vue
@@ -0,0 +1,123 @@
+<template>
+ <div class="flex flex-col gap-2 w-full">
+ <!-- Database group header -->
+ <div class="flex items-center gap-x-2">
+ <DatabaseGroupIcon class="w-5 h-5 text-control shrink-0" />
+ <NTag size="small" round>
+ {{ $t("common.database-group") }}
+ </NTag>
+ <DatabaseGroupName
+ :database-group="dbGroupStore.getDBGroupByName(target)"
+ :link="false"
+ :plain="true"
+ />
+ <!-- Only show external link if the database group still exists -->
+ <div
+ v-if="showExternalLink && isValidDatabaseGroupName(dbGroupStore.getDBGroupByName(target).name)"
+ class="flex items-center cursor-pointer opacity-60 hover:opacity-100"
+ @click="gotoDatabaseGroupDetailPage"
+ >
+ <ExternalLinkIcon class="w-4 h-auto" />
+ </div>
+ </div>
+ <!-- Database children - flat layout -->
+ <div
+ v-if="databases.length > 0"
+ class="flex flex-wrap items-center gap-2 pl-7"
+ >
+ <div
+ v-for="db in databases.slice(0, MAX_INLINE_DATABASES)"
+ :key="db"
+ class="inline-flex items-center gap-x-1 px-2 py-1 border rounded-lg transition-all cursor-default bg-gray-50"
+ >
+ <DatabaseDisplay :database="db" size="small" show-environment />
+ </div>
+ <NPopover
+ v-if="databases.length > MAX_INLINE_DATABASES"
+ placement="bottom"
+ >
+ <template #trigger>
+ <span class="text-xs text-accent cursor-pointer">
+ {{ $t("common.n-more", { n: databases.length - MAX_INLINE_DATABASES }) }}
+ </span>
+ </template>
+ <NVirtualList
+ class="max-h-64"
+ :item-size="24"
+ :items="extraDatabaseItems"
+ :key-field="'key'"
+ >
+ <template #default="{ item }">
+ <div class="py-1">
+ <DatabaseDisplay
+ :database="item.value"
+ size="small"
+ show-environment
+ />
+ </div>
+ </template>
+ </NVirtualList>
+ </NPopover>
+ </div>
+ </div>
+</template>
+
+<script setup lang="ts">
+import { ExternalLinkIcon } from "lucide-vue-next";
+import { NPopover, NTag, NVirtualList } from "naive-ui";
+import { computed } from "vue";
+import { useRouter } from "vue-router";
+import DatabaseGroupIcon from "@/components/DatabaseGroupIcon.vue";
+import DatabaseGroupName from "@/components/v2/Model/DatabaseGroupName.vue";
+import { PROJECT_V1_ROUTE_DATABASE_GROUP_DETAIL } from "@/router/dashboard/projectV1";
+import { getProjectNameAndDatabaseGroupName, useDBGroupStore } from "@/store";
+import { isValidDatabaseGroupName } from "@/types";
+import { usePlanContext } from "../../logic/context";
+import DatabaseDisplay from "../common/DatabaseDisplay.vue";
+
+const MAX_INLINE_DATABASES = 5;
+
+const props = withDefaults(
+ defineProps<{
+ target: string;
+ showExternalLink?: boolean;
+ }>(),
+ {
+ showExternalLink: true,
+ }
+);
+
+const router = useRouter();
+const dbGroupStore = useDBGroupStore();
+const { plan } = usePlanContext();
+
+const databases = computed(() => {
+ const deployment = plan.value.deployment;
+ if (!deployment) return [];
+ const mapping = deployment.databaseGroupMappings.find(
+ (m: { databaseGroup: string }) => m.databaseGroup === props.target
+ );
+ return mapping?.databases ?? [];
+});
+
+const extraDatabaseItems = computed(() => {
+ return databases.value.slice(MAX_INLINE_DATABASES).map((db) => ({
+ key: db,
+ value: db,
+ }));
+});
+
+const gotoDatabaseGroupDetailPage = () => {
+ const [projectId, databaseGroupName] = getProjectNameAndDatabaseGroupName(
+ props.target
+ );
+ const url = router.resolve({
+ name: PROJECT_V1_ROUTE_DATABASE_GROUP_DETAIL,
+ params: {
+ projectId,
+ databaseGroupName,
+ },
+ }).fullPath;
+ window.open(url, "_blank");
+};
+</script>
diff --git a/frontend/src/components/Plan/components/SpecDetailView/TargetListSection.vue b/frontend/src/components/Plan/components/SpecDetailView/TargetListSection.vue
index d9094caf64..ee2b6ffae4 100644
--- a/frontend/src/components/Plan/components/SpecDetailView/TargetListSection.vue
+++ b/frontend/src/components/Plan/components/SpecDetailView/TargetListSection.vue
@@ -38,25 +38,7 @@
<DatabaseDisplay :database="target" show-environment />
</template>
<template v-else-if="isValidDatabaseGroupName(target)">
- <DatabaseGroupIcon class="w-4 h-4 text-control-light shrink-0" />
- <DatabaseGroupName
- :database-group="
- dbGroupStore.getDBGroupByName(target)
- "
- :link="false"
- :plain="true"
- class="text-sm"
- />
- <NTag size="tiny" round :bordered="false">
- {{ $t("plan.targets.type.database-group") }}
- </NTag>
- <div
- v-if="isValidDatabaseGroupName(dbGroupStore.getDBGroupByName(target).name)"
- class="flex items-center justify-end cursor-pointer opacity-60 hover:opacity-100"
- @click="gotoDatabaseGroupDetailPage(target)"
- >
- <ExternalLinkIcon class="w-4 h-auto" />
- </div>
+ <DatabaseGroupTargetDisplay :target="target" class="px-1 py-1" />
</template>
<template v-else>
<!-- Unknown resource -->
@@ -96,19 +78,13 @@
<script setup lang="ts">
import { create } from "@bufbuild/protobuf";
import { isEqual } from "lodash-es";
-import { ExternalLinkIcon } from "lucide-vue-next";
-import { NButton, NTag } from "naive-ui";
+import { NButton } from "naive-ui";
import { computed, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
-import { useRouter } from "vue-router";
import { BBSpin } from "@/bbkit";
-import DatabaseGroupIcon from "@/components/DatabaseGroupIcon.vue";
-import DatabaseGroupName from "@/components/v2/Model/DatabaseGroupName.vue";
import { planServiceClientConnect } from "@/grpcweb";
-import { PROJECT_V1_ROUTE_DATABASE_GROUP_DETAIL } from "@/router/dashboard/projectV1";
import {
batchGetOrFetchDatabases,
- getProjectNameAndDatabaseGroupName,
projectNamePrefix,
pushNotification,
useDBGroupStore,
@@ -122,12 +98,12 @@ import { usePlanContext } from "../../logic/context";
import DatabaseDisplay from "../common/DatabaseDisplay.vue";
import AllTargetsDrawer from "./AllTargetsDrawer.vue";
import { useSelectedSpec } from "./context";
+import DatabaseGroupTargetDisplay from "./DatabaseGroupTargetDisplay.vue";
import TargetsSelectorDrawer from "./TargetsSelectorDrawer.vue";
const DEFAULT_VISIBLE_TARGETS = 20;
const { t } = useI18n();
-const router = useRouter();
const { plan, isCreating, readonly } = usePlanContext();
const selectedSpec = useSelectedSpec();
const dbGroupStore = useDBGroupStore();
@@ -148,6 +124,16 @@ const targets = computed(() => {
return [];
});
+// Get databases from deployment mapping for a database group
+const getDatabasesForGroup = (groupName: string): string[] => {
+ const deployment = plan.value.deployment;
+ if (!deployment) return [];
+ const mapping = deployment.databaseGroupMappings.find(
+ (m: { databaseGroup: string }) => m.databaseGroup === groupName
+ );
+ return mapping?.databases ?? [];
+};
+
const project = computed(() => {
if (!plan.value?.name) return undefined;
const projectName = `${projectNamePrefix}${extractProjectResourceName(plan.value.name)}`;
@@ -212,6 +198,9 @@ const loadTargetData = async () => {
for (const target of visibleTargets) {
if (isValidDatabaseGroupName(target)) {
dbGroupTargets.push(target);
+ // Also collect databases from deployment mapping for this group
+ const mappedDatabases = getDatabasesForGroup(target);
+ databaseTargets.push(...mappedDatabases);
} else {
databaseTargets.push(target);
}
@@ -235,19 +224,6 @@ const loadTargetData = async () => {
}
};
-const gotoDatabaseGroupDetailPage = (dbGroup: string) => {
- const [projectId, databaseGroupName] =
- getProjectNameAndDatabaseGroupName(dbGroup);
- const url = router.resolve({
- name: PROJECT_V1_ROUTE_DATABASE_GROUP_DETAIL,
- params: {
- projectId,
- databaseGroupName,
- },
- }).fullPath;
- window.open(url, "_blank");
-};
-
// Watch for target changes and load data
watch(
targets,
diff --git a/frontend/src/components/v2/Model/DatabaseGroupName.vue b/frontend/src/components/v2/Model/DatabaseGroupName.vue
index 95e419c435..99fce0d715 100644
--- a/frontend/src/components/v2/Model/DatabaseGroupName.vue
+++ b/frontend/src/components/v2/Model/DatabaseGroupName.vue
@@ -1,11 +1,14 @@
<template>
<component
- :is="link ? 'router-link' : tag"
+ :is="isDeleted ? tag : link ? 'router-link' : tag"
v-bind="bindings"
class="inline-flex items-center gap-x-1"
- :class="link && !plain && 'normal-link'"
+ :class="[
+ !isDeleted && link && !plain && 'normal-link',
+ isDeleted && 'text-control-light line-through',
+ ]"
>
- <span>{{ databaseGroup.title }}</span>
+ <span>{{ isDeleted ? $t("database-group.deleted") : databaseGroup.title }}</span>
</component>
</template>
@@ -13,6 +16,7 @@
import { computed } from "vue";
import { PROJECT_V1_ROUTE_DATABASE_GROUP_DETAIL } from "@/router/dashboard/projectV1";
import { getProjectNameAndDatabaseGroupName } from "@/store/modules/v1/common";
+import { isValidDatabaseGroupName } from "@/types";
import type { DatabaseGroup } from "@/types/proto-es/v1/database_group_service_pb";
const props = withDefaults(
@@ -29,6 +33,10 @@ const props = withDefaults(
}
);
+const isDeleted = computed(() => {
+ return !isValidDatabaseGroupName(props.databaseGroup?.name);
+});
+
const bindings = computed(() => {
if (!props.databaseGroup) {
return {};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index a14de4c794..2f9a3d2a58 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -243,6 +243,7 @@
},
"collapse": "Collapse",
"show-more": "Show more",
+ "n-more": "+{n} more",
"remaining": "remaining",
"line": "Line",
"column": "Column",
@@ -1160,9 +1161,6 @@
"targets": {
"title": "Targets",
"no-targets-found": "No targets found",
- "type": {
- "database-group": "Database Group"
- },
"view-all": "View all ({count})"
},
"select-isolation-level": "Select isolation level",
@@ -2814,6 +2812,7 @@
},
"database-group": {
"create": "New database group",
+ "deleted": "Deleted database group",
"matched-database": "Matched database",
"unmatched-database": "Unmatched database",
"condition": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 9f37e79171..14d1f395cc 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -243,6 +243,7 @@
},
"collapse": "Colapsar",
"show-more": "Mostrar más",
+ "n-more": "+{n} más",
"remaining": "restante",
"line": "Línea",
"column": "Columna",
@@ -1160,9 +1161,6 @@
"targets": {
"title": "Objetivos",
"no-targets-found": "No se encontraron objetivos",
- "type": {
- "database-group": "Grupo de bases de datos"
- },
"view-all": "Ver todo ({count})"
},
"select-isolation-level": "Seleccionar nivel de aislamiento",
@@ -2814,6 +2812,7 @@
},
"database-group": {
"create": "Crear grupo de bases de datos",
+ "deleted": "Grupo de bases de datos eliminado",
"matched-database": "Base de datos coincidente",
"unmatched-database": "Base de datos no coincidente",
"condition": {
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 5971549bd6..073717b8e7 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -243,6 +243,7 @@
},
"collapse": "閉じる",
"show-more": "もっと見る",
+ "n-more": "+{n} もっと",
"remaining": "残り",
"line": "行",
"column": "列",
@@ -1160,9 +1161,6 @@
"targets": {
"title": "ターゲット",
"no-targets-found": "ターゲットが見つかりません",
- "type": {
- "database-group": "データベースグループ"
- },
"view-all": "すべて表示 ({count})"
},
"select-isolation-level": "分離レベルを選択",
@@ -2814,6 +2812,7 @@
},
"database-group": {
"create": "新しいデータベースグループを作成する",
+ "deleted": "削除されたデータベースグループ",
"matched-database": "マッチングデータベース",
"unmatched-database": "データベースが一致しません",
"condition": {
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 9bd3cd6571..232f3085fa 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -243,6 +243,7 @@
},
"collapse": "Sụp đổ",
"show-more": "Hiển thị thêm",
+ "n-more": "+{n} nữa",
"remaining": "còn lại",
"line": "Dòng",
"column": "Cột",
@@ -1160,9 +1161,6 @@
"targets": {
"title": "Mục tiêu",
"no-targets-found": "Không tìm thấy mục tiêu",
- "type": {
- "database-group": "Nhóm cơ sở dữ liệu"
- },
"view-all": "Xem tất cả ({count})"
},
"select-isolation-level": "Chọn mức độ cô lập",
@@ -2814,6 +2812,7 @@
},
"database-group": {
"create": "Nhóm cơ sở dữ liệu mới",
+ "deleted": "Nhóm cơ sở dữ liệu đã xóa",
"matched-database": "Cơ sở dữ liệu phù hợp",
"unmatched-database": "Cơ sở dữ liệu không phù hợp",
"condition": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 34016ee01d..f37b8c0e5e 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -243,6 +243,7 @@
},
"collapse": "收起",
"show-more": "显示更多",
+ "n-more": "+{n} 更多",
"remaining": "剩余",
"line": "行",
"column": "列",
@@ -1160,9 +1161,6 @@
"targets": {
"title": "目标库",
"no-targets-found": "未找到目标库",
- "type": {
- "database-group": "数据库组"
- },
"view-all": "查看全部 ({count})"
},
"select-isolation-level": "选择隔离级别",
@@ -2814,6 +2812,7 @@
},
"database-group": {
"create": "新建数据库分组",
+ "deleted": "已删除的数据库组",
"matched-database": "相匹配的数据库",
"unmatched-database": "不匹配的数据库",
"condition": {