Skip to content

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

feat(schema-editor): support procedure and function nodes (#11693)

Liu Jia7703e446efd
diff --git a/frontend/src/components/Icon/FunctionIcon.vue b/frontend/src/components/Icon/FunctionIcon.vue
index 1051145ff8..c547453e3f 100644
--- a/frontend/src/components/Icon/FunctionIcon.vue
+++ b/frontend/src/components/Icon/FunctionIcon.vue
@@ -1,5 +1,5 @@
<template>
- <SquareFunctionIcon class="opacity-75" />
+ <SquareFunctionIcon class="text-gray-400" />
</template>
<script setup lang="ts">
diff --git a/frontend/src/components/Icon/ProcedureIcon.vue b/frontend/src/components/Icon/ProcedureIcon.vue
index e203e6bd91..b9f87f2d4a 100644
--- a/frontend/src/components/Icon/ProcedureIcon.vue
+++ b/frontend/src/components/Icon/ProcedureIcon.vue
@@ -1,5 +1,5 @@
<template>
- <ParenthesesIcon class="opacity-75" />
+ <ParenthesesIcon class="text-gray-400" />
</template>
<script setup lang="ts">
diff --git a/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/FunctionGroupNodeCheckbox.vue b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/FunctionGroupNodeCheckbox.vue
new file mode 100644
index 0000000000..d70d7e179b
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/FunctionGroupNodeCheckbox.vue
@@ -0,0 +1,40 @@
+<template>
+ <NCheckbox
+ :checked="state.checked"
+ :indeterminate="state.indeterminate"
+ size="small"
+ @update:checked="update"
+ @click.prevent.stop
+ />
+</template>
+
+<script setup lang="ts">
+import { NCheckbox } from "naive-ui";
+import { computed } from "vue";
+import { useSchemaEditorContext } from "../../context";
+import type { TreeNodeForGroup } from "../common";
+
+const props = defineProps<{
+ node: TreeNodeForGroup<"function">;
+}>();
+
+const { getAllFunctionsSelectionState, updateAllFunctionsSelection } =
+ useSchemaEditorContext();
+
+const state = computed(() => {
+ return getAllFunctionsSelectionState(
+ props.node.db,
+ props.node.metadata,
+ props.node.metadata.schema.functions
+ );
+});
+
+const update = (on: boolean) => {
+ updateAllFunctionsSelection(
+ props.node.db,
+ props.node.metadata,
+ props.node.metadata.schema.functions,
+ on
+ );
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/FunctionNodeCheckbox.vue b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/FunctionNodeCheckbox.vue
new file mode 100644
index 0000000000..054d50fe4d
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/FunctionNodeCheckbox.vue
@@ -0,0 +1,30 @@
+<template>
+ <NCheckbox
+ :checked="state.checked"
+ :indeterminate="state.indeterminate"
+ size="small"
+ @update:checked="update"
+ @click.prevent.stop
+ />
+</template>
+
+<script setup lang="ts">
+import { NCheckbox } from "naive-ui";
+import { computed } from "vue";
+import { useSchemaEditorContext } from "../../context";
+import type { TreeNodeForFunction } from "../common";
+
+const props = defineProps<{
+ node: TreeNodeForFunction;
+}>();
+
+const { getFunctionSelectionState, updateFunctionSelection } =
+ useSchemaEditorContext();
+
+const state = computed(() => {
+ return getFunctionSelectionState(props.node.db, props.node.metadata);
+});
+const update = (on: boolean) => {
+ updateFunctionSelection(props.node.db, props.node.metadata, on);
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/NodeCheckbox.vue b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/NodeCheckbox.vue
index f188612815..55ca9be456 100644
--- a/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/NodeCheckbox.vue
+++ b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/NodeCheckbox.vue
@@ -1,13 +1,30 @@
<template>
- <SchemaNodeCheckbox v-if="node.type === 'schema'" :node="node" />
+ <TableGroupNodeCheckbox
+ v-if="node.type === 'group' && node.group === 'table'"
+ :node="node as TreeNodeForGroup<'table'>"
+ />
+ <ProcedureGroupNodeCheckbox
+ v-if="node.type === 'group' && node.group === 'procedure'"
+ :node="node as TreeNodeForGroup<'procedure'>"
+ />
+ <FunctionGroupNodeCheckbox
+ v-if="node.type === 'group' && node.group === 'function'"
+ :node="node as TreeNodeForGroup<'function'>"
+ />
<TableNodeCheckbox v-if="node.type === 'table'" :node="node" />
<ColumnNodeCheckbox v-if="node.type === 'column'" :node="node" />
+ <ProcedureNodeCheckbox v-if="node.type === 'procedure'" :node="node" />
+ <FunctionNodeCheckbox v-if="node.type === 'function'" :node="node" />
</template>
<script setup lang="ts">
-import type { TreeNode } from "../common";
+import type { TreeNode, TreeNodeForGroup } from "../common";
import ColumnNodeCheckbox from "./ColumnNodeCheckbox.vue";
-import SchemaNodeCheckbox from "./SchemaNodeCheckbox.vue";
+import FunctionGroupNodeCheckbox from "./FunctionGroupNodeCheckbox.vue";
+import FunctionNodeCheckbox from "./FunctionNodeCheckbox.vue";
+import ProcedureGroupNodeCheckbox from "./ProcedureGroupNodeCheckbox.vue";
+import ProcedureNodeCheckbox from "./ProcedureNodeCheckbox.vue";
+import TableGroupNodeCheckbox from "./TableGroupNodeCheckbox.vue";
import TableNodeCheckbox from "./TableNodeCheckbox.vue";
defineProps<{
diff --git a/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/ProcedureGroupNodeCheckbox.vue b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/ProcedureGroupNodeCheckbox.vue
new file mode 100644
index 0000000000..5e22b52fc5
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/ProcedureGroupNodeCheckbox.vue
@@ -0,0 +1,40 @@
+<template>
+ <NCheckbox
+ :checked="state.checked"
+ :indeterminate="state.indeterminate"
+ size="small"
+ @update:checked="update"
+ @click.prevent.stop
+ />
+</template>
+
+<script setup lang="ts">
+import { NCheckbox } from "naive-ui";
+import { computed } from "vue";
+import { useSchemaEditorContext } from "../../context";
+import type { TreeNodeForGroup } from "../common";
+
+const props = defineProps<{
+ node: TreeNodeForGroup<"procedure">;
+}>();
+
+const { getAllProceduresSelectionState, updateAllProceduresSelection } =
+ useSchemaEditorContext();
+
+const state = computed(() => {
+ return getAllProceduresSelectionState(
+ props.node.db,
+ props.node.metadata,
+ props.node.metadata.schema.procedures
+ );
+});
+
+const update = (on: boolean) => {
+ updateAllProceduresSelection(
+ props.node.db,
+ props.node.metadata,
+ props.node.metadata.schema.procedures,
+ on
+ );
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/ProcedureNodeCheckbox.vue b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/ProcedureNodeCheckbox.vue
new file mode 100644
index 0000000000..5af6b00f88
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/ProcedureNodeCheckbox.vue
@@ -0,0 +1,30 @@
+<template>
+ <NCheckbox
+ :checked="state.checked"
+ :indeterminate="state.indeterminate"
+ size="small"
+ @update:checked="update"
+ @click.prevent.stop
+ />
+</template>
+
+<script setup lang="ts">
+import { NCheckbox } from "naive-ui";
+import { computed } from "vue";
+import { useSchemaEditorContext } from "../../context";
+import type { TreeNodeForProcedure } from "../common";
+
+const props = defineProps<{
+ node: TreeNodeForProcedure;
+}>();
+
+const { getProcedureSelectionState, updateProcedureSelection } =
+ useSchemaEditorContext();
+
+const state = computed(() => {
+ return getProcedureSelectionState(props.node.db, props.node.metadata);
+});
+const update = (on: boolean) => {
+ updateProcedureSelection(props.node.db, props.node.metadata, on);
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/TableGroupNodeCheckbox.vue b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/TableGroupNodeCheckbox.vue
new file mode 100644
index 0000000000..3de3e87aaf
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/NodeCheckbox/TableGroupNodeCheckbox.vue
@@ -0,0 +1,40 @@
+<template>
+ <NCheckbox
+ :checked="state.checked"
+ :indeterminate="state.indeterminate"
+ size="small"
+ @update:checked="update"
+ @click.prevent.stop
+ />
+</template>
+
+<script setup lang="ts">
+import { NCheckbox } from "naive-ui";
+import { computed } from "vue";
+import { useSchemaEditorContext } from "../../context";
+import type { TreeNodeForGroup } from "../common";
+
+const props = defineProps<{
+ node: TreeNodeForGroup<"table">;
+}>();
+
+const { getAllTablesSelectionState, updateAllTablesSelection } =
+ useSchemaEditorContext();
+
+const state = computed(() => {
+ return getAllTablesSelectionState(
+ props.node.db,
+ props.node.metadata,
+ props.node.metadata.schema.tables
+ );
+});
+
+const update = (on: boolean) => {
+ updateAllTablesSelection(
+ props.node.db,
+ props.node.metadata,
+ props.node.metadata.schema.tables,
+ on
+ );
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/NodePrefix.vue b/frontend/src/components/SchemaEditorLite/Aside/NodePrefix.vue
new file mode 100644
index 0000000000..14b6774ab2
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/NodePrefix.vue
@@ -0,0 +1,96 @@
+<template>
+ <div class="flex flex-row items-center gap-x-1 text-sm">
+ <NodeCheckbox v-if="selectionEnabled" :node="node" />
+
+ <template v-if="node.type === 'instance'">
+ <InstanceV1EngineIcon :instance="node.instance" />
+ <span class="text-gray-500">
+ {{ node.instance.environmentEntity.title }}
+ </span>
+ </template>
+ <template v-if="node.type === 'database'">
+ <DatabaseIcon class="w-4 h-auto text-gray-400" />
+ <span class="text-gray-500">
+ {{ node.db.effectiveEnvironmentEntity.title }}
+ </span>
+ </template>
+
+ <template v-if="node.type === 'group'">
+ <SchemaIcon
+ v-if="node.group === 'table'"
+ class="w-4 h-auto text-gray-400"
+ />
+ <ProcedureIcon
+ v-if="node.group === 'procedure'"
+ class="w-4 h-auto text-gray-400"
+ />
+ <FunctionIcon
+ v-if="node.group === 'function'"
+ class="w-4 h-auto text-gray-400"
+ />
+ </template>
+
+ <div
+ v-if="node.type === 'column'"
+ class="w-4 h-4 inline-flex items-center justify-center"
+ >
+ <PrimaryKeyIcon v-if="isPrimaryKeyColumn" class="w-4 h-4" />
+ <IndexIcon v-if="isIndexColumn" class="!w-4 !h-4 text-gray-500" />
+ </div>
+
+ <SchemaIcon
+ v-if="node.type === 'schema'"
+ class="w-4 h-auto text-gray-400"
+ />
+ <TableIcon v-if="node.type === 'table'" class="w-4 h-auto text-gray-400" />
+ <ProcedureIcon
+ v-if="node.type === 'procedure'"
+ class="w-4 h-auto text-gray-400"
+ />
+ <FunctionIcon
+ v-if="node.type === 'function'"
+ class="w-4 h-auto text-gray-400"
+ />
+ </div>
+</template>
+
+<script setup lang="ts">
+import { computed } from "vue";
+import {
+ DatabaseIcon,
+ FunctionIcon,
+ IndexIcon,
+ PrimaryKeyIcon,
+ ProcedureIcon,
+ SchemaIcon,
+ TableIcon,
+} from "@/components/Icon";
+import type { InstanceV1EngineIcon } from "@/components/v2";
+import { useSchemaEditorContext } from "../context";
+import NodeCheckbox from "./NodeCheckbox";
+import type { TreeNode } from "./common";
+
+const props = defineProps<{
+ node: TreeNode;
+}>();
+
+const { selectionEnabled } = useSchemaEditorContext();
+
+const isPrimaryKeyColumn = computed(() => {
+ const { node } = props;
+ if (node.type !== "column") return false;
+
+ const { table, column } = node.metadata;
+ const pk = table.indexes.find((idx) => idx.primary);
+ if (!pk) return false;
+ return pk.expressions.includes(column.name);
+});
+const isIndexColumn = computed(() => {
+ if (isPrimaryKeyColumn.value) return false;
+
+ const { node } = props;
+ if (node.type !== "column") return false;
+ const { table, column } = node.metadata;
+ return table.indexes.some((idx) => idx.expressions.includes(column.name));
+});
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/Tree.vue b/frontend/src/components/SchemaEditorLite/Aside/Tree.vue
index 8371aae034..9adb77b7d0 100644
--- a/frontend/src/components/SchemaEditorLite/Aside/Tree.vue
+++ b/frontend/src/components/SchemaEditorLite/Aside/Tree.vue
@@ -45,13 +45,13 @@
<NDropdown
trigger="manual"
placement="bottom-start"
- :show="contextMenu.showDropdown"
+ :show="contextMenu.show"
:options="contextMenuOptions"
:x="contextMenu.clientX"
:y="contextMenu.clientY"
to="body"
- @select="handleContextMenuDropdownSelect"
- @clickoutside="handleDropdownClickoutside"
+ @select="handleContextMenuSelect"
+ @clickoutside="handleContextMenuClickoutside"
/>
</div>
</div>
@@ -71,48 +71,62 @@
:table="state.tableNameModalContext.table"
@close="state.tableNameModalContext = undefined"
/>
+
+ <ProcedureNameModal
+ v-if="state.procedureNameModalContext !== undefined"
+ :database="state.procedureNameModalContext.db"
+ :metadata="state.procedureNameModalContext.database"
+ :schema="state.procedureNameModalContext.schema"
+ :procedure="state.procedureNameModalContext.procedure"
+ @close="state.procedureNameModalContext = undefined"
+ />
+
+ <FunctionNameModal
+ v-if="state.functionNameModalContext !== undefined"
+ :database="state.functionNameModalContext.db"
+ :metadata="state.functionNameModalContext.database"
+ :schema="state.functionNameModalContext.schema"
+ :func="state.functionNameModalContext.function"
+ @close="state.functionNameModalContext = undefined"
+ />
</template>
<script lang="ts" setup>
import { useElementSize } from "@vueuse/core";
import { cloneDeep, debounce, escape, head } from "lodash-es";
import { MoreHorizontalIcon, CopyIcon } from "lucide-vue-next";
-import type { TreeOption, DropdownOption } from "naive-ui";
+import type { TreeOption } from "naive-ui";
import { NInput, NDropdown, NTree, NPerformantEllipsis } from "naive-ui";
-import type { VNodeChild } from "vue";
import { computed, onMounted, watch, ref, h, reactive, nextTick } from "vue";
import { useI18n } from "vue-i18n";
-import { DatabaseIcon, SchemaIcon, TableIcon } from "@/components/Icon";
-import { InstanceV1EngineIcon } from "@/components/v2";
import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
import type { ComposedDatabase } from "@/types";
-import { Engine } from "@/types/proto/v1/common";
import type {
DatabaseMetadata,
+ FunctionMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
} from "@/types/proto/v1/database_service";
import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";
+import FunctionNameModal from "../Modals/FunctionNameModal.vue";
+import ProcedureNameModal from "../Modals/ProcedureNameModal.vue";
import SchemaNameModal from "../Modals/SchemaNameModal.vue";
import TableNameModal from "../Modals/TableNameModal.vue";
import { useSchemaEditorContext } from "../context";
import { keyForResource, keyForResourceName } from "../context/common";
-import { engineHasSchema } from "../engine-specs";
-import NodeCheckbox from "./NodeCheckbox";
+import { engineSupportsMultiSchema } from "../spec";
+import NodePrefix from "./NodePrefix.vue";
import type {
TreeNode,
TreeNodeForInstance,
TreeNodeForSchema,
TreeNodeForTable,
+ TreeNodeForGroup,
} from "./common";
import { buildTree } from "./common";
+import { useContextMenu } from "./context-menu";
-interface TreeContextMenu {
- showDropdown: boolean;
- clientX: number;
- clientY: number;
- treeNode?: TreeNode;
-}
interface LocalState {
shouldRelocateTreeNode: boolean;
schemaNameModalContext?: {
@@ -126,6 +140,18 @@ interface LocalState {
schema: SchemaMetadata;
table?: TableMetadata;
};
+ procedureNameModalContext?: {
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure?: ProcedureMetadata;
+ };
+ functionNameModalContext?: {
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ function?: FunctionMetadata;
+ };
}
const { t } = useI18n();
@@ -136,13 +162,14 @@ const {
readonly,
currentTab,
disableDiffColoring,
- selectionEnabled,
addTab,
markEditStatus,
removeEditStatus,
getSchemaStatus,
getTableStatus,
getColumnStatus,
+ getProcedureStatus,
+ getFunctionStatus,
upsertTableConfig,
queuePendingScrollToTable,
queuePendingScrollToColumn,
@@ -151,12 +178,6 @@ const state = reactive<LocalState>({
shouldRelocateTreeNode: false,
});
-const contextMenu = reactive<TreeContextMenu>({
- showDropdown: false,
- clientX: 0,
- clientY: 0,
- treeNode: undefined,
-});
const treeContainerElRef = ref<HTMLElement>();
const { height: treeContainerHeight } = useElementSize(
treeContainerElRef,
@@ -172,75 +193,14 @@ const selectedKeysRef = ref<string[]>([]);
const treeDataRef = ref<TreeNode[]>([]);
const treeNodeMap = new Map<string, TreeNode>();
-const contextMenuOptions = computed(() => {
- const treeNode = contextMenu.treeNode;
- if (!treeNode) return [];
- if (treeNode.type === "instance") return [];
- const { engine } = treeNode.db.instanceEntity;
- if (treeNode.type === "database") {
- const options: DropdownOption[] = [];
- if (engineHasSchema(engine)) {
- options.push({
- key: "create-schema",
- label: t("schema-editor.actions.create-schema"),
- });
- } else {
- options.push({
- key: "create-table",
- label: t("schema-editor.actions.create-table"),
- });
- }
- return options;
- } else if (treeNode.type === "schema") {
- const options: DropdownOption[] = [];
- if (engine === Engine.POSTGRES) {
- const status = getSchemaStatus(treeNode.db, treeNode.metadata);
- if (status === "dropped") {
- options.push({
- key: "restore",
- label: t("schema-editor.actions.restore"),
- });
- } else {
- options.push({
- key: "create-table",
- label: t("schema-editor.actions.create-table"),
- });
- options.push({
- key: "rename",
- label: t("schema-editor.actions.rename"),
- });
- options.push({
- key: "drop-schema",
- label: t("schema-editor.actions.drop-schema"),
- });
- }
- }
- return options;
- } else if (treeNode.type === "table") {
- const options: DropdownOption[] = [];
- const status = getTableStatus(treeNode.db, treeNode.metadata);
- if (status === "dropped") {
- options.push({
- key: "restore",
- label: t("schema-editor.actions.restore"),
- });
- } else {
- if (status !== "normal") {
- options.push({
- key: "rename",
- label: t("schema-editor.actions.rename"),
- });
- }
- options.push({
- key: "drop",
- label: t("schema-editor.actions.drop-table"),
- });
- }
- return options;
- }
-
- return [];
-});
+const {
+ menu: contextMenu,
+ options: contextMenuOptions,
+ events: contextMenuEvents,
+ handleShow: handleContextMenuShow,
+ handleSelect: handleContextMenuSelect,
+ handleClickOutside: handleContextMenuClickoutside,
+} = useContextMenu();
const upsertExpandedKeys = (key: string) => {
if (expandedKeysRef.value.includes(key)) return;
@@ -252,36 +212,44 @@ const expandNodeRecursively = (node: TreeNode | undefined) => {
// column nodes are not expandable
expandNodeRecursively(node.parent);
}
+ if (node.type === "group") {
+ upsertExpandedKeys(node.key);
+ expandNodeRecursively(node.parent);
+ }
if (node.type === "table") {
- const key = keyForResource(node.db, node.metadata);
- upsertExpandedKeys(key);
+ upsertExpandedKeys(node.key);
expandNodeRecursively(node.parent);
}
if (node.type === "schema") {
- const key = keyForResource(node.db, node.metadata);
- upsertExpandedKeys(key);
+ upsertExpandedKeys(node.key);
expandNodeRecursively(node.parent);
}
if (node.type === "database") {
- const key = node.db.name;
- upsertExpandedKeys(key);
+ upsertExpandedKeys(node.key);
expandNodeRecursively(node.parent);
}
if (node.type === "instance") {
- const key = node.instance.name;
- upsertExpandedKeys(key);
+ upsertExpandedKeys(node.key);
}
};
-const findFirstSchemaNode = (
- nodeList: TreeNodeForSchema[] | TreeNodeForInstance[]
-) => {
+const findFirstTableGroupNode = (
+ nodeList: TreeNodeForSchema[] | TreeNodeForInstance[] | TreeNodeForGroup[]
+): TreeNodeForGroup<"table"> | undefined => {
const first = nodeList[0];
- if (!first) return undefined;
+ if (!first) {
+ return undefined;
+ }
+ if (first.type === "group" && first.group === "table") {
+ return first as TreeNodeForGroup<"table">;
+ }
+ if (first.type === "instance") {
+ const firstDatabaseNode = head(first.children);
+ return findFirstTableGroupNode(firstDatabaseNode?.children ?? []);
+ }
if (first.type === "schema") {
- return first;
+ return findFirstTableGroupNode(first.children);
}
- const firstDatabaseNode = head(first.children);
- return head(firstDatabaseNode?.children);
+ return undefined;
};
const buildDatabaseTreeData = (openFirstChild: boolean) => {
const treeNodeList = buildTree(targets.value, treeNodeMap, {
@@ -290,11 +258,11 @@ const buildDatabaseTreeData = (openFirstChild: boolean) => {
treeDataRef.value = treeNodeList;
if (openFirstChild) {
- const firstSchemaNode = findFirstSchemaNode(treeNodeList);
- if (firstSchemaNode) {
+ const firstTableGroupNode = findFirstTableGroupNode(treeNodeList);
+ if (firstTableGroupNode) {
nextTick(() => {
// Auto expand the first tree node.
- openTabForTreeNode(firstSchemaNode);
+ openTabForTreeNode(firstTableGroupNode);
});
}
}
@@ -308,7 +276,13 @@ const tabWatchKey = computed(() => {
const tab = currentTab.value;
if (!tab) return undefined;
if (tab.type === "database") {
- return keyForResourceName(tab.database.name, tab.selectedSchema);
+ return keyForResourceName(
+ {
+ database: tab.database.name,
+ schema: tab.selectedSchema,
+ },
+ "table-group"
+ );
}
return keyForResource(tab.database, tab.metadata);
});
@@ -323,8 +297,11 @@ watch(tabWatchKey, () => {
if (tab.type === "database") {
const { database, selectedSchema: schema } = tab;
- if (schema) {
- const key = keyForResourceName(database.name, schema);
+ if (schema !== undefined) {
+ const key = keyForResourceName(
+ { database: database.name, schema },
+ "table-group"
+ );
const node = treeNodeMap.get(key);
if (node) {
expandNodeRecursively(node);
@@ -343,6 +320,30 @@ watch(tabWatchKey, () => {
}
const tableKey = keyForResource(database, { schema, table });
selectedKeysRef.value = [tableKey];
+ } else if (tab.type === "procedure") {
+ const {
+ database,
+ metadata: { schema, procedure },
+ } = tab;
+ const schemaKey = keyForResource(database, { schema });
+ const schemaNode = treeNodeMap.get(schemaKey);
+ if (schemaNode) {
+ expandNodeRecursively(schemaNode);
+ }
+ const procedureKey = keyForResource(database, { schema, procedure });
+ selectedKeysRef.value = [procedureKey];
+ } else if (tab.type === "function") {
+ const {
+ database,
+ metadata: { schema, function: func },
+ } = tab;
+ const schemaKey = keyForResource(database, { schema });
+ const schemaNode = treeNodeMap.get(schemaKey);
+ if (schemaNode) {
+ expandNodeRecursively(schemaNode);
+ }
+ const procedureKey = keyForResource(database, { schema, function: func });
+ selectedKeysRef.value = [procedureKey];
}
if (state.shouldRelocateTreeNode) {
@@ -365,14 +366,19 @@ const openTabForTreeNode = (node: TreeNode) => {
metadata: node.metadata,
});
return;
- } else if (node.type === "table") {
+ }
+ if (node.type === "table") {
expandNodeRecursively(node);
addTab({
type: "table",
database: node.db,
metadata: node.metadata,
});
- } else if (node.type === "schema") {
+ }
+ if (
+ node.type === "schema" ||
+ (node.type === "group" && node.group === "table")
+ ) {
expandNodeRecursively(node);
addTab({
type: "database",
@@ -381,6 +387,22 @@ const openTabForTreeNode = (node: TreeNode) => {
selectedSchema: node.metadata.schema.name,
});
}
+ if (node.type === "procedure") {
+ expandNodeRecursively(node);
+ addTab({
+ type: "procedure",
+ database: node.db,
+ metadata: node.metadata,
+ });
+ }
+ if (node.type === "function") {
+ expandNodeRecursively(node);
+ addTab({
+ type: "function",
+ database: node.db,
+ metadata: node.metadata,
+ });
+ }
state.shouldRelocateTreeNode = true;
};
@@ -391,87 +413,51 @@ onMounted(() => {
// Render prefix icons before label text.
const renderPrefix = ({ option }: { option: TreeOption }) => {
- const treeNode = option as TreeNode;
-
- if (treeNode.type === "instance") {
- const { instance } = treeNode;
- const children = [
- h(InstanceV1EngineIcon, {
- instance,
- }),
- h(
- "span",
- {
- class: "text-gray-500 text-sm",
- },
- `(${instance.environmentEntity.title})`
- ),
- ];
-
- return h("span", { class: "flex items-center gap-x-1" }, children);
- } else if (treeNode.type === "database") {
- return h("span", { class: "flex items-center gap-x-1" }, [
- h(DatabaseIcon, {
- class: "w-4 h-auto text-gray-400",
- }),
- h(
- "span",
- {
- class: "text-gray-500 text-sm",
- },
- `(${treeNode.db.effectiveEnvironmentEntity.title})`
- ),
- ]);
- }
-
- const children: VNodeChild[] = [];
- if (selectionEnabled.value) {
- children.push(
- h(NodeCheckbox, {
- node: treeNode,
- })
- );
- }
- if (treeNode.type === "schema") {
- children.push(
- h(SchemaIcon, {
- class: "w-4 h-auto text-gray-400",
- })
- );
- } else if (treeNode.type === "table") {
- children.push(
- h(TableIcon, {
- class: "w-4 h-auto text-gray-400",
- })
- );
- }
- if (children.length > 0) {
- return h("div", { class: "flex flex-row items-center gap-x-1" }, children);
- }
- return null;
+ const node = option as TreeNode;
+ return h(NodePrefix, { node });
};
// Render label text.
const renderLabel = ({ option }: { option: TreeOption }) => {
- const treeNode = option as TreeNode;
+ const node = option as TreeNode;
const additionalClassList: string[] = ["select-none"];
- let label = treeNode.label;
+ let label = node.label;
- if (treeNode.type === "schema") {
- const { db, metadata } = treeNode;
- additionalClassList.push(getSchemaStatus(db, metadata));
+ if (node.type === "placeholder") {
+ return h(
+ "span",
+ { class: "text-control-placeholder italic" },
+ `<${t("common.empty")}>`
+ );
+ }
- if (db.instanceEntity.engine !== Engine.POSTGRES) {
- label = t("db.tables");
- } else {
- label = metadata.schema.name;
+ if (node.type === "schema") {
+ const { db, metadata } = node;
+ additionalClassList.push(getSchemaStatus(db, metadata));
+ label = metadata.schema.name;
+ }
+ if (node.type === "group") {
+ label = `(${node.group} - group)`;
+ if (node.group === "table") {
+ return t("schema-editor.tables");
}
- } else if (treeNode.type === "table") {
- const { db, metadata } = treeNode;
+ if (node.group === "view") {
+ return t("schema-editor.views");
+ }
+ if (node.group === "procedure") {
+ return t("schema-editor.procedures");
+ }
+ if (node.group === "function") {
+ return t("schema-editor.functions");
+ }
+ }
+ if (node.type === "table") {
+ const { db, metadata } = node;
additionalClassList.push(getTableStatus(db, metadata));
label = metadata.table.name;
- } else if (treeNode.type === "column") {
- const { db, metadata } = treeNode;
+ }
+ if (node.type === "column") {
+ const { db, metadata } = node;
additionalClassList.push(getColumnStatus(db, metadata));
const { name } = metadata.column;
if (name) {
@@ -481,6 +467,16 @@ const renderLabel = ({ option }: { option: TreeOption }) => {
additionalClassList.push("text-control-placeholder italic");
}
}
+ if (node.type === "procedure") {
+ const { db, metadata } = node;
+ additionalClassList.push(getProcedureStatus(db, metadata));
+ label = metadata.procedure.name;
+ }
+ if (node.type === "function") {
+ const { db, metadata } = node;
+ additionalClassList.push(getFunctionStatus(db, metadata));
+ label = metadata.function.name;
+ }
return h(
NPerformantEllipsis,
{
@@ -502,38 +498,47 @@ const renderSuffix = ({ option }: { option: TreeOption }) => {
if (readonly.value) {
return null;
}
- const treeNode = option as TreeNode;
+ const node = option as TreeNode;
const menuIcon = h(MoreHorizontalIcon, {
class: "w-4 h-auto text-gray-600",
onClick: (e) => {
- handleShowDropdown(e, treeNode);
+ handleContextMenuShow(e, node);
},
});
- if (treeNode.type === "database") {
- return menuIcon;
- } else if (treeNode.type === "schema") {
- const { engine } = treeNode.db.instanceEntity;
- if (engine === Engine.POSTGRES) {
+ if (node.type === "database") {
+ const { engine } = node.db.instanceEntity;
+ if (engineSupportsMultiSchema(engine)) {
return menuIcon;
}
- } else if (treeNode.type === "table") {
+ }
+ if (node.type === "schema") {
+ return menuIcon;
+ }
+ if (node.type === "group") {
+ return menuIcon;
+ }
+ if (node.type === "table") {
const icons = [menuIcon];
-
- if (!readonly.value) {
- const duplicateIcon = h(CopyIcon, {
- class: "w-4 h-auto mr-2 text-gray-600",
- onClick: (e) => {
- e.preventDefault();
- e.stopPropagation();
- e.stopImmediatePropagation();
-
- handleDuplicateTable(treeNode);
- },
- });
- icons.unshift(duplicateIcon);
- }
+ const duplicateIcon = h(CopyIcon, {
+ class: "w-4 h-auto mr-2 text-gray-600",
+ onClick: (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ e.stopImmediatePropagation();
+
+ handleDuplicateTable(node);
+ },
+ });
+ icons.unshift(duplicateIcon);
return icons;
}
+ if (node.type === "procedure") {
+ return menuIcon;
+ }
+ if (node.type === "function") {
+ return menuIcon;
+ }
+
return null;
};
@@ -640,37 +645,32 @@ const handleDuplicateTable = (treeNode: TreeNodeForTable) => {
});
};
-const handleShowDropdown = (e: MouseEvent, treeNode: TreeNode) => {
- e.preventDefault();
- e.stopPropagation();
- contextMenu.treeNode = treeNode;
- contextMenu.showDropdown = true;
- contextMenu.clientX = e.clientX;
- contextMenu.clientY = e.clientY;
- selectedKeysRef.value = [treeNode.key];
-};
-
// Set event handler to tree nodes.
const nodeProps = ({ option }: { option: TreeOption }) => {
- const treeNode = option as TreeNode;
+ const node = option as TreeNode;
return {
async onClick(e: MouseEvent) {
// Check if clicked on the content part.
// And ignore the fold/unfold arrow.
if (isDescendantOf(e.target as Element, ".n-tree-node-content")) {
state.shouldRelocateTreeNode = false;
- if (treeNode.type === "instance") {
- expandNodeRecursively(treeNode);
- } else if (treeNode.type === "database") {
- expandNodeRecursively(treeNode);
+ if (node.type === "group") {
+ expandNodeRecursively(node);
+ if (node.group === "table") {
+ openTabForTreeNode(node);
+ }
+ } else if (node.type === "instance") {
+ expandNodeRecursively(node);
+ } else if (node.type === "database") {
+ expandNodeRecursively(node);
addTab({
type: "database",
- database: treeNode.db,
- metadata: treeNode.metadata,
- selectedSchema: head(treeNode.metadata.database.schemas)?.name,
+ database: node.db,
+ metadata: node.metadata,
+ selectedSchema: head(node.metadata.database.schemas)?.name,
});
} else {
- openTabForTreeNode(treeNode);
+ openTabForTreeNode(node);
}
} else {
nextTick(() => {
@@ -679,77 +679,133 @@ const nodeProps = ({ option }: { option: TreeOption }) => {
}
},
oncontextmenu(e: MouseEvent) {
- handleShowDropdown(e, treeNode);
+ handleContextMenuShow(e, node);
},
};
};
-const handleContextMenuDropdownSelect = async (key: string) => {
- const treeNode = contextMenu.treeNode;
- if (!treeNode) return;
- if (treeNode.type === "database") {
- const engine = treeNode.db.instanceEntity.engine;
- if (key === "create-table") {
- if (!engineHasSchema(engine)) {
- const schema = head(treeNode.metadata.database.schemas);
- if (!schema) {
- return;
- }
- state.tableNameModalContext = {
- db: treeNode.db,
- database: treeNode.metadata.database,
- schema: schema,
- table: undefined,
- };
- }
- } else if (key === "create-schema") {
- if (engineHasSchema(engine)) {
- state.schemaNameModalContext = {
- db: treeNode.db,
- database: treeNode.metadata.database,
- schema: undefined,
- };
- }
- }
- } else if (treeNode.type === "schema") {
- if (key === "create-table") {
- state.tableNameModalContext = {
- db: treeNode.db,
- database: treeNode.metadata.database,
- schema: treeNode.metadata.schema,
- table: undefined,
- };
- } else if (key === "drop-schema") {
- markEditStatus(treeNode.db, treeNode.metadata, "dropped");
- } else if (key === "restore") {
- removeEditStatus(treeNode.db, treeNode.metadata, /* recursive */ false);
- }
- } else if (treeNode.type === "table") {
- if (key === "rename") {
- state.tableNameModalContext = {
- db: treeNode.db,
- database: treeNode.metadata.database,
- schema: treeNode.metadata.schema,
- table: treeNode.metadata.table,
- };
- } else if (key === "drop") {
- markEditStatus(treeNode.db, treeNode.metadata, "dropped");
- } else if (key === "restore") {
- removeEditStatus(treeNode.db, treeNode.metadata, /* recursive */ false);
- }
- }
- contextMenu.showDropdown = false;
-};
+// Handle context menu actions
+useEmitteryEventListener(contextMenuEvents, "show", ({ node }) => {
+ selectedKeysRef.value = [node.key];
+});
+useEmitteryEventListener(contextMenuEvents, "hide", () => {
+ selectedKeysRef.value = [];
+});
-const handleDropdownClickoutside = (e: MouseEvent) => {
- if (
- !isDescendantOf(e.target as Element, ".n-tree-node-wrapper") ||
- e.button !== 2
- ) {
- selectedKeysRef.value = [];
- contextMenu.showDropdown = false;
- }
-};
+useEmitteryEventListener(contextMenuEvents, "create-schema", (node) => {
+ state.schemaNameModalContext = {
+ db: node.db,
+ database: node.metadata.database,
+ schema: undefined,
+ };
+});
+useEmitteryEventListener(contextMenuEvents, "drop-schema", (node) => {
+ markEditStatus(node.db, node.metadata, "dropped");
+});
+useEmitteryEventListener(contextMenuEvents, "restore-schema", (node) => {
+ removeEditStatus(node.db, node.metadata, /* recursive */ false);
+});
+
+useEmitteryEventListener(contextMenuEvents, "create-table", (node) => {
+ state.tableNameModalContext = {
+ db: node.db,
+ database: node.metadata.database,
+ schema: node.metadata.schema,
+ table: undefined,
+ };
+});
+useEmitteryEventListener(contextMenuEvents, "drop-table", (node) => {
+ markEditStatus(node.db, node.metadata, "dropped");
+});
+useEmitteryEventListener(contextMenuEvents, "restore-table", (node) => {
+ removeEditStatus(node.db, node.metadata, /* recursive */ false);
+});
+useEmitteryEventListener(contextMenuEvents, "rename-table", (node) => {
+ state.tableNameModalContext = {
+ db: node.db,
+ database: node.metadata.database,
+ schema: node.metadata.schema,
+ table: node.metadata.table,
+ };
+});
+
+useEmitteryEventListener(contextMenuEvents, "create-procedure", (node) => {
+ state.procedureNameModalContext = {
+ db: node.db,
+ database: node.metadata.database,
+ schema: node.metadata.schema,
+ procedure: undefined,
+ };
+ expandNodeRecursively(node);
+});
+useEmitteryEventListener(contextMenuEvents, "drop-procedure", (node) => {
+ markEditStatus(node.db, node.metadata, "dropped");
+});
+useEmitteryEventListener(contextMenuEvents, "restore-procedure", (node) => {
+ removeEditStatus(node.db, node.metadata, /* recursive */ false);
+});
+
+useEmitteryEventListener(contextMenuEvents, "create-function", (node) => {
+ state.functionNameModalContext = {
+ db: node.db,
+ database: node.metadata.database,
+ schema: node.metadata.schema,
+ function: undefined,
+ };
+ expandNodeRecursively(node);
+});
+useEmitteryEventListener(contextMenuEvents, "drop-function", (node) => {
+ markEditStatus(node.db, node.metadata, "dropped");
+});
+useEmitteryEventListener(contextMenuEvents, "restore-function", (node) => {
+ removeEditStatus(node.db, node.metadata, /* recursive */ false);
+});
+
+// const handleContextMenuSelect = async (key: string) => {
+// const treeNode = contextMenu.treeNode;
+// if (!treeNode) return;
+// if (treeNode.type === "database") {
+// const engine = treeNode.db.instanceEntity.engine;
+// if (key === "create-schema") {
+// if (engineSupportsMultiSchema(engine)) {
+// state.schemaNameModalContext = {
+// db: treeNode.db,
+// database: treeNode.metadata.database,
+// schema: undefined,
+// };
+// }
+// }
+// } else if (treeNode.type === "schema") {
+// if (key === "drop-schema") {
+// markEditStatus(treeNode.db, treeNode.metadata, "dropped");
+// } else if (key === "restore") {
+// removeEditStatus(treeNode.db, treeNode.metadata, /* recursive */ false);
+// }
+// } else if (treeNode.type === "group") {
+// if (treeNode.group === "table" && key === "create-table") {
+// state.tableNameModalContext = {
+// db: treeNode.db,
+// database: treeNode.metadata.database,
+// schema: treeNode.metadata.schema,
+// table: undefined,
+// };
+// }
+// } else if (treeNode.type === "table") {
+// if (key === "rename") {
+// state.tableNameModalContext = {
+// db: treeNode.db,
+// database: treeNode.metadata.database,
+// schema: treeNode.metadata.schema,
+// table: treeNode.metadata.table,
+// };
+// } else if (key === "drop") {
+// markEditStatus(treeNode.db, treeNode.metadata, "dropped");
+// } else if (key === "restore") {
+// removeEditStatus(treeNode.db, treeNode.metadata, /* recursive */ false);
+// }
+// }
+// contextMenu.showDropdown = false;
+// };
const handleExpandedKeysChange = (expandedKeys: string[]) => {
expandedKeysRef.value = expandedKeys;
diff --git a/frontend/src/components/SchemaEditorLite/Aside/common.ts b/frontend/src/components/SchemaEditorLite/Aside/common.ts
index 6bbb3775a9..a6f37934df 100644
--- a/frontend/src/components/SchemaEditorLite/Aside/common.ts
+++ b/frontend/src/components/SchemaEditorLite/Aside/common.ts
@@ -3,11 +3,18 @@ import type { ComposedDatabase, ComposedInstance } from "@/types";
import type {
ColumnMetadata,
DatabaseMetadata,
+ FunctionMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
+ ViewMetadata,
} from "@/types/proto/v1/database_service";
import { groupBy } from "@/utils";
import { keyForResource } from "../context/common";
+import {
+ engineSupportsEditFunctions,
+ engineSupportsEditProcedures,
+} from "../spec";
import type { EditTarget } from "../types";
export interface BaseTreeNode extends TreeOption {
@@ -30,7 +37,9 @@ export interface TreeNodeForDatabase extends BaseTreeNode {
metadata: {
database: DatabaseMetadata;
};
- children: TreeNodeForSchema[];
+ children:
+ | TreeNodeForSchema[] // for multi-schema engines
+ | TreeNodeForGroup[]; // for single-schema engines
}
export interface TreeNodeForSchema extends BaseTreeNode {
@@ -41,19 +50,19 @@ export interface TreeNodeForSchema extends BaseTreeNode {
database: DatabaseMetadata;
schema: SchemaMetadata;
};
- children: TreeNodeForTable[];
+ children: TreeNodeForGroup[];
}
export interface TreeNodeForTable extends BaseTreeNode {
type: "table";
db: ComposedDatabase;
- parent: TreeNodeForSchema;
+ parent: TreeNodeForGroup<"table">;
metadata: {
database: DatabaseMetadata;
schema: SchemaMetadata;
table: TableMetadata;
};
- children: TreeNodeForColumn[];
+ children: TreeNodeForColumn[] | TreeNodeForPlaceholder<"column">[];
}
export interface TreeNodeForColumn extends BaseTreeNode {
@@ -70,16 +79,100 @@ export interface TreeNodeForColumn extends BaseTreeNode {
isLeaf: true;
}
+export interface TreeNodeForView extends BaseTreeNode {
+ type: "view";
+ db: ComposedDatabase;
+ parent: TreeNodeForGroup<"view">;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ view: ViewMetadata;
+ };
+ children: undefined;
+ isLeaf: true;
+}
+
+export interface TreeNodeForProcedure extends BaseTreeNode {
+ type: "procedure";
+ db: ComposedDatabase;
+ parent: TreeNodeForGroup<"procedure">;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure: ProcedureMetadata;
+ };
+ children: undefined;
+ isLeaf: true;
+}
+
+export interface TreeNodeForFunction extends BaseTreeNode {
+ type: "function";
+ db: ComposedDatabase;
+ parent: TreeNodeForGroup<"function">;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ function: FunctionMetadata;
+ };
+ children: undefined;
+ isLeaf: true;
+}
+
+export type GroupNodeType = "table" | "view" | "procedure" | "function";
+export interface TreeNodeForGroup<T extends GroupNodeType = GroupNodeType>
+ extends BaseTreeNode {
+ type: "group";
+ group: T;
+ db: ComposedDatabase;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ };
+ parent: TreeNodeForSchema | TreeNodeForDatabase | undefined;
+ isLeaf: false;
+ children: T extends "table"
+ ? TreeNodeForTable[] | TreeNodeForPlaceholder<"table">[]
+ : T extends "view"
+ ? TreeNodeForView[] | TreeNodeForPlaceholder<"view">[]
+ : T extends "procedure"
+ ? TreeNodeForProcedure[] | TreeNodeForPlaceholder<"procedure">[]
+ : T extends "function"
+ ? TreeNodeForFunction[] | TreeNodeForPlaceholder<"function">[]
+ : undefined;
+}
+
+export type PlaceholderNodeType =
+ | "table"
+ | "column"
+ | "view"
+ | "procedure"
+ | "function";
+export interface TreeNodeForPlaceholder<
+ T extends PlaceholderNodeType = PlaceholderNodeType,
+> extends BaseTreeNode {
+ type: "placeholder";
+ placeholder: T;
+ parent: TreeNode;
+ isLeaf: true;
+ children: undefined;
+}
+
export type TreeNode =
| TreeNodeForInstance
| TreeNodeForDatabase
| TreeNodeForSchema
| TreeNodeForTable
- | TreeNodeForColumn;
+ | TreeNodeForColumn
+ | TreeNodeForView
+ | TreeNodeForProcedure
+ | TreeNodeForFunction
+ | TreeNodeForGroup
+ | TreeNodeForPlaceholder;
export type BuildTreeOptions = {
byInstance: boolean;
};
+
export const buildTree = (
targets: EditTarget[],
map: Map<string, TreeNode>,
@@ -161,6 +254,100 @@ const buildSchemaNodeList = (
database: DatabaseMetadata,
parent: TreeNodeForDatabase | undefined
) => {
+ const mapSchemaChildrenNodes = (
+ schema: SchemaMetadata,
+ parent: TreeNodeForSchema | TreeNodeForDatabase | undefined
+ ) => {
+ const groups: TreeNodeForGroup[] = [];
+ const metadata = {
+ database,
+ schema,
+ };
+ // Tables
+ const tableGroupNode: TreeNodeForGroup<"table"> = {
+ type: "group",
+ group: "table",
+ key: keyForResource(db, metadata, "table-group"),
+ parent,
+ db,
+ metadata,
+ label: "Tables",
+ children: [],
+ isLeaf: false,
+ };
+ tableGroupNode.children = buildTableNodeList(
+ schema.tables,
+ map,
+ tableGroupNode
+ );
+ if (tableGroupNode.children.length === 0) {
+ tableGroupNode.children = [
+ buildPlaceholderNode("table", tableGroupNode, map),
+ ];
+ }
+ groups.push(tableGroupNode);
+ map.set(tableGroupNode.key, tableGroupNode);
+
+ // Procedures
+ if (engineSupportsEditProcedures(db.instanceEntity.engine)) {
+ const procedureGroupNode: TreeNodeForGroup<"procedure"> = {
+ type: "group",
+ group: "procedure",
+ key: keyForResource(db, metadata, "procedure-group"),
+ parent,
+ db,
+ metadata,
+ label: "Procedures",
+ children: [],
+ isLeaf: false,
+ };
+ procedureGroupNode.children = buildProcedureNodeList(
+ schema.procedures,
+ map,
+ procedureGroupNode
+ );
+ if (procedureGroupNode.children.length === 0) {
+ procedureGroupNode.children = [
+ buildPlaceholderNode("procedure", procedureGroupNode, map),
+ ];
+ }
+ groups.push(procedureGroupNode);
+ map.set(procedureGroupNode.key, procedureGroupNode);
+ }
+ // Functions
+ if (engineSupportsEditFunctions(db.instanceEntity.engine)) {
+ const functionGroupNode: TreeNodeForGroup<"function"> = {
+ type: "group",
+ group: "function",
+ key: keyForResource(db, metadata, "function-group"),
+ parent,
+ db,
+ metadata,
+ label: "functions",
+ children: [],
+ isLeaf: false,
+ };
+ functionGroupNode.children = buildFunctionNodeList(
+ schema.functions,
+ map,
+ functionGroupNode
+ );
+ if (functionGroupNode.children.length === 0) {
+ functionGroupNode.children = [
+ buildPlaceholderNode("function", functionGroupNode, map),
+ ];
+ }
+ groups.push(functionGroupNode);
+ map.set(functionGroupNode.key, functionGroupNode);
+ }
+ return groups;
+ };
+
+ // MySQL, TiDB has only one "schema" with empty name
+ if (schemas.length === 1 && schemas[0].name === "") {
+ return mapSchemaChildrenNodes(schemas[0], parent);
+ }
+
return schemas.map((schema) => {
const metadata = {
database,
@@ -176,8 +363,8 @@ const buildSchemaNodeList = (
metadata,
children: [],
};
+ schemaNode.children = mapSchemaChildrenNodes(schema, schemaNode);
map.set(schemaNode.key, schemaNode);
- schemaNode.children = buildTableNodeList(schema.tables, map, schemaNode);
return schemaNode;
});
};
@@ -185,7 +372,7 @@ const buildSchemaNodeList = (
const buildTableNodeList = (
tables: TableMetadata[],
map: Map<string, TreeNode>,
- parent: TreeNodeForSchema
+ parent: TreeNodeForGroup<"table">
) => {
return tables.map((table) => {
const { db } = parent;
@@ -205,10 +392,66 @@ const buildTableNodeList = (
};
map.set(tableNode.key, tableNode);
tableNode.children = buildColumnNodeList(table.columns, map, tableNode);
+
+ if (tableNode.children.length === 0) {
+ tableNode.children = [buildPlaceholderNode("column", tableNode, map)];
+ }
return tableNode;
});
};
+const buildProcedureNodeList = (
+ procedures: ProcedureMetadata[],
+ map: Map<string, TreeNode>,
+ parent: TreeNodeForGroup<"procedure">
+) => {
+ return procedures.map((procedure) => {
+ const { db } = parent;
+ const metadata = {
+ ...parent.metadata,
+ procedure,
+ };
+ const procedureNode: TreeNodeForProcedure = {
+ type: "procedure",
+ parent,
+ key: keyForResource(db, metadata),
+ label: procedure.name,
+ isLeaf: true,
+ db,
+ metadata,
+ children: undefined,
+ };
+ map.set(procedureNode.key, procedureNode);
+ return procedureNode;
+ });
+};
+
+const buildFunctionNodeList = (
+ functions: FunctionMetadata[],
+ map: Map<string, TreeNode>,
+ parent: TreeNodeForGroup<"function">
+) => {
+ return functions.map((func) => {
+ const { db } = parent;
+ const metadata = {
+ ...parent.metadata,
+ function: func,
+ };
+ const functionNode: TreeNodeForFunction = {
+ type: "function",
+ parent,
+ key: keyForResource(db, metadata),
+ label: func.name,
+ isLeaf: true,
+ db,
+ metadata,
+ children: undefined,
+ };
+ map.set(functionNode.key, functionNode);
+ return functionNode;
+ });
+};
+
const buildColumnNodeList = (
columns: ColumnMetadata[],
map: Map<string, TreeNode>,
@@ -234,3 +477,21 @@ const buildColumnNodeList = (
return columnNode;
});
};
+
+const buildPlaceholderNode = <T extends PlaceholderNodeType>(
+ placeholder: T,
+ parent: TreeNode,
+ map: Map<string, TreeNode>
+) => {
+ const node: TreeNodeForPlaceholder<T> = {
+ type: "placeholder",
+ key: `${parent.key}/placeholder`,
+ placeholder,
+ parent,
+ label: "",
+ children: undefined,
+ isLeaf: true,
+ };
+ map.set(node.key, node);
+ return node;
+};
diff --git a/frontend/src/components/SchemaEditorLite/Aside/context-menu.ts b/frontend/src/components/SchemaEditorLite/Aside/context-menu.ts
new file mode 100644
index 0000000000..18a6869f98
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/context-menu.ts
@@ -0,0 +1,218 @@
+import Emittery from "emittery";
+import type { DropdownOption } from "naive-ui";
+import { computed, reactive } from "vue";
+import { useI18n } from "vue-i18n";
+import type { ComposedDatabase } from "@/types";
+import { Engine } from "@/types/proto/v1/common";
+import { isDescendantOf } from "@/utils";
+import { useSchemaEditorContext } from "../context";
+import { engineSupportsMultiSchema } from "../spec";
+import type {
+ TreeNode,
+ TreeNodeForDatabase,
+ TreeNodeForGroup,
+ TreeNodeForProcedure,
+ TreeNodeForFunction,
+ TreeNodeForSchema,
+ TreeNodeForTable,
+} from "./common";
+
+interface TreeContextMenu {
+ show: boolean;
+ clientX: number;
+ clientY: number;
+ node?: TreeNode;
+}
+
+type TreeContextMenuEvents = Emittery<{
+ "create-schema": TreeNodeForDatabase;
+ "restore-schema": TreeNodeForSchema;
+ "drop-schema": TreeNodeForSchema;
+ "create-table": TreeNodeForGroup<"table">;
+ "rename-table": TreeNodeForTable;
+ "restore-table": TreeNodeForTable;
+ "drop-table": TreeNodeForTable;
+ "create-procedure": TreeNodeForGroup<"procedure">;
+ "restore-procedure": TreeNodeForProcedure;
+ "drop-procedure": TreeNodeForProcedure;
+ "create-function": TreeNodeForGroup<"function">;
+ "restore-function": TreeNodeForFunction;
+ "drop-function": TreeNodeForFunction;
+ show: {
+ e: MouseEvent;
+ node: TreeNode;
+ };
+ hide: undefined;
+}>;
+
+export const useContextMenu = () => {
+ const menu = reactive<TreeContextMenu>({
+ show: false,
+ clientX: 0,
+ clientY: 0,
+ node: undefined,
+ });
+ const events: TreeContextMenuEvents = new Emittery();
+ const { t } = useI18n();
+ const {
+ getSchemaStatus,
+ getTableStatus,
+ getProcedureStatus,
+ getFunctionStatus,
+ } = useSchemaEditorContext();
+
+ const options = computed((): DropdownOption[] => {
+ const { node } = menu;
+ if (!node) return [];
+ if (typeof node.db === "undefined") return [];
+
+ const { engine } = (node.db as ComposedDatabase).instanceEntity;
+ if (node.type === "database") {
+ if (engineSupportsMultiSchema(engine)) {
+ return [
+ {
+ key: "create-schema",
+ label: t("schema-editor.actions.create-schema"),
+ },
+ ];
+ }
+ return [];
+ }
+ if (node.type === "schema") {
+ const options: DropdownOption[] = [];
+ if (engine === Engine.POSTGRES) {
+ const status = getSchemaStatus(node.db, node.metadata);
+ if (status === "dropped") {
+ options.push({
+ key: "restore-schema",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ options.push({
+ key: "drop-schema",
+ label: t("schema-editor.actions.drop-schema"),
+ });
+ }
+ }
+ return options;
+ }
+ if (node.type === "group") {
+ if (node.group === "table") {
+ return [
+ {
+ key: "create-table",
+ label: t("schema-editor.actions.create-table"),
+ },
+ ];
+ }
+ if (node.group === "procedure") {
+ return [
+ {
+ key: "create-procedure",
+ label: t("schema-editor.actions.create-procedure"),
+ },
+ ];
+ }
+ if (node.group === "function") {
+ return [
+ {
+ key: "create-function",
+ label: t("schema-editor.actions.create-function"),
+ },
+ ];
+ }
+ }
+ if (node.type === "table") {
+ const options: DropdownOption[] = [];
+ const status = getTableStatus(node.db, node.metadata);
+ if (status === "dropped") {
+ options.push({
+ key: "restore-table",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ if (status !== "normal") {
+ options.push({
+ key: "rename-table",
+ label: t("schema-editor.actions.rename"),
+ });
+ }
+ options.push({
+ key: "drop-table",
+ label: t("schema-editor.actions.drop-table"),
+ });
+ }
+ return options;
+ }
+ if (node.type === "procedure") {
+ const options: DropdownOption[] = [];
+ const status = getProcedureStatus(node.db, node.metadata);
+ if (status === "dropped") {
+ options.push({
+ key: "restore-procedure",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ options.push({
+ key: "drop-procedure",
+ label: t("schema-editor.actions.drop"),
+ });
+ }
+ return options;
+ }
+ if (node.type === "function") {
+ const options: DropdownOption[] = [];
+ const status = getFunctionStatus(node.db, node.metadata);
+ if (status === "dropped") {
+ options.push({
+ key: "restore-function",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ options.push({
+ key: "drop-function",
+ label: t("schema-editor.actions.drop"),
+ });
+ }
+ return options;
+ }
+
+ return [];
+ });
+
+ const handleShow = (e: MouseEvent, node: TreeNode) => {
+ e.preventDefault();
+ e.stopPropagation();
+ menu.node = node;
+ menu.show = true;
+ menu.clientX = e.clientX;
+ menu.clientY = e.clientY;
+ events.emit("show", { e, node });
+ };
+
+ const handleSelect = async (key: string) => {
+ const node = menu.node;
+ if (!node) return;
+ events.emit(key as any, node as any);
+ menu.show = false;
+ };
+
+ const handleClickOutside = (e: MouseEvent) => {
+ if (
+ !isDescendantOf(e.target as Element, ".n-tree-node-wrapper") ||
+ e.button !== 2
+ ) {
+ events.emit("hide");
+ menu.show = false;
+ }
+ };
+
+ return {
+ menu: computed(() => menu),
+ options,
+ events,
+ handleShow,
+ handleSelect,
+ handleClickOutside,
+ };
+};
diff --git a/frontend/src/components/SchemaEditorLite/Editor.vue b/frontend/src/components/SchemaEditorLite/Editor.vue
index 3b653abf79..7a1dd3c04a 100644
--- a/frontend/src/components/SchemaEditorLite/Editor.vue
+++ b/frontend/src/components/SchemaEditorLite/Editor.vue
@@ -26,6 +26,22 @@
:table="currentTab.metadata.table"
:search-pattern="state.columnSearchPattern"
/>
+ <ProcedureEditor
+ v-if="currentTab.type === 'procedure'"
+ :key="currentTab.id"
+ :db="currentTab.database"
+ :database="currentTab.metadata.database"
+ :schema="currentTab.metadata.schema"
+ :procedure="currentTab.metadata.procedure"
+ />
+ <FunctionEditor
+ v-if="currentTab.type === 'function'"
+ :key="currentTab.id"
+ :db="currentTab.database"
+ :database="currentTab.metadata.database"
+ :schema="currentTab.metadata.schema"
+ :func="currentTab.metadata.function"
+ />
</div>
</template>
<EmptyTips v-else />
@@ -35,6 +51,8 @@
<script lang="ts" setup>
import { reactive, watch } from "vue";
import DatabaseEditor from "./Panels/DatabaseEditor.vue";
+import FunctionEditor from "./Panels/FunctionEditor.vue";
+import ProcedureEditor from "./Panels/ProcedureEditor.vue";
import TableEditor from "./Panels/TableEditor.vue";
import TabsContainer from "./TabsContainer.vue";
import { useSchemaEditorContext } from "./context";
diff --git a/frontend/src/components/SchemaEditorLite/Modals/FunctionNameModal.vue b/frontend/src/components/SchemaEditorLite/Modals/FunctionNameModal.vue
new file mode 100644
index 0000000000..4de8eb1402
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Modals/FunctionNameModal.vue
@@ -0,0 +1,149 @@
+<template>
+ <BBModal
+ :title="
+ mode === 'create'
+ ? $t('schema-editor.actions.create-function')
+ : $t('schema-editor.actions.rename')
+ "
+ class="shadow-inner outline outline-gray-200"
+ @close="dismissModal"
+ >
+ <div class="w-72">
+ <p>{{ $t("common.name") }}</p>
+ <NInput
+ ref="inputRef"
+ v-model:value="state.functionName"
+ class="my-2"
+ :autofocus="true"
+ />
+ </div>
+ <div class="w-full flex items-center justify-end mt-2 space-x-3">
+ <NButton @click="dismissModal">
+ {{ $t("common.cancel") }}
+ </NButton>
+ <NButton type="primary" @click="handleConfirmButtonClick">
+ {{ mode === "create" ? $t("common.create") : $t("common.save") }}
+ </NButton>
+ </div>
+ </BBModal>
+</template>
+
+<script lang="ts" setup>
+import type { InputInst } from "naive-ui";
+import { NButton, NInput } from "naive-ui";
+import { computed, onMounted, reactive, ref } from "vue";
+import { useI18n } from "vue-i18n";
+import { useNotificationStore } from "@/store";
+import type { ComposedDatabase } from "@/types";
+import type {
+ DatabaseMetadata,
+ SchemaMetadata,
+} from "@/types/proto/v1/database_service";
+import { FunctionMetadata } from "@/types/proto/v1/database_service";
+import { useSchemaEditorContext } from "../context";
+
+// Function name must start with a non-space character, end with a non-space character.
+const functionNameFieldRegexp = /^\S\S*\S?$/;
+
+interface LocalState {
+ functionName: string;
+}
+
+const props = defineProps<{
+ database: ComposedDatabase;
+ metadata: DatabaseMetadata;
+ schema: SchemaMetadata;
+ func?: FunctionMetadata;
+}>();
+
+const emit = defineEmits<{
+ (event: "close"): void;
+}>();
+
+const { t } = useI18n();
+const { events, addTab, markEditStatus } = useSchemaEditorContext();
+const inputRef = ref<InputInst>();
+const notificationStore = useNotificationStore();
+const mode = computed(() => {
+ return props.func ? "edit" : "create";
+});
+const state = reactive<LocalState>({
+ functionName: props.func?.name ?? "",
+});
+
+const handleConfirmButtonClick = async () => {
+ if (!functionNameFieldRegexp.test(state.functionName)) {
+ notificationStore.pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("schema-editor.message.invalid-function-name"),
+ });
+ return;
+ }
+ const { schema } = props;
+ const existed = schema.functions.find(
+ (func) => func.name === state.functionName
+ );
+ if (existed) {
+ notificationStore.pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("schema-editor.message.duplicated-function-name"),
+ });
+ return;
+ }
+
+ if (!props.func) {
+ const func = FunctionMetadata.fromPartial({
+ name: state.functionName,
+ definition: [
+ "CREATE FUNCTION `" +
+ state.functionName +
+ "` ( ... ) RETURNS ... DETERMINISTIC",
+ "BEGIN",
+ " ...",
+ "END",
+ ].join("\n"),
+ });
+ schema.functions.push(func);
+ markEditStatus(
+ props.database,
+ {
+ database: props.metadata,
+ schema,
+ function: func,
+ },
+ "created"
+ );
+
+ addTab({
+ type: "function",
+ database: props.database,
+ metadata: {
+ database: props.metadata,
+ schema: props.schema,
+ function: func,
+ },
+ });
+
+ events.emit("rebuild-tree", {
+ openFirstChild: false,
+ });
+ } else {
+ const { func } = props;
+ func.name = state.functionName;
+ events.emit("rebuild-edit-status", {
+ resets: ["tree"],
+ });
+ }
+ dismissModal();
+};
+
+const dismissModal = () => {
+ emit("close");
+};
+
+onMounted(() => {
+ inputRef.value?.focus();
+});
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Modals/ProcedureNameModal.vue b/frontend/src/components/SchemaEditorLite/Modals/ProcedureNameModal.vue
new file mode 100644
index 0000000000..97868c7d76
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Modals/ProcedureNameModal.vue
@@ -0,0 +1,147 @@
+<template>
+ <BBModal
+ :title="
+ mode === 'create'
+ ? $t('schema-editor.actions.create-procedure')
+ : $t('schema-editor.actions.rename')
+ "
+ class="shadow-inner outline outline-gray-200"
+ @close="dismissModal"
+ >
+ <div class="w-72">
+ <p>{{ $t("common.name") }}</p>
+ <NInput
+ ref="inputRef"
+ v-model:value="state.procedureName"
+ class="my-2"
+ :autofocus="true"
+ />
+ </div>
+ <div class="w-full flex items-center justify-end mt-2 space-x-3">
+ <NButton @click="dismissModal">
+ {{ $t("common.cancel") }}
+ </NButton>
+ <NButton type="primary" @click="handleConfirmButtonClick">
+ {{ mode === "create" ? $t("common.create") : $t("common.save") }}
+ </NButton>
+ </div>
+ </BBModal>
+</template>
+
+<script lang="ts" setup>
+import type { InputInst } from "naive-ui";
+import { NButton, NInput } from "naive-ui";
+import { computed, onMounted, reactive, ref } from "vue";
+import { useI18n } from "vue-i18n";
+import { useNotificationStore } from "@/store";
+import type { ComposedDatabase } from "@/types";
+import type {
+ DatabaseMetadata,
+ SchemaMetadata,
+} from "@/types/proto/v1/database_service";
+import { ProcedureMetadata } from "@/types/proto/v1/database_service";
+import { useSchemaEditorContext } from "../context";
+
+// Procedure name must start with a non-space character, end with a non-space character.
+const procedureNameFieldRegexp = /^\S\S*\S?$/;
+
+interface LocalState {
+ procedureName: string;
+}
+
+const props = defineProps<{
+ database: ComposedDatabase;
+ metadata: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure?: ProcedureMetadata;
+}>();
+
+const emit = defineEmits<{
+ (event: "close"): void;
+}>();
+
+const { t } = useI18n();
+const { events, addTab, markEditStatus } = useSchemaEditorContext();
+const inputRef = ref<InputInst>();
+const notificationStore = useNotificationStore();
+const mode = computed(() => {
+ return props.procedure ? "edit" : "create";
+});
+const state = reactive<LocalState>({
+ procedureName: props.procedure?.name ?? "",
+});
+
+const handleConfirmButtonClick = async () => {
+ if (!procedureNameFieldRegexp.test(state.procedureName)) {
+ notificationStore.pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("schema-editor.message.invalid-procedure-name"),
+ });
+ return;
+ }
+ const { schema } = props;
+ const existed = schema.procedures.find(
+ (procedure) => procedure.name === state.procedureName
+ );
+ if (existed) {
+ notificationStore.pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("schema-editor.message.duplicated-procedure-name"),
+ });
+ return;
+ }
+
+ if (!props.procedure) {
+ const procedure = ProcedureMetadata.fromPartial({
+ name: state.procedureName,
+ definition: [
+ "CREATE PROCEDURE `" + state.procedureName + "` ( ... )",
+ "BEGIN",
+ " ...",
+ "END",
+ ].join("\n"),
+ });
+ schema.procedures.push(procedure);
+ markEditStatus(
+ props.database,
+ {
+ database: props.metadata,
+ schema,
+ procedure,
+ },
+ "created"
+ );
+
+ addTab({
+ type: "procedure",
+ database: props.database,
+ metadata: {
+ database: props.metadata,
+ schema: props.schema,
+ procedure,
+ },
+ });
+
+ events.emit("rebuild-tree", {
+ openFirstChild: false,
+ });
+ } else {
+ const { procedure } = props;
+ procedure.name = state.procedureName;
+ events.emit("rebuild-edit-status", {
+ resets: ["tree"],
+ });
+ }
+ dismissModal();
+};
+
+const dismissModal = () => {
+ emit("close");
+};
+
+onMounted(() => {
+ inputRef.value?.focus();
+});
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/CommonCodeEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/CommonCodeEditor.vue
new file mode 100644
index 0000000000..5669430291
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Panels/CommonCodeEditor.vue
@@ -0,0 +1,117 @@
+<template>
+ <div class="h-full flex flex-col gap-1 overflow-y-hidden pt-1">
+ <div class="flex justify-between items-center text-sm">
+ <div class="flex justify-start items-center gap-2">
+ <template
+ v-if="!readonly && (status === 'normal' || status === 'updated')"
+ >
+ <NButton
+ v-if="!state.unlocked"
+ size="small"
+ @click="state.unlocked = true"
+ >
+ <template #icon>
+ <SquarePenIcon class="w-3.5 h-3.5" />
+ </template>
+ {{ $t("common.edit") }}
+ </NButton>
+ <template v-else>
+ <NButton size="small" @click="cancelEdit">
+ <template #icon>
+ <XIcon class="w-4 h-4" />
+ </template>
+ {{ $t("common.cancel") }}
+ </NButton>
+ <NButton
+ size="small"
+ :disabled="props.code === state.code"
+ @click="confirmEdit"
+ >
+ <template #icon>
+ <CheckIcon class="w-4 h-4" />
+ </template>
+ {{ $t("common.confirm") }}
+ </NButton>
+ </template>
+ </template>
+ </div>
+ </div>
+ <MonacoEditor
+ :content="state.code"
+ :readonly="!editable"
+ :auto-complete-context="{
+ instance: db.instance,
+ database: db.name,
+ }"
+ class="border w-full rounded flex-1 relative"
+ @update:content="handleUpdateCode"
+ />
+ </div>
+</template>
+
+<script setup lang="ts">
+import { XIcon, CheckIcon, SquarePenIcon } from "lucide-vue-next";
+import { NButton } from "naive-ui";
+import { computed, reactive, watch } from "vue";
+import { MonacoEditor } from "@/components/MonacoEditor";
+import type { ComposedDatabase } from "@/types";
+import type { EditStatus } from "..";
+
+type LocalState = {
+ code: string;
+ unlocked: boolean;
+};
+
+const props = defineProps<{
+ db: ComposedDatabase;
+ code: string;
+ readonly: boolean;
+ status: EditStatus;
+}>();
+
+const emit = defineEmits<{
+ (e: "update:code", code: string): void;
+}>();
+
+const state = reactive<LocalState>({
+ code: props.code,
+ unlocked: false,
+});
+
+const editable = computed(() => {
+ if (props.readonly) {
+ return false;
+ }
+ if (props.status === "dropped") {
+ return false;
+ }
+ if (props.status === "created") {
+ return true;
+ }
+ return state.unlocked;
+});
+
+const cancelEdit = () => {
+ state.unlocked = false;
+ state.code = props.code;
+};
+
+const confirmEdit = () => {
+ state.unlocked = false;
+ emit("update:code", state.code);
+};
+
+const handleUpdateCode = (code: string) => {
+ state.code = code;
+ if (props.status === "created") {
+ emit("update:code", code);
+ }
+};
+
+watch(
+ () => props.code,
+ (code) => {
+ state.code = code;
+ }
+);
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/FunctionEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/FunctionEditor.vue
new file mode 100644
index 0000000000..5ebbe6a077
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Panels/FunctionEditor.vue
@@ -0,0 +1,73 @@
+<template>
+ <CommonCodeEditor
+ :db="db"
+ :code="func.definition"
+ :readonly="disallowChangeFunction"
+ :status="status"
+ @update:code="handleUpdateDefinition"
+ />
+</template>
+
+<script setup lang="ts">
+import { computed } from "vue";
+import type { ComposedDatabase } from "@/types";
+import type {
+ DatabaseMetadata,
+ FunctionMetadata,
+ SchemaMetadata,
+} from "@/types/proto/v1/database_service";
+import { useSchemaEditorContext } from "../context";
+import type { EditStatus } from "../types";
+import CommonCodeEditor from "./CommonCodeEditor.vue";
+
+const props = defineProps<{
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ func: FunctionMetadata;
+}>();
+
+const { readonly, markEditStatus, getSchemaStatus, getFunctionStatus } =
+ useSchemaEditorContext();
+
+const statusForSchema = () => {
+ return getSchemaStatus(props.db, {
+ database: props.database,
+ schema: props.schema,
+ });
+};
+const status = computed(() => {
+ return getFunctionStatus(props.db, {
+ database: props.database,
+ schema: props.schema,
+ function: props.func,
+ });
+});
+const markStatus = (status: EditStatus) => {
+ markEditStatus(
+ props.db,
+ {
+ database: props.database,
+ schema: props.schema,
+ function: props.func,
+ },
+ status
+ );
+};
+
+const disallowChangeFunction = computed(() => {
+ if (readonly.value) {
+ return true;
+ }
+ return statusForSchema() === "dropped" || status.value === "dropped";
+});
+
+const handleUpdateDefinition = (code: string) => {
+ // eslint-disable-next-line vue/no-mutating-props
+ props.func.definition = code;
+
+ if (status.value === "normal") {
+ markStatus("updated");
+ }
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/ProcedureEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/ProcedureEditor.vue
new file mode 100644
index 0000000000..982700dde6
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Panels/ProcedureEditor.vue
@@ -0,0 +1,73 @@
+<template>
+ <CommonCodeEditor
+ :db="db"
+ :code="procedure.definition"
+ :readonly="disallowChangeProcedure"
+ :status="status"
+ @update:code="handleUpdateDefinition"
+ />
+</template>
+
+<script setup lang="ts">
+import { computed } from "vue";
+import type { ComposedDatabase } from "@/types";
+import type {
+ DatabaseMetadata,
+ ProcedureMetadata,
+ SchemaMetadata,
+} from "@/types/proto/v1/database_service";
+import { useSchemaEditorContext } from "../context";
+import type { EditStatus } from "../types";
+import CommonCodeEditor from "./CommonCodeEditor.vue";
+
+const props = defineProps<{
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure: ProcedureMetadata;
+}>();
+
+const { readonly, markEditStatus, getSchemaStatus, getProcedureStatus } =
+ useSchemaEditorContext();
+
+const statusForSchema = () => {
+ return getSchemaStatus(props.db, {
+ database: props.database,
+ schema: props.schema,
+ });
+};
+const status = computed(() => {
+ return getProcedureStatus(props.db, {
+ database: props.database,
+ schema: props.schema,
+ procedure: props.procedure,
+ });
+});
+const markStatus = (status: EditStatus) => {
+ markEditStatus(
+ props.db,
+ {
+ database: props.database,
+ schema: props.schema,
+ procedure: props.procedure,
+ },
+ status
+ );
+};
+
+const disallowChangeProcedure = computed(() => {
+ if (readonly.value) {
+ return true;
+ }
+ return statusForSchema() === "dropped" || status.value === "dropped";
+});
+
+const handleUpdateDefinition = (code: string) => {
+ // eslint-disable-next-line vue/no-mutating-props
+ props.procedure.definition = code;
+
+ if (status.value === "normal") {
+ markStatus("updated");
+ }
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/ForeignKeyCell.vue b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/ForeignKeyCell.vue
index d6091b5bd9..34a53e1710 100644
--- a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/ForeignKeyCell.vue
+++ b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/ForeignKeyCell.vue
@@ -35,7 +35,6 @@
<script lang="ts" setup>
import { PenSquareIcon } from "lucide-vue-next";
import { computed } from "vue";
-import { engineHasSchema } from "@/components/SchemaEditorLite/engine-specs";
import { MiniActionButton } from "@/components/v2";
import type { ComposedDatabase } from "@/types";
import type {
@@ -45,6 +44,7 @@ import type {
SchemaMetadata,
TableMetadata,
} from "@/types/proto/v1/database_service";
+import { engineSupportsMultiSchema } from "../../../spec";
const props = defineProps<{
db: ComposedDatabase;
@@ -90,7 +90,7 @@ const referencedNameForFk = (fk: ForeignKeyMetadata) => {
(column) => column.name === fk.referencedColumns[position]
);
- if (engineHasSchema(props.db.instanceEntity.engine)) {
+ if (engineSupportsMultiSchema(props.db.instanceEntity.engine)) {
return `${referencedSchema.name}.${referencedTable.name}(${referencedColumn?.name})`;
} else {
return `${referencedTable.name}(${referencedColumn?.name})`;
diff --git a/frontend/src/components/SchemaEditorLite/TabsContainer.vue b/frontend/src/components/SchemaEditorLite/TabsContainer.vue
index 57f8e05c6d..7d20f44f06 100644
--- a/frontend/src/components/SchemaEditorLite/TabsContainer.vue
+++ b/frontend/src/components/SchemaEditorLite/TabsContainer.vue
@@ -28,6 +28,14 @@
v-if="tab.type === 'table'"
class="w-4 h-4 text-gray-400"
/>
+ <ProcedureIcon
+ v-if="tab.type === 'procedure'"
+ class="w-4 h-4 text-gray-400"
+ />
+ <FunctionIcon
+ v-if="tab.type === 'function'"
+ class="w-4 h-4 text-gray-400"
+ />
</span>
<NEllipsis
class="text-sm w-20 leading-4"
@@ -36,7 +44,7 @@
>
</div>
<span class="tab-close-button shrink-0 flex">
- <heroicons-outline:x
+ <XIcon
class="rounded w-4 h-auto text-gray-400 hover:text-gray-600"
@click.stop.prevent="handleCloseTab(tab)"
/>
@@ -60,11 +68,12 @@
</template>
<script lang="ts" setup>
+import { XIcon } from "lucide-vue-next";
import { NEllipsis, NInput } from "naive-ui";
import scrollIntoView from "scroll-into-view-if-needed";
import { computed, nextTick, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
-import { DatabaseIcon, TableIcon } from "../Icon";
+import { DatabaseIcon, FunctionIcon, ProcedureIcon, TableIcon } from "../Icon";
import { useSchemaEditorContext } from "./context";
import type { TabContext } from "./types";
@@ -148,7 +157,8 @@ const getTabName = (tab: TabContext) => {
}
const { database } = tab;
return database.databaseName;
- } else if (tab.type === "table") {
+ }
+ if (tab.type === "table") {
const {
metadata: { schema, table },
} = tab;
@@ -157,10 +167,29 @@ const getTabName = (tab: TabContext) => {
parts.unshift(schema.name);
}
return parts.join(".");
- } else {
- // Should never reach here.
- return "unknown tab";
}
+ if (tab.type === "procedure") {
+ const {
+ metadata: { schema, procedure },
+ } = tab;
+ const parts = [procedure.name];
+ if (schema.name) {
+ parts.unshift(schema.name);
+ }
+ return parts.join(".");
+ }
+ if (tab.type === "function") {
+ const {
+ metadata: { schema, function: func },
+ } = tab;
+ const parts = [func.name];
+ if (schema.name) {
+ parts.unshift(schema.name);
+ }
+ return parts.join(".");
+ }
+ // Should never reach here.
+ return "unknown tab";
};
const handleSelectTab = (tab: TabContext) => {
diff --git a/frontend/src/components/SchemaEditorLite/algorithm/apply-selected.ts b/frontend/src/components/SchemaEditorLite/algorithm/apply-selected.ts
index ed680cb8c5..b5e399c395 100644
--- a/frontend/src/components/SchemaEditorLite/algorithm/apply-selected.ts
+++ b/frontend/src/components/SchemaEditorLite/algorithm/apply-selected.ts
@@ -4,6 +4,8 @@ import type {
ColumnConfig,
ColumnMetadata,
DatabaseMetadata,
+ FunctionMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
} from "@/types/proto/v1/database_service";
@@ -19,7 +21,12 @@ import {
} from "./utils";
export const useApplySelectedMetadataEdit = (context: SchemaEditorContext) => {
- const { getTableStatus, getColumnStatus } = context;
+ const {
+ getTableStatus,
+ getColumnStatus,
+ getProcedureStatus,
+ getFunctionStatus,
+ } = context;
const applySelectedMetadataEdit = (
db: ComposedDatabase,
@@ -41,14 +48,36 @@ export const useApplySelectedMetadataEdit = (context: SchemaEditorContext) => {
});
})
);
+ const sourceProcedureMap = new Map(
+ source.schemas.flatMap((schema) => {
+ return schema.procedures.map((procedure) => {
+ const key = keyForResource(db, {
+ schema,
+ procedure,
+ });
+ return [key, { schema, procedure }];
+ });
+ })
+ );
+ const sourceFunctionMap = new Map(
+ source.schemas.flatMap((schema) => {
+ return schema.functions.map((func) => {
+ const key = keyForResource(db, {
+ schema,
+ function: func,
+ });
+ return [key, { schema, function: func }];
+ });
+ })
+ );
const sourceTableConfigMap = new Map(
source.schemaConfigs.flatMap((schemaConfig) => {
return schemaConfig.tableConfigs.map((tableConfig) => {
- const key = keyForResourceName(
- db.name,
- schemaConfig.name,
- tableConfig.name
- );
+ const key = keyForResourceName({
+ database: db.name,
+ schema: schemaConfig.name,
+ table: tableConfig.name,
+ });
return [key, { schemaConfig, tableConfig }];
});
})
@@ -69,12 +98,12 @@ export const useApplySelectedMetadataEdit = (context: SchemaEditorContext) => {
const pickedColumnConfigs: ColumnConfig[] = [];
for (let i = 0; i < table.columns.length; i++) {
const column = table.columns[i];
- const key = keyForResourceName(
- db.name,
- schema.name,
- table.name,
- column.name
- );
+ const key = keyForResourceName({
+ database: db.name,
+ schema: schema.name,
+ table: table.name,
+ column: column.name,
+ });
const picked = selectedObjectKeys.has(key);
if (picked) {
const status = getColumnStatus(db, {
@@ -171,7 +200,66 @@ export const useApplySelectedMetadataEdit = (context: SchemaEditorContext) => {
if (schemaConfig.tableConfigs.length > 0) {
schemaConfigs.push(schemaConfig);
}
+
+ const procedures: ProcedureMetadata[] = [];
+ for (let j = 0; j < schema.procedures.length; j++) {
+ const procedure = schema.procedures[j];
+ const key = keyForResource(db, { schema, procedure });
+ const picked = selectedObjectKeys.has(key);
+ if (picked) {
+ const status = getProcedureStatus(db, {
+ database: target,
+ schema,
+ procedure,
+ });
+ if (status === "dropped") {
+ // Drop procedure
+ // Don't collect the dropped procedure
+ continue;
+ } else {
+ // Collect the edited procedure
+ procedures.push(procedure);
+ }
+ } else {
+ const sourceProcedure = sourceProcedureMap.get(key);
+ if (sourceProcedure) {
+ // Collect the original procedure
+ procedures.push(cloneDeep(sourceProcedure.procedure));
+ }
+ }
+ }
+ schema.procedures = procedures;
+
+ const functions: FunctionMetadata[] = [];
+ for (let j = 0; j < schema.functions.length; j++) {
+ const func = schema.functions[j];
+ const key = keyForResource(db, { schema, function: func });
+ const picked = selectedObjectKeys.has(key);
+ if (picked) {
+ const status = getFunctionStatus(db, {
+ database: target,
+ schema,
+ function: func,
+ });
+ if (status === "dropped") {
+ // Drop function
+ // Don't collect the dropped function
+ continue;
+ } else {
+ // Collect the edited function
+ functions.push(func);
+ }
+ } else {
+ const sourceFunction = sourceFunctionMap.get(key);
+ if (sourceFunction) {
+ // Collect the original function
+ functions.push(cloneDeep(sourceFunction.function));
+ }
+ }
+ }
+ schema.functions = functions;
}
+
target.schemaConfigs = schemaConfigs;
cleanupUnusedConfigs(target);
diff --git a/frontend/src/components/SchemaEditorLite/algorithm/apply.ts b/frontend/src/components/SchemaEditorLite/algorithm/apply.ts
index 350a085d01..bc3e590b69 100644
--- a/frontend/src/components/SchemaEditorLite/algorithm/apply.ts
+++ b/frontend/src/components/SchemaEditorLite/algorithm/apply.ts
@@ -4,7 +4,13 @@ import type { SchemaEditorContext } from "../context";
import { cleanupUnusedConfigs } from "./utils";
export const useApplyMetadataEdit = (context: SchemaEditorContext) => {
- const { getSchemaStatus, getTableStatus, getColumnStatus } = context;
+ const {
+ getSchemaStatus,
+ getTableStatus,
+ getColumnStatus,
+ getProcedureStatus,
+ getFunctionStatus,
+ } = context;
const applyMetadataEdit = (
database: ComposedDatabase,
@@ -18,7 +24,7 @@ export const useApplyMetadataEdit = (context: SchemaEditorContext) => {
});
return status !== "dropped";
});
- // Drop tables
+ // Drop tables, procedures and functions
metadata.schemas.forEach((schema) => {
schema.tables = schema.tables.filter((table) => {
const status = getTableStatus(database, {
@@ -28,6 +34,22 @@ export const useApplyMetadataEdit = (context: SchemaEditorContext) => {
});
return status !== "dropped";
});
+ schema.procedures = schema.procedures.filter((procedure) => {
+ const status = getProcedureStatus(database, {
+ database: metadata,
+ schema,
+ procedure,
+ });
+ return status !== "dropped";
+ });
+ schema.functions = schema.functions.filter((func) => {
+ const status = getFunctionStatus(database, {
+ database: metadata,
+ schema,
+ function: func,
+ });
+ return status !== "dropped";
+ });
});
// Drop columns
metadata.schemas.forEach((schema) => {
diff --git a/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.ts b/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.ts
index cb85c79ec8..8805d3ef80 100644
--- a/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.ts
+++ b/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.ts
@@ -1,6 +1,10 @@
import { cloneDeep, isEqual, pick } from "lodash-es";
import type { ComposedDatabase } from "@/types";
-import type { TablePartitionMetadata } from "@/types/proto/v1/database_service";
+import type {
+ FunctionMetadata,
+ ProcedureMetadata,
+ TablePartitionMetadata,
+} from "@/types/proto/v1/database_service";
import {
type ColumnMetadata,
type DatabaseMetadata,
@@ -55,6 +59,10 @@ export class DiffMerge {
targetTableMap = new Map<string, TableMetadata>();
sourceColumnMap = new Map<string, ColumnMetadata>();
targetColumnMap = new Map<string, ColumnMetadata>();
+ sourceProcedureMap = new Map<string, ProcedureMetadata>();
+ targetProcedureMap = new Map<string, ProcedureMetadata>();
+ sourceFunctionMap = new Map<string, FunctionMetadata>();
+ targetFunctionMap = new Map<string, FunctionMetadata>();
sourceSchemaConfigMap = new Map<string, SchemaConfig>();
targetSchemaConfigMap = new Map<string, SchemaConfig>();
sourceTableConfigMap = new Map<string, TableConfig>();
@@ -68,6 +76,8 @@ export class DiffMerge {
| "mergeColumns"
| "mergeTablePartitions"
| "diffColumn"
+ | "mergeProcedures"
+ | "mergeFunctions"
| "mergeSchemaConfigs"
| "mergeTableConfigs"
| "mergeColumnConfigs"
@@ -120,7 +130,10 @@ export class DiffMerge {
const mergedSchemas: SchemaMetadata[] = [];
for (let i = 0; i < sourceSchemas.length; i++) {
const sourceSchema = sourceSchemas[i];
- const key = keyForResourceName(database.name, sourceSchema.name);
+ const key = keyForResourceName({
+ database: database.name,
+ schema: sourceSchema.name,
+ });
let targetSchema = targetSchemaMap.get(key);
if (targetSchema) {
// existed schema
@@ -136,6 +149,27 @@ export class DiffMerge {
schema: targetSchema,
}
);
+ this.mergeProcedures(
+ {
+ database: sourceMetadata,
+ schema: sourceSchema,
+ },
+ {
+ database: targetMetadata,
+ schema: targetSchema,
+ }
+ );
+ this.mergeFunctions(
+ {
+ database: sourceMetadata,
+ schema: sourceSchema,
+ },
+ {
+ database: targetMetadata,
+ schema: targetSchema,
+ }
+ );
+
continue;
}
// dropped schema
@@ -146,7 +180,10 @@ export class DiffMerge {
}
for (let i = 0; i < targetSchemas.length; i++) {
const targetSchema = targetSchemas[i];
- const key = keyForResourceName(database.name, targetSchema.name);
+ const key = keyForResourceName({
+ database: database.name,
+ schema: targetSchema.name,
+ });
const sourceSchema = sourceSchemaMap.get(key);
if (!sourceSchema) {
// newly created schema
@@ -171,11 +208,11 @@ export class DiffMerge {
const mergedTables: TableMetadata[] = [];
for (let i = 0; i < sourceTables.length; i++) {
const sourceTable = sourceTables[i];
- const key = keyForResourceName(
- database.name,
- sourceSchema.name,
- sourceTable.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: sourceSchema.name,
+ table: sourceTable.name,
+ });
let targetTable = targetTableMap.get(key);
if (targetTable) {
// existed table
@@ -215,11 +252,11 @@ export class DiffMerge {
}
for (let i = 0; i < targetTables.length; i++) {
const targetTable = targetTables[i];
- const key = keyForResourceName(
- database.name,
- targetSchema.name,
- targetTable.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: targetSchema.name,
+ table: targetTable.name,
+ });
const sourceTable = sourceTableMap.get(key);
if (!sourceTable) {
// newly created table
@@ -340,13 +377,12 @@ export class DiffMerge {
const mergedPartitions: TablePartitionMetadata[] = [];
for (let i = 0; i < sourcePartitions.length; i++) {
const sourcePartition = sourcePartitions[i];
- const key = keyForResourceName(
- database.name,
- sourceSchema.name,
- sourceTable.name,
- /* column */ undefined,
- sourcePartition.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: sourceSchema.name,
+ table: sourceTable.name,
+ partition: sourcePartition.name,
+ });
let targetPartition = targetPartitionMap.get(key);
if (targetPartition) {
// existed partition
@@ -365,13 +401,12 @@ export class DiffMerge {
}
for (let i = 0; i < targetPartitions.length; i++) {
const targetPartition = targetPartitions[i];
- const key = keyForResourceName(
- database.name,
- targetSchema.name,
- targetTable.name,
- /* column */ undefined,
- targetPartition.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: targetSchema.name,
+ table: targetTable.name,
+ partition: targetPartition.name,
+ });
const sourcePartition = sourcePartitionMap.get(key);
if (!sourcePartition) {
// newly created partition
@@ -418,12 +453,12 @@ export class DiffMerge {
const mergedColumns: ColumnMetadata[] = [];
for (let i = 0; i < sourceColumns.length; i++) {
const sourceColumn = sourceColumns[i];
- const key = keyForResourceName(
- database.name,
- sourceSchema.name,
- sourceTable.name,
- sourceColumn.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: sourceSchema.name,
+ table: sourceTable.name,
+ column: sourceColumn.name,
+ });
let targetColumn = targetColumnMap.get(key);
if (targetColumn) {
// existed column
@@ -442,12 +477,12 @@ export class DiffMerge {
}
for (let i = 0; i < targetColumns.length; i++) {
const targetColumn = targetColumns[i];
- const key = keyForResourceName(
- database.name,
- targetSchema.name,
- targetTable.name,
- targetColumn.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: targetSchema.name,
+ table: targetTable.name,
+ column: targetColumn.name,
+ });
const sourceColumn = sourceColumnMap.get(key);
if (!sourceColumn) {
// newly created column
@@ -474,16 +509,124 @@ export class DiffMerge {
pick(targetColumn, ComparableColumnFields)
)
) {
- const key = keyForResourceName(
- this.database.name,
- targetSchema.name,
- targetTable.name,
- targetColumn.name
- );
+ const key = keyForResourceName({
+ database: this.database.name,
+ schema: targetSchema.name,
+ table: targetTable.name,
+ column: targetColumn.name,
+ });
this.context.markEditStatusByKey(key, "updated");
}
this.timer.end("diffColumn", 1);
}
+ mergeProcedures(source: RichSchemaMetadata, target: RichSchemaMetadata) {
+ const { context, database, sourceProcedureMap, targetProcedureMap } = this;
+ const { schema: sourceSchema } = source;
+ const { schema: targetSchema } = target;
+ const sourceProcedures = sourceSchema.procedures;
+ const targetProcedures = targetSchema.procedures;
+ this.timer.begin("mergeProcedures");
+ mapProcedures(database, sourceSchema, sourceProcedures, sourceProcedureMap);
+ mapProcedures(database, targetSchema, targetProcedures, targetProcedureMap);
+
+ const mergedProcedures: ProcedureMetadata[] = [];
+ for (let i = 0; i < sourceProcedures.length; i++) {
+ const sourceProcedure = sourceProcedures[i];
+ const key = keyForResourceName({
+ database: database.name,
+ schema: sourceSchema.name,
+ procedure: sourceProcedure.name,
+ });
+ let targetProcedure = targetProcedureMap.get(key);
+ if (targetProcedure) {
+ // existed procedure
+ mergedProcedures.push(targetProcedure);
+ if (sourceProcedure.definition !== targetProcedure.definition) {
+ context.markEditStatusByKey(key, "updated");
+ }
+ continue;
+ }
+ // dropped procedure
+ // copy the source procedure to target and mark it as 'dropped'
+ targetProcedure = cloneDeep(sourceProcedure);
+ mergedProcedures.push(targetProcedure);
+ context.markEditStatusByKey(key, "dropped");
+ }
+ for (let i = 0; i < targetProcedures.length; i++) {
+ const targetProcedure = targetProcedures[i];
+ const key = keyForResourceName({
+ database: database.name,
+ schema: targetSchema.name,
+ procedure: targetProcedure.name,
+ });
+ const sourceProcedure = sourceProcedureMap.get(key);
+ if (!sourceProcedure) {
+ // newly created procedure
+ // mark it as 'created'
+ mergedProcedures.push(targetProcedure);
+ context.markEditStatusByKey(key, "created");
+ }
+ }
+ targetSchema.procedures = mergedProcedures;
+ this.timer.end(
+ "mergeProcedures",
+ sourceProcedures.length + targetProcedures.length
+ );
+ }
+ mergeFunctions(source: RichSchemaMetadata, target: RichSchemaMetadata) {
+ const { context, database, sourceFunctionMap, targetFunctionMap } = this;
+ const { schema: sourceSchema } = source;
+ const { schema: targetSchema } = target;
+ const sourceFunctions = sourceSchema.functions;
+ const targetFunctions = targetSchema.functions;
+ this.timer.begin("mergeFunctions");
+ mapFunctions(database, sourceSchema, sourceFunctions, sourceFunctionMap);
+ mapFunctions(database, targetSchema, targetFunctions, targetFunctionMap);
+
+ const mergedFunctions: FunctionMetadata[] = [];
+ for (let i = 0; i < sourceFunctions.length; i++) {
+ const sourceFunction = sourceFunctions[i];
+ const key = keyForResourceName({
+ database: database.name,
+ schema: sourceSchema.name,
+ function: sourceFunction.name,
+ });
+ let targetFunction = targetFunctionMap.get(key);
+ if (targetFunction) {
+ // existed function
+ mergedFunctions.push(targetFunction);
+ if (sourceFunction.definition !== targetFunction.definition) {
+ context.markEditStatusByKey(key, "updated");
+ }
+ continue;
+ }
+ // dropped function
+ // copy the source function to target and mark it as 'dropped'
+ targetFunction = cloneDeep(sourceFunction);
+ mergedFunctions.push(targetFunction);
+ context.markEditStatusByKey(key, "dropped");
+ }
+ for (let i = 0; i < targetFunctions.length; i++) {
+ const targetFunction = targetFunctions[i];
+ const key = keyForResourceName({
+ database: database.name,
+ schema: targetSchema.name,
+ function: targetFunction.name,
+ });
+ const sourceFunction = sourceFunctionMap.get(key);
+ if (!sourceFunction) {
+ // newly created function
+ // mark it as 'created'
+ mergedFunctions.push(targetFunction);
+ context.markEditStatusByKey(key, "created");
+ }
+ }
+ targetSchema.functions = mergedFunctions;
+ this.timer.end(
+ "mergeFunctions",
+ sourceFunctions.length + targetFunctions.length
+ );
+ }
mergeSchemaConfigs() {
const {
context,
@@ -501,7 +644,10 @@ export class DiffMerge {
const mergedSchemaConfigs: SchemaConfig[] = [];
for (let i = 0; i < targetMetadata.schemas.length; i++) {
const schema = targetMetadata.schemas[i];
- const key = keyForResourceName(database.name, schema.name);
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ });
const sourceSchemaConfig = sourceSchemaConfigMap.get(key);
const targetSchemaConfig = targetSchemaConfigMap.get(key);
const schemaStatus = context.getEditStatusByKey(key);
@@ -565,7 +711,11 @@ export class DiffMerge {
const mergedTableConfigs: TableConfig[] = [];
for (let i = 0; i < schema.tables.length; i++) {
const table = schema.tables[i];
- const key = keyForResourceName(database.name, schema.name, table.name);
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ table: table.name,
+ });
const sourceTableConfig = sourceTableConfigMap.get(key);
const targetTableConfig = targetTableConfigMap.get(key);
const tableStatus = context.getEditStatusByKey(key);
@@ -637,12 +787,12 @@ export class DiffMerge {
const mergedColumnConfigs: ColumnConfig[] = [];
for (let i = 0; i < table.columns.length; i++) {
const column = table.columns[i];
- const key = keyForResourceName(
- database.name,
- schema.name,
- table.name,
- column.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ table: table.name,
+ column: column.name,
+ });
const sourceColumnConfig = sourceColumnConfigMap.get(key);
const targetColumnConfig = targetColumnConfigMap.get(key);
const columnStatus = context.getEditStatusByKey(key);
@@ -699,12 +849,12 @@ export class DiffMerge {
this.timer.begin("diffColumnConfig");
if (!isEqual(sourceColumnConfig, targetColumnConfig)) {
- const key = keyForResourceName(
- this.database.name,
- targetSchemaConfig.name,
- targetTableConfig.name,
- targetColumnConfig.name
- );
+ const key = keyForResourceName({
+ database: this.database.name,
+ schema: targetSchemaConfig.name,
+ table: targetTableConfig.name,
+ column: targetColumnConfig.name,
+ });
const status = this.context.getEditStatusByKey(key);
if (status !== "created" && status !== "dropped") {
this.context.markEditStatusByKey(key, "updated");
@@ -720,7 +870,10 @@ const mapSchemas = (
map: Map<string, SchemaMetadata>
) => {
schemas.forEach((schema) => {
- const key = keyForResourceName(database.name, schema.name);
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ });
map.set(key, schema);
});
};
@@ -731,7 +884,11 @@ const mapTables = (
map: Map<string, TableMetadata>
) => {
tables.forEach((table) => {
- const key = keyForResourceName(database.name, schema.name, table.name);
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ table: table.name,
+ });
map.set(key, table);
});
};
@@ -743,12 +900,12 @@ const mapColumns = (
map: Map<string, ColumnMetadata>
) => {
columns.forEach((column) => {
- const key = keyForResourceName(
- database.name,
- schema.name,
- table.name,
- column.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ table: table.name,
+ column: column.name,
+ });
map.set(key, column);
});
};
@@ -760,23 +917,55 @@ const mapTablePartitions = (
map: Map<string, TablePartitionMetadata>
) => {
partitions.forEach((partition) => {
- const key = keyForResourceName(
- database.name,
- schema.name,
- table.name,
- /* column */ undefined,
- partition.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ table: table.name,
+ partition: partition.name,
+ });
map.set(key, partition);
});
};
+const mapProcedures = (
+ database: ComposedDatabase,
+ schema: SchemaMetadata,
+ procedures: ProcedureMetadata[],
+ map: Map<string, ProcedureMetadata>
+) => {
+ procedures.forEach((procedure) => {
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ procedure: procedure.name,
+ });
+ map.set(key, procedure);
+ });
+};
+const mapFunctions = (
+ database: ComposedDatabase,
+ schema: SchemaMetadata,
+ functions: FunctionMetadata[],
+ map: Map<string, FunctionMetadata>
+) => {
+ functions.forEach((fn) => {
+ const key = keyForResourceName({
+ database: database.name,
+ schema: schema.name,
+ function: fn.name,
+ });
+ map.set(key, fn);
+ });
+};
const mapSchemaConfigs = (
db: ComposedDatabase,
schemaConfigs: SchemaConfig[],
map: Map<string, SchemaConfig>
) => {
schemaConfigs.forEach((schemaConfig) => {
- const key = keyForResourceName(db.name, schemaConfig.name);
+ const key = keyForResourceName({
+ database: db.name,
+ schema: schemaConfig.name,
+ });
map.set(key, schemaConfig);
});
};
@@ -787,11 +976,11 @@ const mapTableConfigs = (
map: Map<string, TableConfig>
) => {
tableConfigs.forEach((tableConfig) => {
- const key = keyForResourceName(
- db.name,
- schemaConfig.name,
- tableConfig.name
- );
+ const key = keyForResourceName({
+ database: db.name,
+ schema: schemaConfig.name,
+ table: tableConfig.name,
+ });
map.set(key, tableConfig);
});
};
@@ -804,12 +993,12 @@ const mapColumnConfigs = (
map: Map<string, ColumnConfig>
) => {
columnConfigs.forEach((columnConfig) => {
- const key = keyForResourceName(
- db.name,
- schemaConfig.name,
- tableConfig.name,
- columnConfig.name
- );
+ const key = keyForResourceName({
+ database: db.name,
+ schema: schemaConfig.name,
+ table: tableConfig.name,
+ column: columnConfig.name,
+ });
map.set(key, columnConfig);
});
};
diff --git a/frontend/src/components/SchemaEditorLite/algorithm/utils.ts b/frontend/src/components/SchemaEditorLite/algorithm/utils.ts
index caca833a77..bc90e7b215 100644
--- a/frontend/src/components/SchemaEditorLite/algorithm/utils.ts
+++ b/frontend/src/components/SchemaEditorLite/algorithm/utils.ts
@@ -106,12 +106,12 @@ export const buildColumnConfigMap = (
database.schemaConfigs.flatMap((schemaConfig) => {
return schemaConfig.tableConfigs.flatMap((tableConfig) => {
return tableConfig.columnConfigs.map((columnConfig) => {
- const key = keyForResourceName(
- db.name,
- schemaConfig.name,
- tableConfig.name,
- columnConfig.name
- );
+ const key = keyForResourceName({
+ database: db.name,
+ schema: schemaConfig.name,
+ table: tableConfig.name,
+ column: columnConfig.name,
+ });
return [key, { schemaConfig, tableConfig, columnConfig }];
});
});
diff --git a/frontend/src/components/SchemaEditorLite/context/common.ts b/frontend/src/components/SchemaEditorLite/context/common.ts
index e988a23699..bcadc069ec 100644
--- a/frontend/src/components/SchemaEditorLite/context/common.ts
+++ b/frontend/src/components/SchemaEditorLite/context/common.ts
@@ -1,9 +1,12 @@
import type { ComposedDatabase } from "@/types";
import type {
ColumnMetadata,
+ FunctionMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
TablePartitionMetadata,
+ ViewMetadata,
} from "@/types/proto/v1/database_service";
export const keyForResource = (
@@ -13,25 +16,59 @@ export const keyForResource = (
table?: TableMetadata;
column?: ColumnMetadata;
partition?: TablePartitionMetadata;
- } = {}
+ view?: ViewMetadata;
+ procedure?: ProcedureMetadata;
+ function?: FunctionMetadata;
+ } = {},
+ suffix?: string
) => {
- const { schema, table, column, partition } = metadata;
+ const {
+ schema,
+ table,
+ view,
+ procedure,
+ function: func,
+ column,
+ partition,
+ } = metadata;
return keyForResourceName(
- database.name,
- schema?.name,
- table?.name,
- column?.name,
- partition?.name
+ {
+ database: database.name,
+ schema: schema?.name,
+ table: table?.name,
+ view: view?.name,
+ procedure: procedure?.name,
+ function: func?.name,
+ column: column?.name,
+ partition: partition?.name,
+ },
+ suffix
);
};
export const keyForResourceName = (
- database: string,
- schema?: string,
- table?: string,
- column?: string,
- partition?: string
+ params: {
+ database: string;
+ schema?: string;
+ table?: string;
+ view?: string;
+ procedure?: string;
+ function?: string;
+ column?: string;
+ partition?: string;
+ },
+ suffix?: string
) => {
+ const {
+ database,
+ schema,
+ table,
+ view,
+ procedure,
+ function: func,
+ column,
+ partition,
+ } = params;
const parts = [database];
if (schema !== undefined) {
parts.push(`schemas/${schema}`);
@@ -39,11 +76,24 @@ export const keyForResourceName = (
if (table !== undefined) {
parts.push(`tables/${table}`);
}
+ if (view !== undefined) {
+ parts.push(`views/${view}`);
+ }
+ if (procedure !== undefined) {
+ parts.push(`procedures/${procedure}`);
+ }
+ if (func !== undefined) {
+ parts.push(`functions/${func}`);
+ }
if (column !== undefined) {
parts.push(`columns/${column}`);
}
if (partition !== undefined) {
parts.push(`partitions/${partition}`);
}
+ if (suffix !== undefined) {
+ parts.push(suffix);
+ }
+
return parts.join("/");
};
diff --git a/frontend/src/components/SchemaEditorLite/context/config.ts b/frontend/src/components/SchemaEditorLite/context/config.ts
index 56550e66a5..aec3769c6e 100644
--- a/frontend/src/components/SchemaEditorLite/context/config.ts
+++ b/frontend/src/components/SchemaEditorLite/context/config.ts
@@ -24,11 +24,11 @@ export const useEditConfigs = (targets: Ref<EditTarget[]>) => {
targets.flatMap((target) => {
return target.metadata.schemaConfigs.flatMap((schemaConfig) => {
return schemaConfig.tableConfigs.map((tableConfig) => {
- const key = keyForResourceName(
- target.database.name,
- schemaConfig.name,
- tableConfig.name
- );
+ const key = keyForResourceName({
+ database: target.database.name,
+ schema: schemaConfig.name,
+ table: tableConfig.name,
+ });
return [key, tableConfig];
});
});
@@ -41,12 +41,12 @@ export const useEditConfigs = (targets: Ref<EditTarget[]>) => {
return target.metadata.schemaConfigs.flatMap((schemaConfig) => {
return schemaConfig.tableConfigs.flatMap((tableConfig) => {
return tableConfig.columnConfigs.map((columnConfig) => {
- const key = keyForResourceName(
- target.database.name,
- schemaConfig.name,
- tableConfig.name,
- columnConfig.name
- );
+ const key = keyForResourceName({
+ database: target.database.name,
+ schema: schemaConfig.name,
+ table: tableConfig.name,
+ column: columnConfig.name,
+ });
return [key, columnConfig];
});
});
@@ -122,12 +122,12 @@ export const useEditConfigs = (targets: Ref<EditTarget[]>) => {
update(config);
// Maintain the columnConfig map
config.columnConfigs.forEach((columnConfig) => {
- const key = keyForResourceName(
- database.name,
- metadata.schema.name,
- metadata.table.name,
- columnConfig.name
- );
+ const key = keyForResourceName({
+ database: database.name,
+ schema: metadata.schema.name,
+ table: metadata.table.name,
+ column: columnConfig.name,
+ });
maps.columnConfig.set(key, columnConfig);
});
};
diff --git a/frontend/src/components/SchemaEditorLite/context/edit.ts b/frontend/src/components/SchemaEditorLite/context/edit.ts
index 5dfdbd501b..b61920b6b8 100644
--- a/frontend/src/components/SchemaEditorLite/context/edit.ts
+++ b/frontend/src/components/SchemaEditorLite/context/edit.ts
@@ -3,6 +3,8 @@ import type { ComposedDatabase } from "@/types";
import type {
ColumnMetadata,
DatabaseMetadata,
+ FunctionMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
TablePartitionMetadata,
@@ -24,6 +26,8 @@ export const useEditStatus = () => {
table?: TableMetadata;
column?: ColumnMetadata;
partition?: TablePartitionMetadata;
+ procedure?: ProcedureMetadata;
+ function?: FunctionMetadata;
},
status: EditStatus
) => {
@@ -47,6 +51,8 @@ export const useEditStatus = () => {
table?: TableMetadata;
column?: ColumnMetadata;
partition?: TablePartitionMetadata;
+ procedure?: ProcedureMetadata;
+ function?: FunctionMetadata;
},
recursive: boolean
) => {
@@ -143,6 +149,36 @@ export const useEditStatus = () => {
return "normal";
};
+ const getProcedureStatus = (
+ database: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure: ProcedureMetadata;
+ }
+ ): EditStatus => {
+ const key = keyForResource(database, metadata);
+ if (dirtyPaths.value.has(key)) {
+ return dirtyPaths.value.get(key)!;
+ }
+ return "normal";
+ };
+
+ const getFunctionStatus = (
+ database: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ function: FunctionMetadata;
+ }
+ ): EditStatus => {
+ const key = keyForResource(database, metadata);
+ if (dirtyPaths.value.has(key)) {
+ return dirtyPaths.value.get(key)!;
+ }
+ return "normal";
+ };
+
const clearEditStatus = () => {
dirtyPaths.value.clear();
};
@@ -159,5 +195,7 @@ export const useEditStatus = () => {
getTableStatus,
getColumnStatus,
getPartitionStatus,
+ getProcedureStatus,
+ getFunctionStatus,
};
};
diff --git a/frontend/src/components/SchemaEditorLite/context/selection.ts b/frontend/src/components/SchemaEditorLite/context/selection.ts
index b4ff463fbf..bba834f72e 100644
--- a/frontend/src/components/SchemaEditorLite/context/selection.ts
+++ b/frontend/src/components/SchemaEditorLite/context/selection.ts
@@ -5,6 +5,8 @@ import type { ComposedDatabase } from "@/types";
import type {
ColumnMetadata,
DatabaseMetadata,
+ FunctionMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
} from "@/types/proto/v1/database_service";
@@ -88,6 +90,50 @@ export const useSelection = (
map.delete(keyForResource(db, metadata));
}
};
+ const updateProcedureSelectionImpl = (
+ map: Map<string, RolloutObject>,
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure: ProcedureMetadata;
+ },
+ on: boolean
+ ) => {
+ const key = keyForResource(db, metadata);
+ if (on) {
+ // select the procedure
+ map.set(key, {
+ db,
+ metadata,
+ });
+ } else {
+ // de-select the procedure
+ map.delete(key);
+ }
+ };
+ const updateFunctionSelectionImpl = (
+ map: Map<string, RolloutObject>,
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ function: FunctionMetadata;
+ },
+ on: boolean
+ ) => {
+ const key = keyForResource(db, metadata);
+ if (on) {
+ // select the function
+ map.set(key, {
+ db,
+ metadata,
+ });
+ } else {
+ // de-select the function
+ map.delete(key);
+ }
+ };
const emit = (map: Map<string, RolloutObject>) => {
events.emit("update:selected-rollout-objects", Array.from(map.values()));
};
@@ -284,6 +330,172 @@ export const useSelection = (
emit(updatedMap);
};
+ const getProcedureSelectionState = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure: ProcedureMetadata;
+ }
+ ) => {
+ if (!selectionEnabled.value) {
+ return { checked: false, indeterminate: false };
+ }
+
+ const checked = selectedRolloutObjectMap.value.has(
+ keyForResource(db, metadata)
+ );
+ return {
+ checked,
+ indeterminate: false,
+ };
+ };
+ const updateProcedureSelection = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure: ProcedureMetadata;
+ },
+ on: boolean
+ ) => {
+ if (!selectionEnabled.value) return;
+ const updatedMap = new Map(selectedRolloutObjectMap.value.entries());
+ updateProcedureSelectionImpl(updatedMap, db, metadata, on);
+ emit(updatedMap);
+ };
+ const getAllProceduresSelectionState = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ },
+ procedures: ProcedureMetadata[]
+ ) => {
+ if (!selectionEnabled.value || procedures.length === 0) {
+ return { checked: false, indeterminate: false };
+ }
+ const selected = procedures.filter((procedure) => {
+ return selectedRolloutObjectMap.value.has(
+ keyForResource(db, {
+ ...metadata,
+ procedure,
+ })
+ );
+ });
+ return {
+ checked: selected.length === procedures.length,
+ indeterminate: selected.length > 0 && selected.length < procedures.length,
+ };
+ };
+ const updateAllProceduresSelection = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ },
+ procedures: ProcedureMetadata[],
+ on: boolean
+ ) => {
+ const updatedMap = new Map(selectedRolloutObjectMap.value.entries());
+ procedures.forEach((procedure) => {
+ updateProcedureSelectionImpl(
+ updatedMap,
+ db,
+ {
+ ...metadata,
+ procedure,
+ },
+ on
+ );
+ });
+
+ emit(updatedMap);
+ };
+
+ const getFunctionSelectionState = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ function: FunctionMetadata;
+ }
+ ) => {
+ if (!selectionEnabled.value) {
+ return { checked: false, indeterminate: false };
+ }
+
+ const checked = selectedRolloutObjectMap.value.has(
+ keyForResource(db, metadata)
+ );
+ return {
+ checked,
+ indeterminate: false,
+ };
+ };
+ const updateFunctionSelection = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ function: FunctionMetadata;
+ },
+ on: boolean
+ ) => {
+ if (!selectionEnabled.value) return;
+ const updatedMap = new Map(selectedRolloutObjectMap.value.entries());
+ updateFunctionSelectionImpl(updatedMap, db, metadata, on);
+ emit(updatedMap);
+ };
+ const getAllFunctionsSelectionState = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ },
+ functions: FunctionMetadata[]
+ ) => {
+ if (!selectionEnabled.value || functions.length === 0) {
+ return { checked: false, indeterminate: false };
+ }
+ const selected = functions.filter((func) => {
+ return selectedRolloutObjectMap.value.has(
+ keyForResource(db, {
+ ...metadata,
+ function: func,
+ })
+ );
+ });
+ return {
+ checked: selected.length === functions.length,
+ indeterminate: selected.length > 0 && selected.length < functions.length,
+ };
+ };
+ const updateAllFunctionsSelection = (
+ db: ComposedDatabase,
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ },
+ functions: FunctionMetadata[],
+ on: boolean
+ ) => {
+ const updatedMap = new Map(selectedRolloutObjectMap.value.entries());
+ functions.forEach((func) => {
+ updateFunctionSelectionImpl(
+ updatedMap,
+ db,
+ {
+ ...metadata,
+ function: func,
+ },
+ on
+ );
+ });
+
+ emit(updatedMap);
+ };
+
return {
selectionEnabled,
getTableSelectionState,
@@ -294,5 +506,13 @@ export const useSelection = (
updateColumnSelection,
getAllColumnsSelectionState,
updateAllColumnsSelection,
+ getProcedureSelectionState,
+ updateProcedureSelection,
+ getAllProceduresSelectionState,
+ updateAllProceduresSelection,
+ getFunctionSelectionState,
+ updateFunctionSelection,
+ getAllFunctionsSelectionState,
+ updateAllFunctionsSelection,
};
};
diff --git a/frontend/src/components/SchemaEditorLite/context/tabs.ts b/frontend/src/components/SchemaEditorLite/context/tabs.ts
index 0ad09f05bb..72a72e55a2 100644
--- a/frontend/src/components/SchemaEditorLite/context/tabs.ts
+++ b/frontend/src/components/SchemaEditorLite/context/tabs.ts
@@ -81,6 +81,24 @@ export const useTabs = (events: SchemaEditorEvents) => {
tab.metadata.table.name === target.metadata.table.name
);
}
+ if (tab.type === "view" && target.type === "view") {
+ return (
+ tab.metadata.schema.name === target.metadata.schema.name &&
+ tab.metadata.view.name === target.metadata.view.name
+ );
+ }
+ if (tab.type === "procedure" && target.type === "procedure") {
+ return (
+ tab.metadata.schema.name === target.metadata.schema.name &&
+ tab.metadata.procedure.name === target.metadata.procedure.name
+ );
+ }
+ if (tab.type === "function" && target.type === "function") {
+ return (
+ tab.metadata.schema.name === target.metadata.schema.name &&
+ tab.metadata.function.name === target.metadata.function.name
+ );
+ }
return false;
});
};
diff --git a/frontend/src/components/SchemaEditorLite/engine-specs.ts b/frontend/src/components/SchemaEditorLite/engine-specs.ts
deleted file mode 100644
index 9f58e3e60e..0000000000
--- a/frontend/src/components/SchemaEditorLite/engine-specs.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { Engine } from "@/types/proto/v1/common";
-
-export const engineHasSchema = (engine: Engine) => {
- return [Engine.POSTGRES].includes(engine);
-};
diff --git a/frontend/src/components/SchemaEditorLite/spec.ts b/frontend/src/components/SchemaEditorLite/spec.ts
index f8c01ac600..2d8441b38a 100644
--- a/frontend/src/components/SchemaEditorLite/spec.ts
+++ b/frontend/src/components/SchemaEditorLite/spec.ts
@@ -15,3 +15,7 @@ export const engineSupportsEditProcedures = (engine: Engine) => {
export const engineSupportsEditFunctions = (engine: Engine) => {
return [Engine.MYSQL, Engine.TIDB].includes(engine);
};
+
+export const engineSupportsMultiSchema = (engine: Engine) => {
+ return [Engine.POSTGRES].includes(engine);
+};
diff --git a/frontend/src/components/SchemaEditorLite/types.ts b/frontend/src/components/SchemaEditorLite/types.ts
index c3438c7684..f89bcf341e 100644
--- a/frontend/src/components/SchemaEditorLite/types.ts
+++ b/frontend/src/components/SchemaEditorLite/types.ts
@@ -2,8 +2,11 @@ import type { ComposedDatabase } from "@/types";
import type {
ColumnMetadata,
DatabaseMetadata,
+ FunctionMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
+ ViewMetadata,
} from "@/types/proto/v1/database_service";
export type EditTarget = {
@@ -14,7 +17,7 @@ export type EditTarget = {
export type ResourceType = "branch" | "database";
-export type TabType = "database" | "table";
+export type TabType = "database" | "table" | "view" | "procedure" | "function";
export type CommonTabContext = {
id: string;
@@ -42,11 +45,49 @@ export type TableTabContext = CommonTabContext & {
};
};
-export type TabContext = DatabaseTabContext | TableTabContext;
+export type ViewTabContext = CommonTabContext & {
+ type: "view";
+ database: ComposedDatabase;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ view: ViewMetadata;
+ };
+};
+
+export type ProcedureTabContext = CommonTabContext & {
+ type: "procedure";
+ database: ComposedDatabase;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ procedure: ProcedureMetadata;
+ };
+};
+
+export type FunctionTabContext = CommonTabContext & {
+ type: "function";
+ database: ComposedDatabase;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ function: FunctionMetadata;
+ };
+};
+
+export type TabContext =
+ | DatabaseTabContext
+ | TableTabContext
+ | ViewTabContext
+ | ProcedureTabContext
+ | FunctionTabContext;
export type CoreTabContext =
| Omit<DatabaseTabContext, "id">
- | Omit<TableTabContext, "id">;
+ | Omit<TableTabContext, "id">
+ | Omit<ViewTabContext, "id">
+ | Omit<ProcedureTabContext, "id">
+ | Omit<FunctionTabContext, "id">;
export type EditStatus = "normal" | "created" | "dropped" | "updated";
@@ -58,7 +99,10 @@ export type RolloutObject = {
metadata: {
database: DatabaseMetadata;
schema: SchemaMetadata;
- table: TableMetadata;
+ table?: TableMetadata;
column?: ColumnMetadata;
+ view?: ViewMetadata;
+ procedure?: ProcedureMetadata;
+ function?: FunctionMetadata;
};
};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index fad8cb87fe..0787511854 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2293,7 +2293,10 @@
"drop-column": "Drop column",
"sql-preview": "SQL Preview",
"reset": "Reset",
- "save": "Save"
+ "save": "Save",
+ "create-procedure": "Create procedure",
+ "create-function": "Create function",
+ "drop": "Drop"
},
"database": {
"name": "Name",
@@ -2346,7 +2349,11 @@
"failed-to-fetch-database-schema": "Failed to fetch database schema",
"invalid-schema": "Invalid schema",
"cannot-drop-the-last-column": "Cannot drop the last column",
- "cannot-change-config": "Database config must changed with the schema"
+ "cannot-change-config": "Database config must changed with the schema",
+ "duplicated-procedure-name": "Duplicated procedure name",
+ "invalid-procedure-name": "Invalid procedure name",
+ "duplicated-function-name": "Duplicated function name",
+ "invalid-function-name": "Invalid function name"
},
"confirm-to-close": {
"title": "Confirm to close",
@@ -2370,7 +2377,10 @@
"value": "Value",
"add-sub-partition": "Add sub partition",
"type": "Type"
- }
+ },
+ "views": "Views",
+ "procedures": "Procedures",
+ "functions": "Functions"
},
"label": {
"empty-label-value": "Empty",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index ea548c33c4..860613491d 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2293,7 +2293,10 @@
"drop-column": "Eliminar columna",
"sql-preview": "Vista previa SQL",
"reset": "Restablecer",
- "save": "Guardar"
+ "save": "Guardar",
+ "create-procedure": "Crear procedimiento",
+ "create-function": "Crear función",
+ "drop": "Gota"
},
"database": {
"name": "Nombre",
@@ -2346,7 +2349,11 @@
"failed-to-fetch-database-schema": "Error al obtener el esquema de la base de datos",
"invalid-schema": "Esquema no válido",
"cannot-drop-the-last-column": "No se puede eliminar la última columna",
- "cannot-change-config": "La configuración de la base de datos debe cambiarse con el esquema"
+ "cannot-change-config": "La configuración de la base de datos debe cambiarse con el esquema",
+ "duplicated-procedure-name": "Nombre del procedimiento duplicado",
+ "invalid-procedure-name": "Nombre de procedimiento no válido",
+ "duplicated-function-name": "Nombre de función duplicado",
+ "invalid-function-name": "Nombre de función no válido"
},
"confirm-to-close": {
"title": "Confirmar para cerrar",
@@ -2370,7 +2377,10 @@
"value": "Valor",
"add-sub-partition": "Agregar subpartición",
"type": "Tipo"
- }
+ },
+ "views": "Puntos de vista",
+ "procedures": "Trámites",
+ "functions": "Funciones"
},
"label": {
"empty-label-value": "Valor",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 65bc42f985..f35a35376b 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -2293,7 +2293,10 @@
"drop-column": "列の削除",
"sql-preview": "SQLのプレビュー",
"reset": "リセット",
- "save": "保存"
+ "save": "保存",
+ "create-procedure": "プロシージャの作成",
+ "create-function": "関数の作成",
+ "drop": "落とす"
},
"database": {
"name": "名前",
@@ -2346,7 +2349,11 @@
"failed-to-fetch-database-schema": "データベース スキーマの要求に失敗しました",
"invalid-schema": "無効なスキーマ",
"cannot-drop-the-last-column": "最後の列は削除できません",
- "cannot-change-config": "データベース構成はスキーマとともに変更する必要があります"
+ "cannot-change-config": "データベース構成はスキーマとともに変更する必要があります",
+ "duplicated-procedure-name": "重複したプロシージャ名",
+ "invalid-procedure-name": "無効なプロシージャ名です",
+ "duplicated-function-name": "関数名が重複しています",
+ "invalid-function-name": "無効な関数名です"
},
"confirm-to-close": {
"title": "閉じることを確認しますか?",
@@ -2370,7 +2377,10 @@
"value": "価値",
"add-sub-partition": "サブパーティションの追加",
"type": "タイプ"
- }
+ },
+ "views": "ビュー",
+ "procedures": "手順",
+ "functions": "関数"
},
"label": {
"empty-label-value": "ヌル",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 5c10212742..f51d177861 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -2293,7 +2293,10 @@
"drop-column": "Cột thả",
"sql-preview": "Xem trước SQL",
"reset": "Cài lại",
- "save": "Cứu"
+ "save": "Cứu",
+ "create-procedure": "Tạo thủ tục",
+ "create-function": "Tạo chức năng",
+ "drop": "Làm rơi"
},
"database": {
"name": "Tên",
@@ -2346,7 +2349,11 @@
"failed-to-fetch-database-schema": "Không tìm nạp được lược đồ cơ sở dữ liệu",
"invalid-schema": "Lược đồ không hợp lệ",
"cannot-drop-the-last-column": "Không thể bỏ cột cuối cùng",
- "cannot-change-config": "Cấu hình cơ sở dữ liệu phải thay đổi với lược đồ"
+ "cannot-change-config": "Cấu hình cơ sở dữ liệu phải thay đổi với lược đồ",
+ "duplicated-procedure-name": "Tên thủ tục trùng lặp",
+ "invalid-procedure-name": "Tên thủ tục không hợp lệ",
+ "duplicated-function-name": "Tên hàm trùng lặp",
+ "invalid-function-name": "Tên hàm không hợp lệ"
},
"confirm-to-close": {
"title": "Xác nhận để đóng",
@@ -2370,7 +2377,10 @@
"value": "Giá trị",
"add-sub-partition": "Thêm phân vùng phụ",
"type": "Kiểu"
- }
+ },
+ "views": "Lượt xem",
+ "procedures": "Thủ tục",
+ "functions": "Hàm số"
},
"label": {
"empty-label-value": "Trống",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index e2dab8faf8..944e650056 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2293,7 +2293,10 @@
"drop-column": "删除列",
"sql-preview": "预览 SQL",
"reset": "重置",
- "save": "保存"
+ "save": "保存",
+ "create-procedure": "新建存储过程",
+ "create-function": "新建函数",
+ "drop": "删除"
},
"database": {
"name": "名称",
@@ -2346,7 +2349,11 @@
"failed-to-fetch-database-schema": "请求数据库 Schema 失败",
"invalid-schema": "无效的 Schema",
"cannot-drop-the-last-column": "不能删除最后一列",
- "cannot-change-config": "数据库配置必须同 Schema 一起变更"
+ "cannot-change-config": "数据库配置必须同 Schema 一起变更",
+ "duplicated-procedure-name": "重复的存储过程名称",
+ "invalid-procedure-name": "无效的存储过程名称",
+ "duplicated-function-name": "重复的函数名称",
+ "invalid-function-name": "无效的函数名称"
},
"confirm-to-close": {
"title": "确定关闭?",
@@ -2370,7 +2377,10 @@
"value": "值",
"add-sub-partition": "添加子分区",
"type": "类型"
- }
+ },
+ "views": "视图",
+ "procedures": "存储过程",
+ "functions": "函数"
},
"label": {
"empty-label-value": "空",
diff --git a/frontend/src/utils/schemaEditor/filter.ts b/frontend/src/utils/schemaEditor/filter.ts
index cebb6a3a76..8fadbea119 100644
--- a/frontend/src/utils/schemaEditor/filter.ts
+++ b/frontend/src/utils/schemaEditor/filter.ts
@@ -2,7 +2,9 @@ import {
ColumnMetadata,
DatabaseMetadata,
ForeignKeyMetadata,
+ FunctionMetadata,
IndexMetadata,
+ ProcedureMetadata,
SchemaMetadata,
TableMetadata,
TablePartitionMetadata,
@@ -19,6 +21,10 @@ export const filterDatabaseMetadata = (metadata: DatabaseMetadata) => {
return SchemaMetadata.fromPartial({
name: schema.name,
tables: schema.tables.map((table) => filterTableMetadata(table)),
+ procedures: schema.procedures.map((procedure) =>
+ filterProcedureMetadata(procedure)
+ ),
+ functions: schema.functions.map((func) => filterFunctionMetadata(func)),
});
}),
});
@@ -94,6 +100,20 @@ export const filterTableMetadata = (table: TableMetadata) => {
});
};
+export const filterProcedureMetadata = (procedure: ProcedureMetadata) => {
+ return ProcedureMetadata.fromPartial({
+ name: procedure.name,
+ definition: procedure.definition,
+ });
+};
+
+export const filterFunctionMetadata = (func: FunctionMetadata) => {
+ return FunctionMetadata.fromPartial({
+ name: func.name,
+ definition: func.definition,
+ });
+};
+
export const ComparableTableFields: (keyof TableMetadata)[] = [
"name",
"classification",