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: add schema editor v1 store definition (#8160)
boojacke593c2164104
diff --git a/frontend/src/components/SchemaTemplate/FieldTemplateForm.vue b/frontend/src/components/SchemaTemplate/FieldTemplateForm.vueindex d40cc66af9..ab47224da2 100644--- a/frontend/src/components/SchemaTemplate/FieldTemplateForm.vue+++ b/frontend/src/components/SchemaTemplate/FieldTemplateForm.vue@@ -251,9 +251,12 @@import { isEqual } from "lodash-es";import { computed, reactive } from "vue";import { DrawerContent } from "@/components/v2";-import { useSchemaEditorStore, useSettingV1Store } from "@/store";+import { useSettingV1Store } from "@/store";import { Engine } from "@/types/proto/v1/common";-import { SchemaTemplateSetting_FieldTemplate } from "@/types/proto/v1/setting_service";+import {+ SchemaTemplateSetting,+ SchemaTemplateSetting_FieldTemplate,+} from "@/types/proto/v1/setting_service";import {getDataTypeSuggestionList,engineNameV1,@@ -281,7 +284,6 @@ const state = reactive<LocalState>({showClassificationDrawer: false,});const settingStore = useSettingV1Store();-const store = useSchemaEditorStore();const allowEdit = computed(() => {return (useWorkspacePermissionV1("bb.permission.workspace.manage-general").value &&@@ -289,6 +291,11 @@ const allowEdit = computed(() => {);});+const schemaTemplateList = computed(() => {+ const setting = settingStore.getSettingByName("bb.workspace.schema-template");+ return setting?.value?.schemaTemplateSettingValue?.fieldTemplates ?? [];+});+const classificationConfig = computed(() => {// TODO(ed): it's a temporary solutionreturn settingStore.classification[0];@@ -306,7 +313,7 @@ const dataTypeOptions = computed(() => {const categoryOptions = computed(() => {const options = [];for (const category of new Set(- store.schemaTemplateList.map((template) => template.category)+ schemaTemplateList.value.map((template) => template.category)).values()) {if (!category) {continue;@@ -350,7 +357,31 @@ const sumbitDisabled = computed(() => {});const sumbit = async () => {- await store.upsertSchemaTemplate(state);+ const template = state;+ const setting = await settingStore.fetchSettingByName(+ "bb.workspace.schema-template"+ );++ const settingValue = SchemaTemplateSetting.fromJSON({});+ if (setting?.value?.schemaTemplateSettingValue) {+ Object.assign(settingValue, setting.value.schemaTemplateSettingValue);+ }++ const index = settingValue.fieldTemplates.findIndex(+ (t) => t.id === template.id+ );+ if (index >= 0) {+ settingValue.fieldTemplates[index] = template;+ } else {+ settingValue.fieldTemplates.push(template);+ }++ await settingStore.upsertSetting({+ name: "bb.workspace.schema-template",+ value: {+ schemaTemplateSettingValue: settingValue,+ },+ });emit("dismiss");};diff --git a/frontend/src/components/SchemaTemplate/FieldTemplateTable.vue b/frontend/src/components/SchemaTemplate/FieldTemplateTable.vueindex d1910783a0..fc23127f37 100644--- a/frontend/src/components/SchemaTemplate/FieldTemplateTable.vue+++ b/frontend/src/components/SchemaTemplate/FieldTemplateTable.vue@@ -48,12 +48,16 @@</template><script lang="ts" setup>+import { pullAt } from "lodash-es";import { computed } from "vue";import { useI18n } from "vue-i18n";import { BBGrid, BBGridColumn } from "@/bbkit";-import { useSchemaEditorStore, useSettingV1Store } from "@/store";+import { useSettingV1Store } from "@/store";import { Engine } from "@/types/proto/v1/common";-import { SchemaTemplateSetting_FieldTemplate } from "@/types/proto/v1/setting_service";+import {+ SchemaTemplateSetting,+ SchemaTemplateSetting_FieldTemplate,+} from "@/types/proto/v1/setting_service";import { getDefaultValue } from "./utils";const props = defineProps<{@@ -68,7 +72,6 @@ const emit = defineEmits<{}>();const { t } = useI18n();-const store = useSchemaEditorStore();const settingStore = useSettingV1Store();const columnList = computed((): BBGridColumn[] => {@@ -115,7 +118,26 @@ const isRowClickable = (template: SchemaTemplateSetting_FieldTemplate) => {};const deleteTemplate = async (id: string) => {- await store.deleteSchemaTemplate(id);+ const setting = await settingStore.fetchSettingByName(+ "bb.workspace.schema-template"+ );++ const settingValue = SchemaTemplateSetting.fromJSON({});+ if (setting?.value?.schemaTemplateSettingValue) {+ Object.assign(settingValue, setting.value.schemaTemplateSettingValue);+ }++ const index = settingValue.fieldTemplates.findIndex((t) => t.id === id);+ if (index >= 0) {+ pullAt(settingValue.fieldTemplates, index);++ await settingStore.upsertSetting({+ name: "bb.workspace.schema-template",+ value: {+ schemaTemplateSettingValue: settingValue,+ },+ });+ }};const classificationConfig = computed(() => {diff --git a/frontend/src/store/modules/schemaEditor.ts b/frontend/src/store/modules/schemaEditor.tsindex cd2561d855..1632b51dbc 100644--- a/frontend/src/store/modules/schemaEditor.ts+++ b/frontend/src/store/modules/schemaEditor.ts@@ -1,5 +1,5 @@import axios from "axios";-import { cloneDeep, isUndefined, uniqueId, pullAt } from "lodash-es";+import { cloneDeep, isUndefined, uniqueId } from "lodash-es";import { defineStore } from "pinia";import {ComposedDatabase,@@ -10,19 +10,15 @@ import {DatabaseEdit,ResourceObject,} from "@/types";-import { SchemaMetadata } from "@/types/proto/store/database";import { Engine } from "@/types/proto/v1/common";-import {- SchemaTemplateSetting,- SchemaTemplateSetting_FieldTemplate,-} from "@/types/proto/v1/setting_service";+import { SchemaMetadata } from "@/types/proto/v1/database_service";import { DatabaseEditResult } from "@/types/schemaEditor";import {convertSchemaMetadataList,convertSchemaMetadataToSchema,Table,} from "@/types/schemaEditor/atomType";-import { useDatabaseV1Store, useDBSchemaV1Store, useSettingV1Store } from ".";+import { useDatabaseV1Store, useDBSchemaV1Store } from ".";export const generateUniqueTabId = () => {return uniqueId();@@ -65,13 +61,6 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {(databaseSchema) => databaseSchema.database);},- schemaTemplateList(): SchemaTemplateSetting_FieldTemplate[] {- const settingStore = useSettingV1Store();- const setting = settingStore.getSettingByName(- "bb.workspace.schema-template"- );- return setting?.value?.schemaTemplateSettingValue?.fieldTemplates ?? [];- },},actions: {addTab(tab: TabContext, setAsCurrentTab = true) {@@ -307,55 +296,5 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {const databaseEditResult = convertDatabaseEditResult(resData.data);return databaseEditResult;},- async upsertSchemaTemplate(template: SchemaTemplateSetting_FieldTemplate) {- const settingStore = useSettingV1Store();- const setting = await settingStore.fetchSettingByName(- "bb.workspace.schema-template"- );-- const settingValue = SchemaTemplateSetting.fromJSON({});- if (setting?.value?.schemaTemplateSettingValue) {- Object.assign(settingValue, setting.value.schemaTemplateSettingValue);- }-- const index = settingValue.fieldTemplates.findIndex(- (t) => t.id === template.id- );- if (index >= 0) {- settingValue.fieldTemplates[index] = template;- } else {- settingValue.fieldTemplates.push(template);- }-- await settingStore.upsertSetting({- name: "bb.workspace.schema-template",- value: {- schemaTemplateSettingValue: settingValue,- },- });- },- async deleteSchemaTemplate(id: string) {- const settingStore = useSettingV1Store();- const setting = await settingStore.fetchSettingByName(- "bb.workspace.schema-template"- );-- const settingValue = SchemaTemplateSetting.fromJSON({});- if (setting?.value?.schemaTemplateSettingValue) {- Object.assign(settingValue, setting.value.schemaTemplateSettingValue);- }-- const index = settingValue.fieldTemplates.findIndex((t) => t.id === id);- if (index >= 0) {- pullAt(settingValue.fieldTemplates, index);-- await settingStore.upsertSetting({- name: "bb.workspace.schema-template",- value: {- schemaTemplateSettingValue: settingValue,- },- });- }- },},});diff --git a/frontend/src/store/modules/schemaEditorV1.ts b/frontend/src/store/modules/schemaEditorV1.tsnew file mode 100644index 0000000000..ccbebd6f71--- /dev/null+++ b/frontend/src/store/modules/schemaEditorV1.ts@@ -0,0 +1,221 @@+import { isUndefined, uniqueId } from "lodash-es";+import { defineStore } from "pinia";+import { ComposedDatabase } from "@/types";+import {+ SchemaEditorV1State,+ Table,+ TabContext,+ SchemaEditorTabType,+ TableTabContext,+} from "@/types/schemaEditorV1";++export const generateUniqueTabId = () => {+ return uniqueId();+};++const getDefaultSchemaEditorState = (): SchemaEditorV1State => {+ return {+ resourceType: "database",+ resourceMap: {+ database: new Map(),+ branch: new Map(),+ },+ tabState: {+ tabMap: new Map<string, TabContext>(),+ currentTabId: "",+ },+ };+};++export const useSchemaEditorStore = defineStore("SchemaEditorV1", {+ state: (): SchemaEditorV1State => {+ return getDefaultSchemaEditorState();+ },+ getters: {+ currentTab(): TabContext | undefined {+ if (isUndefined(this.tabState.currentTabId)) {+ return undefined;+ }+ return this.tabState.tabMap.get(this.tabState.currentTabId);+ },+ tabList(): TabContext[] {+ return Array.from(this.tabState.tabMap.values());+ },+ databaseList(): ComposedDatabase[] {+ return Array.from(this.resourceMap["database"].values()).map(+ (databaseSchema) => databaseSchema.database+ );+ },+ },+ actions: {+ addTab(tab: TabContext, setAsCurrentTab = true) {+ const tabCache = this.tabList.find((item) => {+ if (+ item.type === tab.type &&+ item.parentName === tab.parentName &&+ (item.type === SchemaEditorTabType.TabForDatabase ||+ (item.type === SchemaEditorTabType.TabForTable &&+ item.tableId === (tab as TableTabContext).tableId))+ ) {+ return true;+ }+ return false;+ });++ if (tabCache !== undefined) {+ tab = {+ ...tabCache,+ ...tab,+ id: tabCache.id,+ };+ }+ this.tabState.tabMap.set(tab.id, tab);++ if (setAsCurrentTab) {+ this.setCurrentTab(tab.id);+ }+ },+ setCurrentTab(tabId: string) {+ if (isUndefined(this.tabState.tabMap.get(tabId))) {+ this.tabState.currentTabId = undefined;+ } else {+ this.tabState.currentTabId = tabId;+ }+ },+ closeTab(tabId: string) {+ const tabList = Array.from(this.tabState.tabMap.values());+ const tabIndex = tabList.findIndex((tab) => tab.id === tabId);+ // Find next tab for showing.+ if (this.tabState.currentTabId === tabId) {+ let nextTabIndex = -1;+ if (tabIndex === 0) {+ nextTabIndex = 1;+ } else {+ nextTabIndex = tabIndex - 1;+ }+ const nextTab = tabList[nextTabIndex];+ if (nextTab) {+ this.setCurrentTab(nextTab.id);+ } else {+ this.setCurrentTab("");+ }+ }+ this.tabState.tabMap.delete(tabId);+ },+ findTab(parentName: string, tableId?: string) {+ let tabType = SchemaEditorTabType.TabForDatabase;+ if (tableId !== undefined) {+ tabType = SchemaEditorTabType.TabForTable;+ }++ const tab = this.tabList.find((tab) => {+ if (+ tab.type === tabType &&+ tab.parentName === parentName &&+ (tab.type === SchemaEditorTabType.TabForDatabase ||+ (tab.type === SchemaEditorTabType.TabForTable &&+ tab.tableId === tableId))+ ) {+ return true;+ }+ return false;+ });++ return tab;+ },+ getSchema(parentName: string, schemaId: string) {+ return this.resourceMap[this.resourceType]+ .get(parentName)+ ?.schemaList.find((schema) => schema.id === schemaId);+ },+ getOriginSchema(parentName: string, schemaId: string) {+ return this.resourceMap[this.resourceType]+ .get(parentName)+ ?.originSchemaList.find((schema) => schema.id === schemaId);+ },+ dropSchema(parentName: string, schemaId: string) {+ const schema = this.getSchema(parentName, schemaId);+ if (!schema) {+ return;+ }++ if (schema.status === "created") {+ const resource = this.resourceMap[this.resourceType].get(parentName);+ if (resource) {+ resource.schemaList =+ this.resourceMap[this.resourceType]+ .get(parentName)+ ?.schemaList.filter((schema) => schema.id !== schemaId) || [];++ // Close related tabs.+ for (const tab of this.tabList) {+ if (tab.parentName !== parentName) {+ continue;+ }++ if (+ tab.type === SchemaEditorTabType.TabForTable &&+ tab.schemaId === schemaId+ ) {+ this.closeTab(tab.id);+ }+ }+ }+ } else {+ schema.status = "dropped";+ }+ },+ restoreSchema(databaseId: string, schemaId: string) {+ const schema = this.getSchema(databaseId, schemaId);+ if (!schema) {+ return;+ }+ schema.status = "normal";+ },+ getTable(databaseId: string, schemaId: string, tableId: string) {+ return this.getSchema(databaseId, schemaId)?.tableList.find(+ (table) => table.id === tableId+ );+ },+ getOriginTable(databaseId: string, schemaId: string, tableId: string) {+ return this.getOriginSchema(databaseId, schemaId)?.tableList.find(+ (table) => table.id === tableId+ );+ },+ getTableWithTableTab(tab: TableTabContext) {+ return this.resourceMap[this.resourceType]+ .get(tab.parentName)+ ?.schemaList.find((schema) => schema.id === tab.schemaId)+ ?.tableList?.find((table) => table.id === tab.tableId);+ },+ dropTable(parentName: string, schemaId: string, tableId: string) {+ const table = this.getTable(parentName, schemaId, tableId);+ if (!table) {+ return;+ }++ // Remove table record and close tab for created table.+ if (table.status === "created") {+ const tableList = this.resourceMap[this.resourceType]+ .get(parentName)+ ?.schemaList.find((schema) => schema.id === schemaId)+ ?.tableList as Table[];+ const index = tableList.findIndex((item) => item.id === table.id);+ tableList.splice(index, 1);+ const tab = this.findTab(parentName, table.id);+ if (tab) {+ this.closeTab(tab.id);+ }+ } else {+ table.status = "dropped";+ }+ },+ restoreTable(parentName: string, schemaId: string, tableId: string) {+ const table = this.getTable(parentName, schemaId, tableId);+ if (!table) {+ return;+ }+ table.status = "normal";+ },+ },+});diff --git a/frontend/src/types/schemaEditorV1/atomType.ts b/frontend/src/types/schemaEditorV1/atomType.tsnew file mode 100644index 0000000000..fcf29c5c3c--- /dev/null+++ b/frontend/src/types/schemaEditorV1/atomType.ts@@ -0,0 +1,208 @@+import { v1 as uuidv1 } from "uuid";+import { isGhostTable } from "@/utils";+import {+ SchemaMetadata,+ TableMetadata,+ ColumnMetadata,+} from "../proto/v1/database_service";++type Status = "normal" | "created" | "dropped";++export interface Column {+ id: string;+ name: string;+ type: string;+ nullable: boolean;+ comment: string;+ userComment: string;+ default?: string;+ status: Status;+ classification?: string;+}++export interface PrimaryKey {+ name: string;+ columnIdList: string[];+}++export interface ForeignKey {+ // Should be an unique name.+ name: string;+ tableId: string;+ columnIdList: string[];+ referencedSchemaId: string;+ referencedTableId: string;+ referencedColumnIdList: string[];+}++export interface Table {+ id: string;+ name: string;+ engine: string;+ collation: string;+ rowCount: number;+ dataSize: number;+ comment: string;+ columnList: Column[];+ primaryKey: PrimaryKey;+ foreignKeyList: ForeignKey[];+ status: Status;+}++export interface Schema {+ id: string;+ // It should be an empty string for MySQL/TiDB.+ name: string;+ tableList: Table[];+ status: Status;+}++export const convertColumnMetadataToColumn = (+ columnMetadata: ColumnMetadata+): Column => {+ return {+ id: uuidv1(),+ name: columnMetadata.name,+ type: columnMetadata.type,+ nullable: columnMetadata.nullable,+ comment: columnMetadata.comment,+ userComment: columnMetadata.userComment,+ default: columnMetadata.default,+ classification: columnMetadata.classification,+ status: "normal",+ };+};++export const convertTableMetadataToTable = (+ tableMetadata: TableMetadata+): Table => {+ const table: Table = {+ id: uuidv1(),+ name: tableMetadata.name,+ engine: tableMetadata.engine,+ collation: tableMetadata.collation,+ rowCount: tableMetadata.rowCount,+ dataSize: tableMetadata.dataSize,+ comment: tableMetadata.comment,+ columnList: tableMetadata.columns.map((column) =>+ convertColumnMetadataToColumn(column)+ ),+ primaryKey: {+ name: "",+ columnIdList: [],+ },+ foreignKeyList: [],+ status: "normal",+ };++ for (const indexMetadata of tableMetadata.indexes) {+ if (indexMetadata.primary === true) {+ table.primaryKey.name = indexMetadata.name;+ for (const columnName of indexMetadata.expressions) {+ const column = table.columnList.find(+ (column) => column.name === columnName+ );+ if (column) {+ table.primaryKey.columnIdList.push(column.id);+ }+ }+ break;+ }+ }++ return table;+};++export const convertSchemaMetadataToSchema = (+ schemaMetadata: SchemaMetadata+): Schema => {+ const tableList: Table[] = [];++ for (const tableMetadata of schemaMetadata.tables) {+ // Don't display ghost table in Schema Editor.+ if (isGhostTable(tableMetadata)) {+ continue;+ }++ const table = convertTableMetadataToTable(tableMetadata);+ tableList.push(table);+ }++ return {+ id: uuidv1(),+ name: schemaMetadata.name,+ tableList: tableList,+ status: "normal",+ };+};++export const convertSchemaMetadataList = (+ schemaMetadataList: SchemaMetadata[]+): Schema[] => {+ // Compose all tables of each schema.+ const schemaList: Schema[] = schemaMetadataList.map((schemaMetadata) =>+ convertSchemaMetadataToSchema(schemaMetadata)+ );++ // Build foreign keys for schema and referenced schema.+ for (const schemaMetadata of schemaMetadataList) {+ const schema = schemaList.find(+ (schema) => schema.name === schemaMetadata.name+ );+ if (!schema) {+ continue;+ }++ const tableList = schema.tableList;+ for (const tableMetadata of schemaMetadata.tables) {+ const table = tableList.find(+ (table) => table.name === tableMetadata.name+ );+ if (!table) {+ continue;+ }++ const foreignKeyList: ForeignKey[] = [];+ for (const foreignKeyMetadata of tableMetadata.foreignKeys) {+ const referencedSchema = schemaList.find(+ (schema) => schema.name === foreignKeyMetadata.referencedSchema+ );+ const referencedTable = referencedSchema?.tableList.find(+ (table) => table.name === foreignKeyMetadata.referencedTable+ );+ if (!referencedSchema || !referencedTable) {+ continue;+ }++ const fk: ForeignKey = {+ name: foreignKeyMetadata.name,+ tableId: table.id,+ columnIdList: [],+ referencedSchemaId: referencedSchema.id,+ referencedTableId: referencedTable.id,+ referencedColumnIdList: [],+ };+ for (const columnName of foreignKeyMetadata.columns) {+ const column = table.columnList.find(+ (column) => column.name === columnName+ );+ if (column) {+ fk.columnIdList.push(column.id);+ }+ }+ for (const referencedColumnName of foreignKeyMetadata.referencedColumns) {+ const referencedColumn = referencedTable.columnList.find(+ (column) => column.name === referencedColumnName+ );+ if (referencedColumn) {+ fk.referencedColumnIdList.push(referencedColumn.id);+ }+ }+ foreignKeyList.push(fk);+ }+ table.foreignKeyList = foreignKeyList;+ }+ }++ return schemaList;+};diff --git a/frontend/src/types/schemaEditorV1/index.ts b/frontend/src/types/schemaEditorV1/index.tsnew file mode 100644index 0000000000..74fdda058e--- /dev/null+++ b/frontend/src/types/schemaEditorV1/index.ts@@ -0,0 +1,2 @@+export * from "./atomType";+export * from "./state";diff --git a/frontend/src/types/schemaEditorV1/state.ts b/frontend/src/types/schemaEditorV1/state.tsnew file mode 100644index 0000000000..03dce5422f--- /dev/null+++ b/frontend/src/types/schemaEditorV1/state.ts@@ -0,0 +1,53 @@+import { SchemaDesign } from "../proto/v1/schema_design_service";+import { ComposedDatabase } from "../v1";+import { Schema } from "./atomType";++export enum SchemaEditorTabType {+ TabForDatabase = "database",+ TabForTable = "table",+}++// Tab context for editing database.+export interface DatabaseTabContext {+ id: string;+ type: SchemaEditorTabType.TabForDatabase;+ // Parent could be a database.+ parentName: string;+ selectedSchemaId?: string;+}++// Tab context for editing table.+export interface TableTabContext {+ id: string;+ type: SchemaEditorTabType.TabForTable;+ // Parent could be a database or a branch.+ parentName: string;+ schemaId: string;+ tableId: string;+}++export type TabContext = DatabaseTabContext | TableTabContext;++export interface DatabaseSchema {+ database: ComposedDatabase;+ schemaList: Schema[];+ originSchemaList: Schema[];+}++export interface BranchSchema {+ branch: SchemaDesign;+ schemaList: Schema[];+ originSchemaList: Schema[];+}++export interface SchemaEditorV1State {+ resourceType: "database" | "branch";+ resourceMap: {+ database: Map<string, DatabaseSchema>;+ branch: Map<string, BranchSchema>;+ };+ tabState: {+ tabMap: Map<string, TabContext>;+ currentTabId?: string;+ };+}