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: migrate legacy cache (#18378)
ecmadao251985324021
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vueindex bfad7d8434..1870496f71 100644--- a/frontend/src/components/ProvideSQLEditorContext.vue+++ b/frontend/src/components/ProvideSQLEditorContext.vue@@ -43,6 +43,7 @@ import {useSQLEditorTabStore,useWorkSheetStore,} from "@/store";+import { migrateLegacyCache } from "@/store/modules/sqlEditor/legacy/migration";import type { SQLEditorConnection } from "@/types";import {DEFAULT_PROJECT_NAME,@@ -149,7 +150,7 @@ const connect = (connection: SQLEditorConnection) => {/* defaultTitle */ suggestedTabTitleForSQLEditorConnection(connection),/* ignoreMode */ true);- if (currentTab.mode === "ADMIN") {+ if (currentTab?.mode === "ADMIN") {// Don't enter ADMIN mode initiallytabStore.updateCurrentTab({mode: DEFAULT_SQL_EDITOR_TAB_MODE,@@ -432,6 +433,7 @@ onMounted(async () => {initializeProject(),]);+ await migrateLegacyCache();await tabStore.maybeInitProject();await initializeConnectionFromQuery();syncURLWithConnection();diff --git a/frontend/src/store/modules/sqlEditor/legacy/extendedTab.ts b/frontend/src/store/modules/sqlEditor/legacy/extendedTab.tsnew file mode 100644index 0000000000..b2da3ccab0--- /dev/null+++ b/frontend/src/store/modules/sqlEditor/legacy/extendedTab.ts@@ -0,0 +1,76 @@+import { pick } from "lodash-es";+import { defineStore } from "pinia";+import PouchDB from "pouchdb";+import PouchDBFind from "pouchdb-find";+import type { SQLEditorTab } from "@/types";++export const EXTENDED_TAB_FIELDS = [+ "statement",+ "batchQueryContext",+ "treeState",+] as const;+type ExtendedTab = Pick<SQLEditorTab, (typeof EXTENDED_TAB_FIELDS)[number]>;+type ExtendedTabEntity = ExtendedTab & {+ _id: string;+ _rev?: string;+ project: string; // for tracing+ user: string; // for tracing+};++PouchDB.plugin(PouchDBFind);++export const useExtendedTabStore = defineStore("sqlEditorExtendedTab", () => {+ // stores+ const extendedTabDB = new PouchDB<ExtendedTabEntity>(+ "bb.sql-editor.extended-tab",+ {+ // Remove unused and old data automatically.+ auto_compaction: true,+ // Do not save and track old revisions, this is for saving storage usage.+ revs_limit: 1,+ }+ );+ const ready = Promise.all([+ extendedTabDB.createIndex({+ index: { name: "idx_tab_user", fields: ["user"] },+ }),+ extendedTabDB.createIndex({+ index: { name: "idx_tab_project", fields: ["project"] },+ }),+ ]);++ const fetchExtendedTab = async (tab: SQLEditorTab, fallback?: () => void) => {+ try {+ await ready;+ const doc = await extendedTabDB.get(tab.id);+ Object.assign(tab, {+ _rev: doc._rev,+ _user: doc.user,+ _project: doc.project,+ ...pick(doc, EXTENDED_TAB_FIELDS),+ });+ } catch (err) {+ if (fallback) {+ fallback();+ }++ if ((err as any)?.status === 404) return;+ console.debug("[SQLEditorExtendedTabStore] fetchExtendedTab", err);+ }+ };++ const deleteExtendedTab = async (id: string) => {+ try {+ await ready;+ const doc = await extendedTabDB.get(id);+ await extendedTabDB.remove(doc);+ } catch (err) {+ console.warn("[SQLEditorExtendedTabStore] deleteExtendedTab", err);+ }+ };++ return {+ fetchExtendedTab,+ deleteExtendedTab,+ };+});diff --git a/frontend/src/store/modules/sqlEditor/legacy/migration.ts b/frontend/src/store/modules/sqlEditor/legacy/migration.tsnew file mode 100644index 0000000000..c5b5a46e73--- /dev/null+++ b/frontend/src/store/modules/sqlEditor/legacy/migration.ts@@ -0,0 +1,104 @@+import { omit } from "lodash-es";+import { computed, reactive } from "vue";+import type { SQLEditorTab } from "@/types";+import { DEFAULT_SQL_EDITOR_TAB_MODE } from "@/types";+import {+ defaultSQLEditorTab,+ useDynamicLocalStorage,+ WebStorageHelper,+} from "@/utils";+import { extractUserId } from "../../v1";+import { useCurrentUserV1 } from "../../v1/auth";+import { EXTENDED_TAB_FIELDS, useExtendedTabStore } from "./extendedTab";++const LOCAL_STORAGE_KEY_PREFIX = "bb.sql-editor-tab";++type PersistentTab = Pick<+ SQLEditorTab,+ "id" | "title" | "connection" | "mode" | "worksheet" | "status"+>;++export const migrateLegacyCache = async () => {+ const me = useCurrentUserV1();+ const userUID = computed(() => extractUserId(me.value.name));++ const keyNamespace = computed(+ () => `${LOCAL_STORAGE_KEY_PREFIX}.${userUID.value}`+ );+ const extendedTabStore = useExtendedTabStore();+ const tabIdListKey = computed(() => `${keyNamespace.value}.tab-id-list`);+ const tabIdListMapByProject = useDynamicLocalStorage<+ Record<string, string[]>+ >(tabIdListKey, {});++ const getStorage = () => {+ return new WebStorageHelper(keyNamespace.value);+ };++ const keyForTab = (id: string) => {+ return `tab.${id}`;+ };++ const loadStoredTab = async (id: string) => {+ const stored = getStorage().load<PersistentTab | undefined>(+ keyForTab(id),+ undefined+ );+ if (!stored) {+ return undefined;+ }+ const tab = reactive<SQLEditorTab>({+ ...defaultSQLEditorTab(),+ // Ignore extended fields stored in localStorage since they are migrated+ // to extendedTabStore.+ ...omit(stored, EXTENDED_TAB_FIELDS),+ id,+ });+ if (tab.mode !== DEFAULT_SQL_EDITOR_TAB_MODE) {+ // Do not enter ADMIN mode initially+ tab.mode = DEFAULT_SQL_EDITOR_TAB_MODE;+ }++ await extendedTabStore.fetchExtendedTab(tab, () => {+ // When the first time of migration, the extended doc in IndexedDB is not+ // found.+ // Fallback to the original PersistentTab in LocalStorage if possible.+ // This might happen only once to each user, since the second time when a+ // tab is saved, extended fields will be migrated, and won't be saved to+ // LocalStorage, so the fallback routine won't be hit.+ const { statement } = stored as any;+ if (statement) {+ tab.statement = statement;+ }+ });++ return tab;+ };++ const entries = [...Object.entries(tabIdListMapByProject.value)];+ for (const [project, tabIds] of entries) {+ const draftTabList = useDynamicLocalStorage<SQLEditorTab[]>(+ computed(+ () =>+ `${LOCAL_STORAGE_KEY_PREFIX}.${project}.${userUID.value}.draft-tab-list`+ ),+ [],+ localStorage+ );++ for (const tabId of tabIds) {+ const exist = draftTabList.value.find((draft) => draft.id === tabId);+ if (exist) {+ continue;+ }+ const tab = await loadStoredTab(tabId);+ await extendedTabStore.deleteExtendedTab(tabId);+ if (!tab) {+ continue;+ }+ draftTabList.value.push(tab);+ }++ delete tabIdListMapByProject.value[project];+ }+};diff --git a/frontend/src/store/modules/sqlEditor/tab.ts b/frontend/src/store/modules/sqlEditor/tab.tsindex 52e480a556..4b1d6a9973 100644--- a/frontend/src/store/modules/sqlEditor/tab.ts+++ b/frontend/src/store/modules/sqlEditor/tab.ts@@ -187,6 +187,11 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {});const { id, worksheet } = newTab;+ if (openTabList.value.find((tab) => tab.id === id)) {+ setCurrentTabId(id);+ return newTab;+ }+const persistentTab = pick(newTab,...PERSISTENT_TAB_FIELDS@@ -199,7 +204,10 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {} else {openTabList.value.push(persistentTab);}- if (!worksheet) {+ if (+ !worksheet &&+ !draftTabList.value.find((draft) => draft.id === newTab.id)+ ) {draftTabList.value.push(newTab);}diff --git a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vueindex eb9c81dd63..32af3f737e 100644--- a/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue+++ b/frontend/src/views/sql-editor/AsidePanel/WorksheetPane/SheetList/SheetTree.vue@@ -440,7 +440,12 @@ const nodeProps = ({ option }: { option: TreeOption }) => {e.metaKey || e.ctrlKey);} else {- tabStore.setCurrentTabId(node.worksheet.name);+ const tab = tabStore.draftList.find(+ (draft) => draft.id === node.worksheet?.name+ );+ if (tab) {+ tabStore.addTab(tab);+ }}} else {if (expandedKeys.value.has(node.key)) {