Skip to content

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

feat(frontend): implement release and revision store (#13869)

boojackba61d1ae680b
diff --git a/frontend/src/components/Database/DatabaseChangeHistoryPanel.vue b/frontend/src/components/Database/DatabaseChangeHistoryPanel.vue
index 8941ef1bb1..f29754f84f 100644
--- a/frontend/src/components/Database/DatabaseChangeHistoryPanel.vue
+++ b/frontend/src/components/Database/DatabaseChangeHistoryPanel.vue
@@ -140,11 +140,8 @@ import { useDatabaseDetailContext } from "@/components/Database/context";
import { TooltipButton } from "@/components/v2";
import { Drawer, DrawerContent } from "@/components/v2";
import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
-import {
- DEFAULT_CHANGE_HISTORY_PAGE_SIZE,
- useChangeHistoryStore,
- useDBSchemaV1Store,
-} from "@/store";
+import { useChangeHistoryStore, useDBSchemaV1Store } from "@/store";
+import { DEFAULT_PAGE_SIZE } from "@/store/modules/common";
import type { ComposedDatabase } from "@/types";
import { DEFAULT_PROJECT_NAME } from "@/types";
import type { Table } from "@/types/changeHistory";
@@ -189,7 +186,7 @@ const prepareChangeHistoryList = async () => {
state.loading = true;
await changeHistoryStore.fetchChangeHistoryList({
parent: props.database.name,
- pageSize: DEFAULT_CHANGE_HISTORY_PAGE_SIZE,
+ pageSize: DEFAULT_PAGE_SIZE,
});
// prepare database metadata for getting affected tables.
await useDBSchemaV1Store().getOrFetchDatabaseMetadata({
diff --git a/frontend/src/grpcweb/index.ts b/frontend/src/grpcweb/index.ts
index 07a7bd8147..77ebdec890 100644
--- a/frontend/src/grpcweb/index.ts
+++ b/frontend/src/grpcweb/index.ts
@@ -15,12 +15,14 @@ import { ChangelistServiceDefinition } from "@/types/proto/v1/changelist_service
import { DatabaseGroupServiceDefinition } from "@/types/proto/v1/database_group_service";
import { DatabaseServiceDefinition } from "@/types/proto/v1/database_service";
import { EnvironmentServiceDefinition } from "@/types/proto/v1/environment_service";
+import { GroupServiceDefinition } from "@/types/proto/v1/group";
import { IdentityProviderServiceDefinition } from "@/types/proto/v1/idp_service";
import { InstanceServiceDefinition } from "@/types/proto/v1/instance_service";
import { IssueServiceDefinition } from "@/types/proto/v1/issue_service";
import { OrgPolicyServiceDefinition } from "@/types/proto/v1/org_policy_service";
import { PlanServiceDefinition } from "@/types/proto/v1/plan_service";
import { ProjectServiceDefinition } from "@/types/proto/v1/project_service";
+import { ReleaseServiceDefinition } from "@/types/proto/v1/release_service";
import { ReviewConfigServiceDefinition } from "@/types/proto/v1/review_config_service";
import { RiskServiceDefinition } from "@/types/proto/v1/risk_service";
import { RoleServiceDefinition } from "@/types/proto/v1/role_service";
@@ -29,7 +31,6 @@ import { SettingServiceDefinition } from "@/types/proto/v1/setting_service";
import { SheetServiceDefinition } from "@/types/proto/v1/sheet_service";
import { SQLServiceDefinition } from "@/types/proto/v1/sql_service";
import { SubscriptionServiceDefinition } from "@/types/proto/v1/subscription_service";
-import { GroupServiceDefinition } from "@/types/proto/v1/group";
import { VCSConnectorServiceDefinition } from "@/types/proto/v1/vcs_connector_service";
import { VCSProviderServiceDefinition } from "@/types/proto/v1/vcs_provider_service";
import { WorksheetServiceDefinition } from "@/types/proto/v1/worksheet_service";
@@ -221,6 +222,11 @@ export const workspaceServiceClient = clientFactory.create(
channel
);
+export const releaseServiceClient = clientFactory.create(
+ ReleaseServiceDefinition,
+ channel
+);
+
// e.g. How to use `authServiceClient`?
//
// await authServiceClient.login({
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 7abb81837e..95e9dc3115 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1918,7 +1918,7 @@
"repository": {
"our-webhook-link": "Bytebase 创建的 webhook 可以在 {webhookLink} 中找到。",
"branch-observe-file-change": "Bytebase 跟踪文件变更的分支。",
- "database-group-description": "默认情况下,更改将应用于项目中的所有数据库。或者,它们可以应用于数据库组。",
+ "database-group-description": "默认情况下,更改将应用于项目中的所有数据库。或者,也可以应用于指定的数据库分组。",
"base-directory": "根目录",
"base-directory-description": "Bytebase 观察文件变化的根目录。如果文件存储在存储库根目录中,请使用“/”。",
"merge-request": "合并请求",
diff --git a/frontend/src/store/modules/common.ts b/frontend/src/store/modules/common.ts
new file mode 100644
index 0000000000..59ccd80646
--- /dev/null
+++ b/frontend/src/store/modules/common.ts
@@ -0,0 +1,3 @@
+// The default page size for the list API.
+// Set to a large number to fetch all items in one request.
+export const DEFAULT_PAGE_SIZE = 1000000;
diff --git a/frontend/src/store/modules/index.ts b/frontend/src/store/modules/index.ts
index 17bec3f80e..e8e1186b07 100644
--- a/frontend/src/store/modules/index.ts
+++ b/frontend/src/store/modules/index.ts
@@ -16,5 +16,7 @@ export * from "./slowQuery";
export * from "./role";
export * from "./dbGroup";
export * from "./utils";
-export * from "./v1";
export * from "./branch";
+export * from "./release";
+
+export * from "./v1";
diff --git a/frontend/src/store/modules/release.ts b/frontend/src/store/modules/release.ts
new file mode 100644
index 0000000000..fea5dcf2b7
--- /dev/null
+++ b/frontend/src/store/modules/release.ts
@@ -0,0 +1,109 @@
+import { head, uniq } from "lodash-es";
+import { defineStore } from "pinia";
+import { computed, reactive, ref, unref, watch } from "vue";
+import { releaseServiceClient } from "@/grpcweb";
+import type { MaybeRef, ComposedRelease, Pagination } from "@/types";
+import { isValidDatabaseName, unknownRelease } from "@/types";
+import { DEFAULT_PROJECT_NAME } from "@/types";
+import type { Release } from "@/types/proto/v1/release_service";
+import { DEFAULT_PAGE_SIZE } from "./common";
+import { useProjectV1Store } from "./v1";
+import { getProjectNameReleaseId, projectNamePrefix } from "./v1/common";
+
+export const useReleaseStore = defineStore("release", () => {
+ const releaseMapByName = reactive(new Map<string, ComposedRelease>());
+
+ const releaseList = computed(() => {
+ return Array.from(releaseMapByName.values());
+ });
+
+ const fetchReleasesByProject = async (
+ project: string,
+ pagination?: Pagination
+ ) => {
+ const resp = await releaseServiceClient.listReleases({
+ parent: project,
+ pageSize: pagination?.pageSize || DEFAULT_PAGE_SIZE,
+ pageToken: pagination?.pageToken,
+ });
+ const composedReleaseList = await batchComposeRelease(resp.releases);
+ composedReleaseList.forEach((release) => {
+ releaseMapByName.set(release.name, release);
+ });
+ return {
+ releases: composedReleaseList,
+ nextPageToken: resp.nextPageToken,
+ };
+ };
+
+ const fetchReleaseByName = async (name: string, silent = false) => {
+ const release = await releaseServiceClient.getRelease({ name }, { silent });
+ const [composedRelease] = await batchComposeRelease([release]);
+ releaseMapByName.set(composedRelease.name, composedRelease);
+ return composedRelease;
+ };
+
+ const getReleasesByProject = (project: string) => {
+ return releaseList.value.filter((release) => release.project === project);
+ };
+
+ const getReleaseByName = (name: string) => {
+ return releaseMapByName.get(name) ?? unknownRelease();
+ };
+
+ return {
+ releaseList,
+ fetchReleasesByProject,
+ fetchReleaseByName,
+ getReleasesByProject,
+ getReleaseByName,
+ };
+});
+
+export const useReleaseByName = (name: MaybeRef<string>) => {
+ const store = useReleaseStore();
+ const ready = ref(true);
+ watch(
+ () => unref(name),
+ (name) => {
+ if (!isValidDatabaseName(store.getReleaseByName(name).name)) {
+ ready.value = false;
+ store.fetchReleaseByName(name).then(() => {
+ ready.value = true;
+ });
+ }
+ },
+ { immediate: true }
+ );
+ const release = computed(() => store.getReleaseByName(unref(name)));
+
+ return {
+ release,
+ ready,
+ };
+};
+
+export const batchComposeRelease = async (releaseList: Release[]) => {
+ const composedReleaseList = releaseList.map((release) => {
+ const composed = release as ComposedRelease;
+ composed.project = `${projectNamePrefix}${head(getProjectNameReleaseId(release.name))}`;
+ return composed;
+ });
+ const distinctProjectList = uniq(
+ composedReleaseList.map((release) => release.project)
+ );
+
+ const projectV1Store = useProjectV1Store();
+ await Promise.all(
+ distinctProjectList.map((project) => {
+ if (project === DEFAULT_PROJECT_NAME) {
+ return;
+ }
+ return projectV1Store.getOrFetchProjectByName(project);
+ })
+ );
+ return composedReleaseList.map((release) => {
+ release.projectEntity = projectV1Store.getProjectByName(release.project);
+ return release;
+ });
+};
diff --git a/frontend/src/store/modules/revision.ts b/frontend/src/store/modules/revision.ts
new file mode 100644
index 0000000000..5cd8b9295b
--- /dev/null
+++ b/frontend/src/store/modules/revision.ts
@@ -0,0 +1,47 @@
+import { defineStore } from "pinia";
+import { computed, reactive } from "vue";
+import { databaseServiceClient } from "@/grpcweb";
+import type { Pagination } from "@/types";
+import { Revision } from "@/types/proto/v1/database_service";
+import { DEFAULT_PAGE_SIZE } from "./common";
+import { revisionNamePrefix } from "./v1/common";
+
+export const useRevisionStore = defineStore("revision", () => {
+ const revisionMapByName = reactive(new Map<string, Revision>());
+
+ const revisionList = computed(() => {
+ return Array.from(revisionMapByName.values());
+ });
+
+ const fetchRevisionsByDatabase = async (
+ database: string,
+ pagination?: Pagination
+ ) => {
+ const resp = await databaseServiceClient.listRevisions({
+ parent: database,
+ pageSize: pagination?.pageSize || DEFAULT_PAGE_SIZE,
+ pageToken: pagination?.pageToken,
+ });
+ resp.revisions.forEach((revision) => {
+ revisionMapByName.set(revision.name, revision);
+ });
+ return resp;
+ };
+
+ const getRevisionsByDatabase = (database: string) => {
+ return revisionList.value.filter((revision) =>
+ revision.name.startsWith(`${database}/${revisionNamePrefix}`)
+ );
+ };
+
+ const getRevisionByName = (name: string) => {
+ return revisionMapByName.get(name) ?? Revision.fromPartial({});
+ };
+
+ return {
+ revisionList,
+ fetchRevisionsByDatabase,
+ getRevisionsByDatabase,
+ getRevisionByName,
+ };
+});
diff --git a/frontend/src/store/modules/v1/changeHistory.ts b/frontend/src/store/modules/v1/changeHistory.ts
index 05909a8552..211fb99ac7 100644
--- a/frontend/src/store/modules/v1/changeHistory.ts
+++ b/frontend/src/store/modules/v1/changeHistory.ts
@@ -13,8 +13,7 @@ import {
ChangeHistory_Type,
} from "@/types/proto/v1/database_service";
import { extractChangeHistoryUID } from "@/utils";
-
-export const DEFAULT_CHANGE_HISTORY_PAGE_SIZE = 1000000;
+import { DEFAULT_PAGE_SIZE } from "../common";
type CacheKeyType = [string /* name */, ChangeHistoryView];
@@ -61,7 +60,7 @@ export const useChangeHistoryStore = defineStore("changeHistory_v1", () => {
}
return fetchChangeHistoryList({
parent: databaseName,
- pageSize: DEFAULT_CHANGE_HISTORY_PAGE_SIZE,
+ pageSize: DEFAULT_PAGE_SIZE,
});
};
const changeHistoryListByDatabase = (name: string) => {
diff --git a/frontend/src/store/modules/v1/common.ts b/frontend/src/store/modules/v1/common.ts
index a10c597551..56f4ff974e 100644
--- a/frontend/src/store/modules/v1/common.ts
+++ b/frontend/src/store/modules/v1/common.ts
@@ -29,6 +29,8 @@ export const planCheckRunPrefix = "planCheckRuns/";
export const rolloutNamePrefix = "rollouts/";
export const stageNamePrefix = "stages/";
export const taskNamePrefix = "tasks/";
+export const releaseNamePrefix = "releases/";
+export const revisionNamePrefix = "revisions/";
export const getNameParentTokens = (
name: string,
@@ -230,6 +232,17 @@ export const getInstanceIdPolicyId = (name: string): string[] => {
return tokens;
};
+export const getProjectNameReleaseId = (name: string): string[] => {
+ const tokens = getNameParentTokens(name, [
+ projectNamePrefix,
+ releaseNamePrefix,
+ ]);
+ if (tokens.length !== 2) {
+ return ["", ""];
+ }
+ return tokens;
+};
+
export const isDatabaseName = (name: string): boolean => {
const regex = /^instances\/([^/]+)\/databases\/(.+)$/;
return regex.test(name);
diff --git a/frontend/src/store/modules/v1/database.ts b/frontend/src/store/modules/v1/database.ts
index ddcabb570b..b1a72cc584 100644
--- a/frontend/src/store/modules/v1/database.ts
+++ b/frontend/src/store/modules/v1/database.ts
@@ -22,8 +22,6 @@ import { useGracefulRequest } from "../utils";
import { useEnvironmentV1Store } from "./environment";
import { useProjectV1Store } from "./project";
-export const DEFAULT_DATABASE_PAGE_SIZE = 1000000;
-
export const useDatabaseV1Store = defineStore("database_v1", () => {
const databaseMapByName = reactive(new Map<string, ComposedDatabase>());
diff --git a/frontend/src/store/modules/v1/databaseList.ts b/frontend/src/store/modules/v1/databaseList.ts
index 367c80aa01..f791b1bc06 100644
--- a/frontend/src/store/modules/v1/databaseList.ts
+++ b/frontend/src/store/modules/v1/databaseList.ts
@@ -1,12 +1,13 @@
import { computed, unref, watch, type MaybeRef } from "vue";
import { databaseServiceClient } from "@/grpcweb";
+import { DEFAULT_PAGE_SIZE } from "../common";
import { useListCache } from "./cache";
import {
instanceNamePrefix,
projectNamePrefix,
workspaceNamePrefix,
} from "./common";
-import { DEFAULT_DATABASE_PAGE_SIZE, useDatabaseV1Store } from "./database";
+import { useDatabaseV1Store } from "./database";
const formatDatabaseParent = (parent: string) => {
if (
@@ -52,7 +53,7 @@ export const useDatabaseV1List = (
: databaseServiceClient.listDatabases;
const { databases } = await request({
parent: formatParent.value,
- pageSize: DEFAULT_DATABASE_PAGE_SIZE,
+ pageSize: DEFAULT_PAGE_SIZE,
});
await store.upsertDatabaseMap(databases);
listCache.cacheMap.set(cacheKey.value, {
diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.ts
index 9ca89a6aef..e2b17a4576 100644
--- a/frontend/src/types/common.ts
+++ b/frontend/src/types/common.ts
@@ -50,3 +50,8 @@ const makeUnknown = (type: ResourceType) => {
};
export const unknown = makeUnknown as ResourceMaker;
+
+export interface Pagination {
+ pageSize?: number;
+ pageToken?: string;
+}
diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts
index 5c1aebbfcd..53f2610554 100644
--- a/frontend/src/types/index.ts
+++ b/frontend/src/types/index.ts
@@ -31,5 +31,6 @@ export * from "./semanticTypes";
export * from "./iam";
export * from "./misc";
export * from "./appProfile";
+export * from "./release";
export * from "./v1";
diff --git a/frontend/src/types/release.ts b/frontend/src/types/release.ts
new file mode 100644
index 0000000000..108640f7fb
--- /dev/null
+++ b/frontend/src/types/release.ts
@@ -0,0 +1,55 @@
+import { getProjectNameReleaseId } from "@/store/modules/v1/common";
+import { EMPTY_ID, UNKNOWN_ID } from "./const";
+import { Release } from "./proto/v1/release_service";
+import {
+ emptyProject,
+ unknownProject,
+ type ComposedProject,
+} from "./v1/project";
+
+export interface ComposedRelease extends Release {
+ // Format: projects/{project}
+ project: string;
+ projectEntity: ComposedProject;
+}
+
+export const UNKNOWN_RELEASE_NAME = `${unknownProject().name}/releases/${UNKNOWN_ID}`;
+
+export const emptyRelease = (): ComposedRelease => {
+ const projectEntity = emptyProject();
+ const release = Release.fromJSON({
+ name: `${projectEntity.name}/releases/${EMPTY_ID}`,
+ uid: String(EMPTY_ID),
+ project: projectEntity.name,
+ });
+ return {
+ ...release,
+ project: projectEntity.name,
+ projectEntity,
+ };
+};
+
+export const unknownRelease = (): ComposedRelease => {
+ const projectEntity = unknownProject();
+ const release = Release.fromJSON({
+ name: `${projectEntity.name}/releases/${UNKNOWN_ID}`,
+ uid: String(UNKNOWN_ID),
+ project: projectEntity.name,
+ });
+ return {
+ ...release,
+ project: projectEntity.name,
+ projectEntity,
+ };
+};
+
+export const isValidReleaseName = (name: any): name is string => {
+ if (typeof name !== "string") return false;
+ const [projectName, releaseName] = getProjectNameReleaseId(name);
+ return Boolean(
+ projectName &&
+ projectName !== String(UNKNOWN_ID) &&
+ releaseName &&
+ releaseName !== String(UNKNOWN_ID)
+ );
+};
diff --git a/frontend/src/views/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail.vue
index 0044037add..d548603bd7 100644
--- a/frontend/src/views/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail.vue
@@ -89,9 +89,7 @@
@click.prevent="tryTransferProject"
>
<span>{{ $t("database.transfer-project") }}</span>
- <heroicons-outline:switch-horizontal
- class="-mr-1 ml-2 h-5 w-5 text-control-light"
- />
+ <ArrowRightLeftIcon class="ml-1" :size="16" />
</NButton>
<NButton
v-if="allowChangeData"
@@ -191,6 +189,7 @@
<script lang="ts" setup>
import { useTitle } from "@vueuse/core";
import dayjs from "dayjs";
+import { ArrowRightLeftIcon } from "lucide-vue-next";
import { NButton, NTabPane, NTabs } from "naive-ui";
import { computed, reactive, watch, ref, onMounted } from "vue";
import { useRouter } from "vue-router";