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: refactor sheet store cache (#10750)
Liu Jif1edf5daa6be
diff --git a/frontend/src/components/Changelist/ChangelistDetail/NavBar/export.ts b/frontend/src/components/Changelist/ChangelistDetail/NavBar/export.tsindex 5caf9f7f42..dd91333a8b 100644--- a/frontend/src/components/Changelist/ChangelistDetail/NavBar/export.ts+++ b/frontend/src/components/Changelist/ChangelistDetail/NavBar/export.ts@@ -26,10 +26,7 @@ const zipFileForChangeHistory = async (change: Change,index: number) => {- const sheet = await useSheetV1Store().fetchSheetByName(- change.sheet,- true /* raw */- );+ const sheet = await useSheetV1Store().fetchSheetByName(change.sheet, "FULL");if (!sheet) {return;}@@ -51,10 +48,7 @@ const zipFileForChangeHistory = async (};const zipFileForBranch = async (zip: JSZip, change: Change, index: number) => {- const sheet = await useSheetV1Store().fetchSheetByName(- change.sheet,- true /* raw */- );+ const sheet = await useSheetV1Store().fetchSheetByName(change.sheet, "FULL");if (!sheet) {return;}@@ -71,10 +65,7 @@ const zipFileForBranch = async (zip: JSZip, change: Change, index: number) => {};const zipFileForRawSQL = async (zip: JSZip, change: Change, index: number) => {- const sheet = await useSheetV1Store().fetchSheetByName(- change.sheet,- true /* raw */- );+ const sheet = await useSheetV1Store().fetchSheetByName(change.sheet, "FULL");if (!sheet) {return;}diff --git a/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue b/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vueindex 0daa02c6f6..c3d0d8f616 100644--- a/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue+++ b/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue@@ -61,6 +61,7 @@</template><script setup lang="ts">+import { computedAsync } from "@vueuse/core";import { zindexable as vZindexable } from "vdirs";import { computed, ref, watch } from "vue";import { useI18n } from "vue-i18n";@@ -93,11 +94,11 @@ const title = ref(t("changelist.change-source.raw-sql"));const statement = ref("");const isUpdating = ref(false);-const sheet = computed(() => {+const sheet = computedAsync(async () => {const { sheetName } = props;if (!sheetName) return undefined;- return sheetStore.getSheetByName(sheetName);-});+ return sheetStore.getOrFetchSheetByName(sheetName, "FULL");+}, undefined);const show = computed(() => {return sheet.value !== undefined;diff --git a/frontend/src/components/Issue/activity/ActionSentence.vue b/frontend/src/components/Issue/activity/ActionSentence.vueindex 62369be0df..1df517b2e6 100644--- a/frontend/src/components/Issue/activity/ActionSentence.vue+++ b/frontend/src/components/Issue/activity/ActionSentence.vue@@ -305,6 +305,7 @@ watch(if (activity.action === LogEntity_Action.ACTION_PIPELINE_TASK_STATEMENT_UPDATE) {+ // Use any (basic or full) view of sheets here to save data sizesheetV1Store.getOrFetchSheetByUID(String((JSON.parse(activity.payload) as ActivityTaskStatementUpdatePayload)diff --git a/frontend/src/components/IssueV1/components/ActivitySection/Activity/StatementUpdate.vue b/frontend/src/components/IssueV1/components/ActivitySection/Activity/StatementUpdate.vueindex a92594e9d6..6d4380e6d4 100644--- a/frontend/src/components/IssueV1/components/ActivitySection/Activity/StatementUpdate.vue+++ b/frontend/src/components/IssueV1/components/ActivitySection/Activity/StatementUpdate.vue@@ -50,6 +50,7 @@ const showPanel = ref(false);const prepareStatement = async (uid: string) => {if (uid === String(UNKNOWN_ID)) return "";+ // Use any (basic or full) view of sheets here to save data sizeconst sheet = await useSheetV1Store().getOrFetchSheetByUID(uid);if (!sheet) return "";return getSheetStatement(sheet);diff --git a/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vue b/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vueindex 5e7030bafb..d46cb352f0 100644--- a/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vue+++ b/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vue@@ -184,7 +184,7 @@ const createSheets = async () => {sheet.database = "";}sheet.title = issue.value.title;- const createdSheet = await useSheetV1Store().createSheet(+ const createdSheet = await sheetStore.createSheet(issue.value.project,sheet);@@ -246,7 +246,7 @@ const emitIssueCreateWindowEvent = async (issue: ComposedIssue) => {const spec = specForTask(issue.planEntity, task);const database = databaseForTask(issue, task);const sheetName = sheetNameOfTaskV1(task);- const sheet = await sheetStore.getOrFetchSheetByName(sheetName);+ const sheet = await sheetStore.getOrFetchSheetByName(sheetName, "FULL");const statement = sheet ? getSheetStatement(sheet) : "";eventParams.tasks.push({database: toRaw(database),diff --git a/frontend/src/components/IssueV1/components/Sidebar/RollbackSection/RollbackStatus.vue b/frontend/src/components/IssueV1/components/Sidebar/RollbackSection/RollbackStatus.vueindex b29e843396..519d83e29e 100644--- a/frontend/src/components/IssueV1/components/Sidebar/RollbackSection/RollbackStatus.vue+++ b/frontend/src/components/IssueV1/components/Sidebar/RollbackSection/RollbackStatus.vue@@ -168,7 +168,8 @@ const tryRollbackTask = async () => {].join(" ");const originalSheet = await sheetV1Store.getOrFetchSheetByName(- config.value.sheet+ config.value.sheet,+ "FULL");const description = ["The original SQL statement:",diff --git a/frontend/src/components/IssueV1/components/Sidebar/useActiveTaskSheet.ts b/frontend/src/components/IssueV1/components/Sidebar/useActiveTaskSheet.tsindex b01daf5fc1..f894237b06 100644--- a/frontend/src/components/IssueV1/components/Sidebar/useActiveTaskSheet.ts+++ b/frontend/src/components/IssueV1/components/Sidebar/useActiveTaskSheet.ts@@ -16,6 +16,7 @@ export const useActiveTaskSheet = () => {return undefined;}const name = sheetName.value;+ // Use any (basic or full) view of sheets here to save data sizereturn sheetStore.getSheetByName(name);});watch(@@ -28,6 +29,7 @@ export const useActiveTaskSheet = () => {const uid = extractSheetUID(sheetName);if (!uid) return;sheetReady.value = false;+ // Use any (basic or full) view of sheets here to save data sizesheetStore.getOrFetchSheetByName(sheetName).finally(() => {sheetReady.value = true;});diff --git a/frontend/src/components/IssueV1/components/StatementSection/useTaskSheet.ts b/frontend/src/components/IssueV1/components/StatementSection/useTaskSheet.tsindex e085abbf6b..1f976b7eda 100644--- a/frontend/src/components/IssueV1/components/StatementSection/useTaskSheet.ts+++ b/frontend/src/components/IssueV1/components/StatementSection/useTaskSheet.ts@@ -1,4 +1,5 @@-import { computed, ref, watch } from "vue";+import { computedAsync } from "@vueuse/core";+import { computed, ref } from "vue";import { useSheetV1Store } from "@/store";import { ESTABLISH_BASELINE_SQL } from "@/types";import { Task_Type } from "@/types/proto/v1/rollout_service";@@ -18,30 +19,21 @@ export const useTaskSheet = () => {return sheetNameOfTaskV1(selectedTask.value);});const sheetReady = ref(false);- const sheet = computed(() => {- const name = sheetName.value;- const uid = extractSheetUID(name);- if (uid.startsWith("-")) {- return getLocalSheetByName(name);- }- return sheetStore.getSheetByName(name);- });- watch(- sheetName,- (sheetName) => {- const uid = extractSheetUID(sheetName);- if (!uid) return;+ const sheet = computedAsync(+ async () => {+ const name = sheetName.value;+ const uid = extractSheetUID(name);if (uid.startsWith("-")) {- // The sheet is not created yet (uid starts with "-")- sheetReady.value = true;- } else {- sheetReady.value = false;- sheetStore.getOrFetchSheetByName(sheetName).finally(() => {- sheetReady.value = true;- });+ return getLocalSheetByName(name);}++ // Use any (basic or full) view of sheets here to save data size+ return sheetStore.getOrFetchSheetByName(name);},- { immediate: true }+ undefined,+ {+ evaluating: sheetReady,+ });const sheetStatement = computed({get() {diff --git a/frontend/src/components/IssueV1/logic/initialize/create.ts b/frontend/src/components/IssueV1/logic/initialize/create.tsindex 39580caed6..073d6fffd2 100644--- a/frontend/src/components/IssueV1/logic/initialize/create.ts+++ b/frontend/src/components/IssueV1/logic/initialize/create.ts@@ -316,7 +316,10 @@ export const buildSpecForTarget = async (type: Plan_ChangeDatabaseConfig_Type.DATA,});if (query.sheetId) {- const sheet = await useSheetV1Store().getOrFetchSheetByUID(query.sheetId);+ const sheet = await useSheetV1Store().getOrFetchSheetByUID(+ query.sheetId,+ "FULL"+ );if (sheet) {spec.changeDatabaseConfig.sheet = sheet.name;}@@ -341,7 +344,8 @@ export const buildSpecForTarget = async (if (query.sheetId) {const remoteSheet = await useSheetV1Store().getOrFetchSheetByUID(- query.sheetId+ query.sheetId,+ "FULL");if (remoteSheet) {// make a local copy for remote sheet for further editingdiff --git a/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue b/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vueindex cbd5c165ee..41f6cd63a4 100644--- a/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue+++ b/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue@@ -140,7 +140,7 @@ const engineSelectorOptions = computed(() => {});const sheet = computed(() => {- return sheetStore.getSheetByUID(String(props.sheetId || UNKNOWN_ID));+ return sheetStore.getSheetByUID(String(props.sheetId || UNKNOWN_ID), "FULL");});const isSheetOversized = computed(() => {@@ -154,7 +154,10 @@ const isSheetOversized = computed(() => {onMounted(async () => {if (props.sheetId) {- const sheet = await sheetStore.getOrFetchSheetByUID(String(props.sheetId));+ const sheet = await sheetStore.getOrFetchSheetByUID(+ String(props.sheetId),+ "FULL"+ );if (sheet) {const statement = new TextDecoder().decode(sheet.content);state.editStatement = statement;@@ -234,10 +237,13 @@ const handleUpdateSheet = useDebounceFn(async (statement: string) => {return;}- await sheetStore.patchSheet({- name: sheet.value.name,- content: new TextEncoder().encode(statement),- });+ await sheetStore.patchSheet(+ {+ name: sheet.value.name,+ content: new TextEncoder().encode(statement),+ },+ ["content"]+ );}, 1000);const update = (sheetId?: number) => {diff --git a/frontend/src/components/SyncDatabaseSchema/SelectTargetDatabasesView.vue b/frontend/src/components/SyncDatabaseSchema/SelectTargetDatabasesView.vueindex 4b333872bb..c821c11e3b 100644--- a/frontend/src/components/SyncDatabaseSchema/SelectTargetDatabasesView.vue+++ b/frontend/src/components/SyncDatabaseSchema/SelectTargetDatabasesView.vue@@ -244,7 +244,12 @@ import {import { ComposedDatabase, UNKNOWN_ID } from "@/types";import { Engine } from "@/types/proto/v1/common";import { ChangeHistory } from "@/types/proto/v1/database_service";-import { changeHistoryLink, databaseV1Url, toClipboard } from "@/utils";+import {+ changeHistoryLink,+ databaseV1Url,+ getSheetStatement,+ toClipboard,+} from "@/utils";import DiffViewPanel from "./DiffViewPanel.vue";import RawSQLEditorPanel from "./RawSQLEditorPanel.vue";import TargetDatabasesSelectPanel from "./TargetDatabasesSelectPanel.vue";@@ -316,9 +321,12 @@ const sourceDatabaseSchema = computed(() => {} else if (props.sourceSchemaType === "RAW_SQL") {let statement = props.rawSqlState?.statement || "";if (props.rawSqlState?.sheetId) {- const sheet = sheetStore.getSheetByUID(String(props.rawSqlState.sheetId));+ const sheet = sheetStore.getSheetByUID(+ String(props.rawSqlState.sheetId),+ "FULL"+ );if (sheet) {- statement = new TextDecoder().decode(sheet.content);+ statement = getSheetStatement(sheet);}}return statement;@@ -404,7 +412,10 @@ onMounted(async () => {// Prepare raw sql statement from sheet.if (props.rawSqlState?.sheetId) {- await sheetStore.getOrFetchSheetByUID(String(props.rawSqlState.sheetId));+ await sheetStore.getOrFetchSheetByUID(+ String(props.rawSqlState.sheetId),+ "FULL"+ );}});diff --git a/frontend/src/store/modules/v1/changelist.ts b/frontend/src/store/modules/v1/changelist.tsindex 241ed160e8..99f646cc19 100644--- a/frontend/src/store/modules/v1/changelist.ts+++ b/frontend/src/store/modules/v1/changelist.ts@@ -115,6 +115,7 @@ export const useChangelistStore = defineStore("changelist", () => {// Raw SQL, no need to compose}composer.collect(sheet, () =>+ // Use any (basic or full) view of sheets here to save data sizeuseSheetV1Store().getOrFetchSheetByName(sheet));};diff --git a/frontend/src/store/modules/v1/sheet.ts b/frontend/src/store/modules/v1/sheet.tsindex 0ac4250576..e92a4e2413 100644--- a/frontend/src/store/modules/v1/sheet.ts+++ b/frontend/src/store/modules/v1/sheet.ts@@ -1,30 +1,34 @@-import { isEqual, isUndefined } from "lodash-es";import { defineStore } from "pinia";-import { computed, ref, unref, watchEffect } from "vue";+import { computed, unref, watchEffect } from "vue";import { sheetServiceClient } from "@/grpcweb";+import { useCache } from "@/store/cache";import { UNKNOWN_ID, MaybeRef } from "@/types";import { Sheet } from "@/types/proto/v1/sheet_service";import { extractSheetUID, getSheetStatement } from "@/utils";-const REQUEST_CACHE_BY_UID = new Map<- string /* uid */,- Promise<Sheet | undefined>->();+export type SheetView = "FULL" | "BASIC";+type SheetCacheKey = [string /* uid */, SheetView];export const useSheetV1Store = defineStore("sheet_v1", () => {- const sheetsByName = ref(new Map<string, Sheet>());+ const cacheByUID = useCache<SheetCacheKey, Sheet | undefined>(+ "bb.sheet.by-uid"+ );// Utilitiesconst removeLocalSheet = (name: string) => {const uid = extractSheetUID(name);if (uid.startsWith("-")) {- sheetsByName.value.delete(name);+ cacheByUID.invalidateEntity([uid, "FULL"]);}};- const setSheetList = (sheets: Sheet[]) => {- for (const sheet of sheets) {- sheetsByName.value.set(sheet.name, sheet);- }+ const setCache = (sheet: Sheet, view: SheetView) => {+ const uid = extractSheetUID(sheet.name);+ if (uid === String(UNKNOWN_ID)) return;+ if (view === "FULL") {+ // A FULL version should override BASIC version+ cacheByUID.invalidateEntity([uid, "BASIC"]);+ }+ cacheByUID.setEntity([uid, view], sheet);};// CRUD@@ -33,115 +37,102 @@ export const useSheetV1Store = defineStore("sheet_v1", () => {parent,sheet,});- setSheetList([created]);+ setCache(created, "FULL");if (sheet.name) {removeLocalSheet(sheet.name);}return created;};- const getSheetByName = (name: string) => {- return sheetsByName.value.get(name);+ /**+ *+ * @param uid+ * @param view default undefined to any (FULL -> BASIC)+ * @returns+ */+ const getSheetByUID = (uid: string, view?: SheetView) => {+ if (view === undefined) {+ return (+ cacheByUID.getEntity([uid, "FULL"]) ??+ cacheByUID.getEntity([uid, "BASIC"])+ );+ }+ return cacheByUID.getEntity([uid, view]);+ };+ const getSheetRequestByUID = (uid: string, view?: SheetView) => {+ if (view === undefined) {+ return (+ cacheByUID.getRequest([uid, "FULL"]) ??+ cacheByUID.getRequest([uid, "BASIC"])+ );+ }+ return cacheByUID.getRequest([uid, view]);+ };+ /**+ *+ * @param name+ * @param view default undefined to any (FULL -> BASIC)+ * @returns+ */+ const getSheetByName = (name: string, view?: SheetView) => {+ const uid = extractSheetUID(name);+ if (!uid || uid === String(UNKNOWN_ID)) {+ return undefined;+ }+ return getSheetByUID(uid, view);};- const fetchSheetByName = async (name: string, raw = false) => {+ const fetchSheetByName = async (name: string, view: SheetView) => {const uid = extractSheetUID(name);if (uid.startsWith("-") || !uid) {return undefined;}try {+ console.debug("[fetchSheetByName]", name, view);const sheet = await sheetServiceClient.getSheet({name,- raw,+ raw: view === "FULL",});-- setSheetList([sheet]);return sheet;} catch {return undefined;}};- const getOrFetchSheetByName = async (name: string) => {+ const getOrFetchSheetByName = async (name: string, view?: SheetView) => {const uid = extractSheetUID(name);if (uid.startsWith("-") || !uid) {return undefined;}- if (uid === String(UNKNOWN_ID)) {- return undefined;+ const entity = getSheetByUID(name, view);+ if (entity) {+ return entity;}- const existed = getSheetByName(name);- if (existed) {- return existed;- }- const cached = REQUEST_CACHE_BY_UID.get(uid);- if (cached) {- return cached;+ const request = getSheetRequestByUID(name, view);+ if (request) {+ return request;}- const request = fetchSheetByName(name);- REQUEST_CACHE_BY_UID.set(uid, request);- request.then((sheet) => {+ const promise = fetchSheetByName(name, view ?? "BASIC");+ cacheByUID.setRequest([uid, view ?? "BASIC"], promise);+ promise.then((sheet) => {if (!sheet) {// If the request failed// remove the request cache entry so we can retry when needed.- REQUEST_CACHE_BY_UID.delete(uid);+ cacheByUID.invalidateRequest([uid, view ?? "BASIC"]);}});- return request;+ return promise;};- const getSheetByUID = (uid: string) => {- for (const [name, sheet] of sheetsByName.value) {- if (extractSheetUID(name) === uid) {- return sheet;- }- }- };- const getOrFetchSheetByUID = async (uid: string) => {- if (uid.startsWith("-") || !uid) {- return undefined;- }- if (uid === String(UNKNOWN_ID)) {- return undefined;- }- const existed = getSheetByUID(uid);- if (existed) {- return existed;- }- const cached = REQUEST_CACHE_BY_UID.get(uid);- if (cached) {- return cached;- }-- const name = `projects/-/sheets/${uid}`;- const request = fetchSheetByName(name);- REQUEST_CACHE_BY_UID.set(uid, request);- request.then((sheet) => {- if (!sheet) {- // If the request failed- // remove the request cache entry so we can retry when needed.- REQUEST_CACHE_BY_UID.delete(uid);- }- });- return request;+ const getOrFetchSheetByUID = async (uid: string, view?: SheetView) => {+ return getOrFetchSheetByName(`projects/-/sheets/${uid}`, view);};- const patchSheet = async (- sheet: Partial<Sheet>,- updateMask: string[] | undefined = undefined- ) => {+ const patchSheet = async (sheet: Partial<Sheet>, updateMask: string[]) => {if (!sheet.name) return;- const existed = sheetsByName.value.get(sheet.name);- if (!existed) return;- if (!updateMask) {- updateMask = getUpdateMaskForSheet(existed, sheet);- }- if (updateMask.length === 0) {- return existed;- }const updated = await sheetServiceClient.updateSheet({sheet,updateMask,});- setSheetList([updated]);+ setCache(updated, "FULL");return updated;};@@ -156,31 +147,17 @@ export const useSheetV1Store = defineStore("sheet_v1", () => {};});-const getUpdateMaskForSheet = (- origin: Sheet,- update: Partial<Sheet>-): string[] => {- const updateMask: string[] = [];- if (!isUndefined(update.title) && !isEqual(origin.title, update.title)) {- updateMask.push("title");- }- if (- !isUndefined(update.content) &&- !isEqual(origin.content, update.content)- ) {- updateMask.push("content");- }- return updateMask;-};--export const useSheetStatementByUID = (uid: MaybeRef<string>) => {+export const useSheetStatementByUID = (+ uid: MaybeRef<string>,+ view?: MaybeRef<SheetView | undefined>+) => {const store = useSheetV1Store();watchEffect(async () => {- await store.getOrFetchSheetByUID(unref(uid));+ await store.getOrFetchSheetByUID(unref(uid), unref(view));});return computed(() => {- const sheet = store.getSheetByUID(unref(uid));+ const sheet = store.getSheetByUID(unref(uid), unref(view));if (!sheet) return "";return getSheetStatement(sheet);});diff --git a/frontend/src/utils/v1/changelist.ts b/frontend/src/utils/v1/changelist.tsindex 972a96ac5d..79c5d06942 100644--- a/frontend/src/utils/v1/changelist.ts+++ b/frontend/src/utils/v1/changelist.ts@@ -57,10 +57,7 @@ export const generateSQLForChangeToDatabase = async (change: Change,database: ComposedDatabase) => {- const sheet = await useSheetV1Store().fetchSheetByName(- change.sheet,- true /* raw */- );+ const sheet = await useSheetV1Store().fetchSheetByName(change.sheet, "FULL");if (!sheet) {return "";}