Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(branching): support table partitions editor UI (#11617)
Liu Jibdf6a4957008
diff --git a/frontend/src/components/Icon/TablePartitionIcon.vue b/frontend/src/components/Icon/TablePartitionIcon.vuenew file mode 100644index 0000000000..e87aebecdd--- /dev/null+++ b/frontend/src/components/Icon/TablePartitionIcon.vue@@ -0,0 +1,7 @@+<template>+ <TableRowsSplitIcon />+</template>++<script setup lang="ts">+import { TableRowsSplitIcon } from "lucide-vue-next";+</script>diff --git a/frontend/src/components/Icon/index.ts b/frontend/src/components/Icon/index.tsindex 1b7612a4f8..2eb3cbdcfd 100644--- a/frontend/src/components/Icon/index.ts+++ b/frontend/src/components/Icon/index.ts@@ -6,6 +6,7 @@ import PrimaryKeyIcon from "./PrimaryKeyIcon.vue";import SchemaIcon from "./SchemaIcon.vue";import SkipIcon from "./SkipIcon.vue";import TableIcon from "./TableIcon.vue";+import TablePartitionIcon from "./TablePartitionIcon.vue";import ViewIcon from "./ViewIcon.vue";export {@@ -18,4 +19,5 @@ export {IndexIcon,PrimaryKeyIcon,ColumnIcon,+ TablePartitionIcon,};diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/PartitionsEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/PartitionsEditor.vuenew file mode 100644index 0000000000..309f77a960--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/PartitionsEditor.vue@@ -0,0 +1,357 @@+<template>+ <div+ v-show="show"+ ref="containerElRef"+ class="w-full h-full overflow-x-auto"+ :data-height="containerHeight"+ :data-table-header-height="tableHeaderHeight"+ :data-table-body-height="tableBodyHeight"+ >+ <NDataTable+ v-bind="$attrs"+ ref="dataTableRef"+ size="small"+ :row-key="getItemKey"+ :columns="columns"+ :data="layoutReady ? flattenItemList : []"+ :row-class-name="classesForRow"+ :max-height="tableBodyHeight"+ :virtual-scroll="true"+ :striped="true"+ :bordered="true"+ :bottom-bordered="true"+ class="schema-editor-table-partitions-editor"+ :class="[disableDiffColoring && 'disable-diff-coloring']"+ />+ </div>+</template>++<script setup lang="tsx">+import { useElementSize } from "@vueuse/core";+import { pull } from "lodash-es";+import { ChevronDownIcon } from "lucide-vue-next";+import type { DataTableColumn } from "naive-ui";+import { computed, ref } from "vue";+import { useI18n } from "vue-i18n";+import type { ComposedDatabase } from "@/types";+import {+ TablePartitionMetadata,+ type DatabaseMetadata,+ type SchemaMetadata,+ type TableMetadata,+} from "@/types/proto/v1/database_service";+import type { EditStatus } from "../..";+import { useSchemaEditorContext } from "../../context";+import { markUUID } from "../common";+import {+ OperationCell,+ NameCell,+ TypeCell,+ ExpressionCell,+ ValueCell,+} from "./components";++type FlattenTablePartitionMetadata = {+ partition: TablePartitionMetadata;+ parent?: TablePartitionMetadata;+};++const props = withDefaults(+ defineProps<{+ show?: boolean;+ readonly?: boolean;+ db: ComposedDatabase;+ database: DatabaseMetadata;+ schema: SchemaMetadata;+ table: TableMetadata;+ maxBodyHeight?: number;+ }>(),+ {+ show: true,+ readonly: false,+ maxBodyHeight: undefined,+ }+);+const emit = defineEmits<{+ (event: "update"): void;+}>();++const { t } = useI18n();+const containerElRef = ref<HTMLElement>();+const tableHeaderElRef = computed(+ () =>+ containerElRef.value?.querySelector("thead.n-data-table-thead") as+ | HTMLElement+ | undefined+);+const { height: containerHeight } = useElementSize(containerElRef);+const { height: tableHeaderHeight } = useElementSize(tableHeaderElRef);+const tableBodyHeight = computed(() => {+ const bodyHeight = containerHeight.value - tableHeaderHeight.value - 2;+ const { maxBodyHeight = 0 } = props;+ if (maxBodyHeight > 0) {+ return Math.min(maxBodyHeight, bodyHeight);+ }+ return bodyHeight;+});+// Use this to avoid unnecessary initial rendering+const layoutReady = computed(() => tableHeaderHeight.value > 0);+const {+ disableDiffColoring,+ markEditStatus,+ getPartitionStatus,+ removeEditStatus,+} = useSchemaEditorContext();++const metadataForPartition = (partition: TablePartitionMetadata) => {+ return {+ database: props.database,+ schema: props.schema,+ table: props.table,+ partition,+ };+};+const statusForPartition = (partition: TablePartitionMetadata) => {+ return getPartitionStatus(props.db, metadataForPartition(partition));+};+const markStatus = (partition: TablePartitionMetadata, status: EditStatus) => {+ markEditStatus(props.db, metadataForPartition(partition), status);+};++const allowEditPartition = (partition: TablePartitionMetadata) => {+ if (props.readonly) {+ return false;+ }+ const status = statusForPartition(partition);+ return status === "created";+};++const classesForRow = (item: FlattenTablePartitionMetadata, index: number) => {+ return statusForPartition(item.partition);+};++const flattenItemList = computed(() => {+ const list: FlattenTablePartitionMetadata[] = [];+ const dfsWalk = (+ partition: TablePartitionMetadata,+ parent?: TablePartitionMetadata+ ) => {+ list.push({+ partition,+ parent,+ });+ partition.subpartitions?.forEach((child) => {+ dfsWalk(child, partition);+ });+ };+ props.table.partitions.forEach((partition) => dfsWalk(partition, undefined));+ return list;+});++const getItemKey = (item: FlattenTablePartitionMetadata) => {+ return markUUID(item.partition);+};++const columns = computed(() => {+ const columns: (DataTableColumn<FlattenTablePartitionMetadata> & {+ hide?: boolean;+ })[] = [+ {+ key: "parent",+ resizable: false,+ width: 24,+ render: (item) => {+ if (item.partition.subpartitions?.length > 0) {+ return <ChevronDownIcon class="w-4 h-4" />;+ }+ },+ },+ {+ key: "name",+ title: t("common.name"),+ resizable: true,+ minWidth: 140,+ maxWidth: 320,+ className: "input-cell",+ render: (item) => {+ return (+ <NameCell+ readonly={!allowEditPartition(item.partition)}+ partition={item.partition}+ onUpdate:name={(name) => {+ removeEditStatus(+ props.db,+ metadataForPartition(item.partition),+ /* !recursive */ false+ );+ item.partition.name = name;+ markStatus(item.partition, "created");+ emit("update");+ }}+ />+ );+ },+ },+ {+ key: "type",+ title: t("common.type"),+ resizable: true,+ minWidth: 140,+ className: "input-cell",+ render: (item) => {+ return (+ <TypeCell+ readonly={!allowEditPartition(item.partition)}+ partition={item.partition}+ parent={item.parent}+ onUpdate:type={(type) => {+ item.partition.type = type;+ emit("update");+ }}+ />+ );+ },+ },+ {+ key: "expression",+ title: t("schema-editor.table-partition.expression"),+ resizable: true,+ minWidth: 140,+ className: "input-cell",+ render: (item) => {+ return (+ <ExpressionCell+ readonly={!allowEditPartition(item.partition)}+ partition={item.partition}+ onUpdate:expression={(expression) => {+ item.partition.expression = expression;+ emit("update");+ }}+ />+ );+ },+ },+ {+ key: "value",+ title: t("schema-editor.table-partition.value"),+ resizable: true,+ minWidth: 140,+ className: "input-cell",+ render: (item) => {+ return (+ <ValueCell+ readonly={!allowEditPartition(item.partition)}+ partition={item.partition}+ onUpdate:value={(value) => {+ item.partition.value = value;+ emit("update");+ }}+ />+ );+ },+ },+ {+ key: "operations",+ title: "",+ resizable: false,+ width: 60,+ hide: props.readonly,+ className: "!px-0",+ render: (item) => {+ const status = statusForPartition(item.partition);+ return (+ <OperationCell+ partition={item.partition}+ parent={item.parent}+ status={status}+ onDrop={() => {+ const status = statusForPartition(item.partition);+ if (status === "created") {+ // For newly created partitions, we remove it directly+ if (item.parent) {+ pull(item.parent.subpartitions, item.partition);+ } else {+ pull(props.table.partitions, item.partition);+ }+ } else {+ markStatus(item.partition, "dropped");+ item.partition.subpartitions?.forEach((subpartition) => {+ markStatus(subpartition, "dropped");+ });+ }+ emit("update");+ }}+ onRestore={() => {+ removeEditStatus(+ props.db,+ metadataForPartition(item.partition),+ /* !recursive */ false+ );+ if (item.parent) {+ removeEditStatus(+ props.db,+ metadataForPartition(item.parent),+ /* !recursive */ false+ );+ }+ }}+ onAdd-sub={() => {+ const sub = TablePartitionMetadata.fromPartial({});+ markStatus(sub, "created");+ if (item.partition.subpartitions) {+ item.partition.subpartitions.push(sub);+ } else {+ item.partition.subpartitions = [sub];+ }+ emit("update");+ }}+ />+ );+ },+ },+ ];+ return columns.filter((header) => !header.hide);+});+</script>++<style lang="postcss" scoped>+.schema-editor-table-partitions-editor+ :deep(.n-data-table-th .n-data-table-resize-button::after) {+ @apply bg-control-bg h-2/3;+}+.schema-editor-table-partitions-editor :deep(.n-data-table-td.input-cell) {+ @apply pl-0.5 pr-1 py-0;+}+.schema-editor-table-partitions-editor+ :deep(.n-data-table-td .n-data-table-expand-placeholder) {+ @apply hidden;+}+.schema-editor-table-partitions-editor+ :deep(.n-data-table-td .n-data-table-expand-trigger) {+ @apply ml-2 mr-0;+}+.schema-editor-table-partitions-editor+ :deep(.n-data-table-td.input-cell .n-input__placeholder),+.schema-editor-table-partitions-editor+ :deep(.n-data-table-td.input-cell .n-base-selection-placeholder) {+ @apply italic;+}+.schema-editor-table-partitions-editor :deep(.n-data-table-td.checkbox-cell) {+ @apply pr-1 py-0;+}+.schema-editor-table-partitions-editor :deep(.n-data-table-td.text-cell) {+ @apply pr-1 py-0;+}+.schema-editor-table-partitions-editor+ :deep(.n-data-table-tr.created .n-data-table-td) {+ @apply text-green-700 !bg-green-50;+}+.schema-editor-table-partitions-editor+ :deep(.n-data-table-tr.dropped .n-data-table-td) {+ @apply text-red-700 cursor-not-allowed !bg-red-50 opacity-70;+}+.schema-editor-table-partitions-editor+ :deep(.n-data-table-tr.updated .n-data-table-td) {+ @apply text-yellow-700 !bg-yellow-50;+}+</style>diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/common.ts b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/common.tsnew file mode 100644index 0000000000..1b122862f0--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/common.ts@@ -0,0 +1,9 @@+import { TablePartitionMetadata_Type } from "@/types/proto/v1/database_service";++export const PartitionTypesSupportSubPartition: readonly TablePartitionMetadata_Type[] =+ [+ TablePartitionMetadata_Type.RANGE,+ TablePartitionMetadata_Type.RANGE_COLUMNS,+ TablePartitionMetadata_Type.LIST,+ TablePartitionMetadata_Type.LIST_COLUMNS,+ ] as const;diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/ExpressionCell.vue b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/ExpressionCell.vuenew file mode 100644index 0000000000..386de06613--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/ExpressionCell.vue@@ -0,0 +1,26 @@+<template>+ <InlineInput+ :value="partition.expression"+ :disabled="readonly"+ :placeholder="$t('schema-editor.table-partition.expression')"+ style="+ --n-padding-left: 6px;+ --n-padding-right: 4px;+ --n-text-color-disabled: rgb(var(--color-main));+ "+ @update:value="$emit('update:expression', $event)"+ />+</template>++<script lang="ts" setup>+import { InlineInput } from "@/components/v2";+import { type TablePartitionMetadata } from "@/types/proto/v1/database_service";++defineProps<{+ readonly?: boolean;+ partition: TablePartitionMetadata;+}>();+defineEmits<{+ (event: "update:expression", expression: string): void;+}>();+</script>diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/NameCell.vue b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/NameCell.vuenew file mode 100644index 0000000000..e765373708--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/NameCell.vue@@ -0,0 +1,27 @@+<template>+ <InlineInput+ :value="partition.name"+ :disabled="readonly"+ :placeholder="$t('common.name')"+ style="+ --n-padding-left: 6px;+ --n-padding-right: 4px;+ --n-text-color-disabled: rgb(var(--color-main));+ width: inherit;+ "+ @update:value="$emit('update:name', $event)"+ />+</template>++<script lang="ts" setup>+import { InlineInput } from "@/components/v2";+import { type TablePartitionMetadata } from "@/types/proto/v1/database_service";++defineProps<{+ readonly?: boolean;+ partition: TablePartitionMetadata;+}>();+defineEmits<{+ (event: "update:name", name: string): void;+}>();+</script>diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/OperationCell.vue b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/OperationCell.vuenew file mode 100644index 0000000000..16cc8a32f2--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/OperationCell.vue@@ -0,0 +1,70 @@+<template>+ <div class="flex justify-start items-center">+ <NPopconfirm v-if="status === 'created'" @positive-click="$emit('drop')">+ <template #trigger>+ <MiniActionButton tag="div">+ <TrashIcon class="w-4 h-4" />+ </MiniActionButton>+ </template>+ <template #default>+ <div class="flex flex-col">+ <p>+ {{ $t("branch.table-partition.drop-partition-confirm") }}+ </p>+ <p v-if="partition.subpartitions?.length > 0">+ {{+ $t(+ "branch.table-partition.drop-partition-with-subpartitions-confirm"+ )+ }}+ </p>+ </div>+ </template>+ </NPopconfirm>+ <NTooltip v-if="status === 'dropped'" trigger="hover" to="body">+ <template #trigger>+ <MiniActionButton tag="div" @click="$emit('restore')">+ <Undo2Icon class="w-4 h-4" />+ </MiniActionButton>+ </template>+ <span>{{ $t("schema-editor.actions.restore") }}</span>+ </NTooltip>+ <NTooltip v-if="allowAddSub" trigger="hover" to="body">+ <template #trigger>+ <MiniActionButton tag="div" @click="$emit('add-sub')">+ <CirclePlusIcon class="w-4 h-4" />+ </MiniActionButton>+ </template>+ <span>{{ $t("schema-editor.table-partition.add-sub-partition") }}</span>+ </NTooltip>+ </div>+</template>+<script lang="ts" setup>+import { CirclePlusIcon, TrashIcon, Undo2Icon } from "lucide-vue-next";+import { NPopconfirm, NTooltip } from "naive-ui";+import { computed } from "vue";+import type { EditStatus } from "@/components/SchemaEditorLite";+import { MiniActionButton } from "@/components/v2";+import type { TablePartitionMetadata } from "@/types/proto/v1/database_service";+import { PartitionTypesSupportSubPartition } from "../common";++const props = defineProps<{+ partition: TablePartitionMetadata;+ parent?: TablePartitionMetadata;+ status: EditStatus;+}>();+defineEmits<{+ (event: "drop"): void;+ (event: "restore"): void;+ (event: "add-sub"): void;+}>();++const allowAddSub = computed(() => {+ const { partition, parent } = props;+ if (parent) {+ // partitions cannot nest (only 1-level parent-sub relationships)+ return false;+ }+ return PartitionTypesSupportSubPartition.includes(partition.type);+});+</script>diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/TypeCell.vue b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/TypeCell.vuenew file mode 100644index 0000000000..666e8b4b6e--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/TypeCell.vue@@ -0,0 +1,96 @@+<template>+ <NSelect+ :value="partition.type || null"+ :options="typeOptions"+ :style="style"+ :disabled="readonly"+ :show-arrow="!readonly"+ :placeholder="$t('schema-editor.table-partition.type')"+ suffix-style="right: 3px"+ class="bb-schema-editor--table-partition-type-select"+ @focus="focused = true"+ @blur="focused = false"+ @update:value="$emit('update:type', $event)"+ />+</template>++<script lang="ts" setup>+import type { SelectOption } from "naive-ui";+import type { CSSProperties } from "vue";+import { computed, ref } from "vue";+import {+ tablePartitionMetadata_TypeToJSON,+ type TablePartitionMetadata,+ TablePartitionMetadata_Type,+} from "@/types/proto/v1/database_service";++const props = defineProps<{+ readonly?: boolean;+ partition: TablePartitionMetadata;+ parent?: TablePartitionMetadata;+}>();+defineEmits<{+ (event: "update:type", type: TablePartitionMetadata_Type): void;+}>();++const focused = ref(false);++const typeOptions = computed(() => {+ const values: TablePartitionMetadata_Type[] = props.parent+ ? [+ TablePartitionMetadata_Type.HASH,+ TablePartitionMetadata_Type.LINEAR_HASH,+ TablePartitionMetadata_Type.KEY,+ TablePartitionMetadata_Type.LINEAR_KEY,+ ]+ : [+ TablePartitionMetadata_Type.RANGE,+ TablePartitionMetadata_Type.RANGE_COLUMNS,+ TablePartitionMetadata_Type.LIST,+ TablePartitionMetadata_Type.LIST_COLUMNS,+ TablePartitionMetadata_Type.HASH,+ TablePartitionMetadata_Type.LINEAR_HASH,+ TablePartitionMetadata_Type.KEY,+ TablePartitionMetadata_Type.LINEAR_KEY,+ ];+ return values.map<SelectOption>((type) => {+ const label = tablePartitionMetadata_TypeToJSON(type);+ return {+ value: type,+ label,+ };+ });+});++const style = computed(() => {+ const style: CSSProperties = {+ "--n-padding-left": "6px",+ "--n-padding-right": "16px",+ "--n-color": "transparent",+ "--n-color-disabled": "transparent",+ "--n-text-color-disabled": "rgb(var(--color-main))",+ cursor: "default",+ };+ const border = focused.value+ ? "1px solid rgb(var(--color-control-border))"+ : "none";+ style["--n-border"] = border;+ style["--n-border-disabled"] = border;++ return style;+});+</script>++<style lang="postcss" scoped>+.bb-schema-editor--table-partition-type-select :deep(.n-base-selection) {+ --n-padding-single: 0 16px 0 6px !important;+ --n-color: transparent !important;+ --n-color-disabled: transparent !important;+ --n-border: none !important;+ --n-text-color-disabled: rgb(var(--color-main)) !important;+}+.bb-schema-editor--table-partition-type-select+ :deep(.n-base-selection .n-base-suffix) {+ right: 4px;+}+</style>diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/ValueCell.vue b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/ValueCell.vuenew file mode 100644index 0000000000..6a0ffb9a21--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/ValueCell.vue@@ -0,0 +1,26 @@+<template>+ <InlineInput+ :value="partition.value"+ :disabled="readonly"+ :placeholder="$t('schema-editor.table-partition.value')"+ style="+ --n-padding-left: 6px;+ --n-padding-right: 4px;+ --n-text-color-disabled: rgb(var(--color-main));+ "+ @update:value="$emit('update:value', $event)"+ />+</template>++<script lang="ts" setup>+import { InlineInput } from "@/components/v2";+import { type TablePartitionMetadata } from "@/types/proto/v1/database_service";++defineProps<{+ readonly?: boolean;+ partition: TablePartitionMetadata;+}>();+defineEmits<{+ (event: "update:value", value: string): void;+}>();+</script>diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/index.ts b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/index.tsnew file mode 100644index 0000000000..112fe478c6--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/components/index.ts@@ -0,0 +1,7 @@+import ExpressionCell from "./ExpressionCell.vue";+import NameCell from "./NameCell.vue";+import OperationCell from "./OperationCell.vue";+import TypeCell from "./TypeCell.vue";+import ValueCell from "./ValueCell.vue";++export { NameCell, TypeCell, ExpressionCell, ValueCell, OperationCell };diff --git a/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/index.ts b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/index.tsnew file mode 100644index 0000000000..f6ef90ee4a--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/PartitionsEditor/index.ts@@ -0,0 +1,3 @@+import PartitionsEditor from "./PartitionsEditor.vue";++export default PartitionsEditor;diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vueindex c9fbcaa652..d205faded9 100644--- a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue+++ b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue@@ -4,12 +4,26 @@><div class="w-full flex flex-row justify-between items-center"><div class="w-full flex justify-start items-center gap-x-2">- <template v-if="state.mode === 'INDEXES'">+ <template+ v-if="state.mode === 'INDEXES' || state.mode === 'PARTITIONS'"+ ><NButton size="small" @click="state.mode = 'COLUMNS'"><ArrowLeftIcon class="w-4 h-4" /></NButton><template v-if="!readonly">- <NButton size="small" @click="handleAddIndex">+ <NButton+ v-if="state.mode === 'INDEXES'"+ size="small"+ @click="handleAddIndex"+ >+ <PlusIcon class="w-4 h-4 mr-1" />+ {{ $t("common.add") }}+ </NButton>+ <NButton+ v-if="state.mode === 'PARTITIONS'"+ size="small"+ @click="handleAddPartition"+ ><PlusIcon class="w-4 h-4 mr-1" />{{ $t("common.add") }}</NButton>@@ -47,6 +61,19 @@: $t("schema-editor.index.edit-indexes")}}</NButton>+ <NButton+ v-if="hasTablePartitionsFeature"+ size="small"+ :disabled="disableChangeTable"+ @click="state.mode = 'PARTITIONS'"+ >+ <TablePartitionIcon class="w-3 h-3 mr-1" />+ {{+ readonly+ ? $t("schema-editor.table-partition.partitions")+ : $t("schema-editor.table-partition.edit-partitions")+ }}+ </NButton></template></div><div class="text-sm flex flex-row items-center justify-end gap-x-2">@@ -105,6 +132,15 @@:table="table"@update="markTableStatus('updated')"/>+ <PartitionsEditor+ :show="state.mode === 'PARTITIONS'"+ :readonly="readonly"+ :db="db"+ :database="database"+ :schema="schema"+ :table="table"+ @update="markTableStatus('updated')"+ /></div></div>@@ -159,11 +195,13 @@ import type {import {ColumnMetadata,IndexMetadata,+ TablePartitionMetadata,} from "@/types/proto/v1/database_service";import type { SchemaTemplateSetting_FieldTemplate } from "@/types/proto/v1/setting_service";import {arraySwap,instanceV1AllowsReorderColumns,+ instanceV1SupportsTablePartition,randomString,} from "@/utils";import FieldTemplates from "@/views/SchemaTemplate/FieldTemplates.vue";@@ -177,9 +215,10 @@ import {import type { EditStatus } from "../types";import ColumnSelectionSummary from "./ColumnSelectionSummary.vue";import IndexesEditor from "./IndexesEditor";+import PartitionsEditor from "./PartitionsEditor";import TableColumnEditor from "./TableColumnEditor";-type EditMode = "COLUMNS" | "INDEXES";+type EditMode = "COLUMNS" | "INDEXES" | "PARTITIONS";const props = withDefaults(defineProps<{@@ -293,6 +332,10 @@ const allowReorderColumns = computed(() => {return instanceV1AllowsReorderColumns(engine.value) && status === "created";});+const hasTablePartitionsFeature = computed(() => {+ return instanceV1SupportsTablePartition(engine.value);+});+const disableAlterColumn = (column: ColumnMetadata): boolean => {return (isDroppedSchema.value || isDroppedTable.value || isDroppedColumn(column)@@ -375,6 +418,22 @@ const handleAddIndex = () => {);markTableStatus("updated");};+const handleAddPartition = () => {+ const partition = TablePartitionMetadata.fromPartial({});+ // eslint-disable-next-line vue/no-mutating-props+ props.table.partitions.push(partition);+ markEditStatus(+ props.db,+ {+ database: props.database,+ schema: props.schema,+ table: props.table,+ partition,+ },+ "created"+ );+ markTableStatus("updated");+};const gotoForeignKeyReferencedTable = (column: ColumnMetadata,diff --git a/frontend/src/components/SchemaEditorLite/Panels/common.ts b/frontend/src/components/SchemaEditorLite/Panels/common.tsindex 3673558140..eed5793dde 100644--- a/frontend/src/components/SchemaEditorLite/Panels/common.ts+++ b/frontend/src/components/SchemaEditorLite/Panels/common.ts@@ -1,4 +1,5 @@import { v1 as uuidv1 } from "uuid";+import type { EditStatus } from "../types";export const markUUID = (obj: any) => {// column.name is editable, so we need to insert another hidden field@@ -14,3 +15,16 @@ export const markUUID = (obj: any) => {}return obj.__uuid as string;};++export const markEditStatusBeforeDrop = (obj: any, status: EditStatus) => {+ Object.defineProperty(obj, "__status_before_drop", {+ enumerable: false,+ writable: true,+ configurable: false,+ value: status,+ });+};++export const getEditStatusBeforeDrop = (obj: any) => {+ return obj["__status_before_drop"] as EditStatus | undefined;+};diff --git a/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.ts b/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.tsindex 6e22fec9ff..cb85c79ec8 100644--- a/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.ts+++ b/frontend/src/components/SchemaEditorLite/algorithm/diff-merge.ts@@ -1,19 +1,20 @@import { cloneDeep, isEqual, pick } from "lodash-es";import type { ComposedDatabase } from "@/types";-import type {- ColumnMetadata,- DatabaseMetadata,- ForeignKeyMetadata,- IndexMetadata,- SchemaMetadata,- TableMetadata,+import type { TablePartitionMetadata } from "@/types/proto/v1/database_service";+import {+ type ColumnMetadata,+ type DatabaseMetadata,+ type ForeignKeyMetadata,+ type IndexMetadata,+ type SchemaMetadata,+ type TableMetadata,} from "@/types/proto/v1/database_service";import {ColumnConfig,SchemaConfig,TableConfig,} from "@/types/proto/v1/database_service";-import { TinyTimer, keyBy } from "@/utils";+import { ComparableTablePartitionFields, TinyTimer, keyBy } from "@/utils";import {ComparableColumnFields,ComparableForeignKeyFields,@@ -65,6 +66,7 @@ export class DiffMerge {| "mergeSchemas"| "mergeTables"| "mergeColumns"+ | "mergeTablePartitions"| "diffColumn"| "mergeSchemaConfigs"| "mergeTableConfigs"@@ -183,6 +185,10 @@ export class DiffMerge {{ ...source, table: sourceTable },{ ...target, table: targetTable });+ this.mergeTablePartitions(+ { ...source, table: sourceTable },+ { ...target, table: targetTable }+ );if (!isEqual(pick(sourceTable, ComparableTableFields),@@ -271,6 +277,122 @@ export class DiffMerge {}return true;}+ isEqualTablePartitions(+ sourcePartitions: TablePartitionMetadata[],+ targetPartitions: TablePartitionMetadata[]+ ) {+ if (sourcePartitions.length !== targetPartitions.length) {+ return false;+ }+ const targetPartitionsByName = keyBy(targetPartitions, (part) => part.name);++ for (let i = 0; i < sourcePartitions.length; i++) {+ const sourcePartition = sourcePartitions[i];+ const targetPartition = targetPartitionsByName.get(sourcePartition.name);+ // targetPartition not found+ if (!targetPartition) return false;+ if (+ !isEqual(+ pick(sourcePartition, ComparableTablePartitionFields),+ pick(targetPartition, ComparableTablePartitionFields)+ )+ ) {+ return false;+ }+ if (+ !this.isEqualTablePartitions(+ sourcePartition.subpartitions ?? [],+ targetPartition.subpartitions ?? []+ )+ ) {+ return false;+ }+ }+ return true;+ }+ mergeTablePartitions(source: RichTableMetadata, target: RichTableMetadata) {+ const { context, database } = this;+ const { schema: sourceSchema, table: sourceTable } = source;+ const { schema: targetSchema, table: targetTable } = target;++ const doMergePartitions = (+ sourcePartitions: TablePartitionMetadata[],+ targetPartitions: TablePartitionMetadata[]+ ) => {+ const sourcePartitionMap = new Map<string, TablePartitionMetadata>();+ const targetPartitionMap = new Map<string, TablePartitionMetadata>();++ mapTablePartitions(+ database,+ sourceSchema,+ sourceTable,+ sourcePartitions,+ sourcePartitionMap+ );+ mapTablePartitions(+ database,+ targetSchema,+ targetTable,+ targetPartitions,+ targetPartitionMap+ );++ 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+ );+ let targetPartition = targetPartitionMap.get(key);+ if (targetPartition) {+ // existed partition+ mergedPartitions.push(targetPartition);+ targetPartition.subpartitions = doMergePartitions(+ sourcePartition.subpartitions ?? [],+ targetPartition.subpartitions ?? []+ );+ continue;+ }+ // dropped partition+ // copy the source partition to target and mark it as 'dropped'+ targetPartition = cloneDeep(sourcePartition);+ mergedPartitions.push(targetPartition);+ context.markEditStatusByKey(key, "dropped");+ }+ 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 sourcePartition = sourcePartitionMap.get(key);+ if (!sourcePartition) {+ // newly created partition+ // mark it as 'created'+ mergedPartitions.push(targetPartition);+ context.markEditStatusByKey(key, "created");+ }+ }+ return mergedPartitions;+ };++ this.timer.begin("mergeTablePartitions");+ targetTable.partitions = doMergePartitions(+ sourceTable.partitions,+ targetTable.partitions+ );+ this.timer.end(+ "mergeTablePartitions",+ sourceTable.partitions.length + targetTable.partitions.length+ );+ }mergeColumns(source: RichTableMetadata, target: RichTableMetadata) {const { context, database, sourceColumnMap, targetColumnMap } = this;const { schema: sourceSchema, table: sourceTable } = source;@@ -630,6 +752,24 @@ const mapColumns = (map.set(key, column);});};+const mapTablePartitions = (+ database: ComposedDatabase,+ schema: SchemaMetadata,+ table: TableMetadata,+ partitions: TablePartitionMetadata[],+ map: Map<string, TablePartitionMetadata>+) => {+ partitions.forEach((partition) => {+ const key = keyForResourceName(+ database.name,+ schema.name,+ table.name,+ /* column */ undefined,+ partition.name+ );+ map.set(key, partition);+ });+};const mapSchemaConfigs = (db: ComposedDatabase,schemaConfigs: SchemaConfig[],diff --git a/frontend/src/components/SchemaEditorLite/context/common.ts b/frontend/src/components/SchemaEditorLite/context/common.tsindex ef2e748839..e988a23699 100644--- a/frontend/src/components/SchemaEditorLite/context/common.ts+++ b/frontend/src/components/SchemaEditorLite/context/common.ts@@ -3,6 +3,7 @@ import type {ColumnMetadata,SchemaMetadata,TableMetadata,+ TablePartitionMetadata,} from "@/types/proto/v1/database_service";export const keyForResource = (@@ -11,14 +12,16 @@ export const keyForResource = (schema?: SchemaMetadata;table?: TableMetadata;column?: ColumnMetadata;+ partition?: TablePartitionMetadata;} = {}) => {- const { schema, table, column } = metadata;+ const { schema, table, column, partition } = metadata;return keyForResourceName(database.name,schema?.name,table?.name,- column?.name+ column?.name,+ partition?.name);};@@ -26,7 +29,8 @@ export const keyForResourceName = (database: string,schema?: string,table?: string,- column?: string+ column?: string,+ partition?: string) => {const parts = [database];if (schema !== undefined) {@@ -38,5 +42,8 @@ export const keyForResourceName = (if (column !== undefined) {parts.push(`columns/${column}`);}+ if (partition !== undefined) {+ parts.push(`partitions/${partition}`);+ }return parts.join("/");};diff --git a/frontend/src/components/SchemaEditorLite/context/edit.ts b/frontend/src/components/SchemaEditorLite/context/edit.tsindex 7df9162099..5dfdbd501b 100644--- a/frontend/src/components/SchemaEditorLite/context/edit.ts+++ b/frontend/src/components/SchemaEditorLite/context/edit.ts@@ -5,6 +5,7 @@ import type {DatabaseMetadata,SchemaMetadata,TableMetadata,+ TablePartitionMetadata,} from "@/types/proto/v1/database_service";import type { EditStatus } from "../types";import { keyForResource } from "./common";@@ -22,6 +23,7 @@ export const useEditStatus = () => {schema: SchemaMetadata;table?: TableMetadata;column?: ColumnMetadata;+ partition?: TablePartitionMetadata;},status: EditStatus) => {@@ -44,6 +46,7 @@ export const useEditStatus = () => {schema: SchemaMetadata;table?: TableMetadata;column?: ColumnMetadata;+ partition?: TablePartitionMetadata;},recursive: boolean) => {@@ -121,6 +124,25 @@ export const useEditStatus = () => {return "normal";};+ const getPartitionStatus = (+ database: ComposedDatabase,+ metadata: {+ database: DatabaseMetadata;+ schema: SchemaMetadata;+ table: TableMetadata;+ partition: TablePartitionMetadata;+ }+ ): EditStatus => {+ const key = keyForResource(database, metadata);+ if (dirtyPaths.value.has(key)) {+ return dirtyPaths.value.get(key)!;+ }+ if (dirtyPathsArray.value.some((path) => path.startsWith(`${key}/`))) {+ return "updated";+ }+ return "normal";+ };+const clearEditStatus = () => {dirtyPaths.value.clear();};@@ -136,5 +158,6 @@ export const useEditStatus = () => {getSchemaStatus,getTableStatus,getColumnStatus,+ getPartitionStatus,};};diff --git a/frontend/src/components/v2/Form/InlineInput.vue b/frontend/src/components/v2/Form/InlineInput.vueindex 868b75a420..674c04e480 100644--- a/frontend/src/components/v2/Form/InlineInput.vue+++ b/frontend/src/components/v2/Form/InlineInput.vue@@ -2,6 +2,7 @@<NInput:value="value":style="style"+ v-bind="$attrs"@focus="focused = true"@blur="focused = false"@update:value="$emit('update:value', $event)"@@ -13,6 +14,10 @@ import { NInput } from "naive-ui";import type { CSSProperties } from "vue";import { computed, ref } from "vue";+defineOptions({+ inheritAttrs: false,+});+defineProps<{value: string | undefined | null;}>();diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 9e23f13dde..69ed48aa04 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -2336,7 +2336,15 @@"edit-indexes": "Edit indexes","unique": "Unique"},- "generated-ddl-is-empty": "Generated DDL is empty"+ "generated-ddl-is-empty": "Generated DDL is empty",+ "table-partition": {+ "partitions": "Partitions",+ "edit-partitions": "Edit partitions",+ "expression": "Expression",+ "value": "Value",+ "add-sub-partition": "Add sub partition",+ "type": "Type"+ }},"label": {"empty-label-value": "Empty",@@ -3404,6 +3412,10 @@"schema-text": "Schema Text","baseline": "Baseline","head": "Head",- "show-diff-with-branch-baseline": "Show diff with branch baseline"+ "show-diff-with-branch-baseline": "Show diff with branch baseline",+ "table-partition": {+ "drop-partition-confirm": "Drop table partition?",+ "drop-partition-with-subpartitions-confirm": "Will also drop all its subpartitions."+ }}}diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 2884745995..416eb738cb 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -2336,7 +2336,15 @@"edit-indexes": "Editar índices","unique": "Único"},- "generated-ddl-is-empty": "El DDL generado está vacío"+ "generated-ddl-is-empty": "El DDL generado está vacío",+ "table-partition": {+ "partitions": "Particiones",+ "edit-partitions": "Editar particiones",+ "expression": "Expresión",+ "value": "Valor",+ "add-sub-partition": "Agregar subpartición",+ "type": "Tipo"+ }},"label": {"empty-label-value": "Valor",@@ -3404,6 +3412,10 @@"schema-text": "Texto del esquema","baseline": "Base","head": "Cabeza",- "show-diff-with-branch-baseline": "Mostrar diferencia con la línea base de la rama"+ "show-diff-with-branch-baseline": "Mostrar diferencia con la línea base de la rama",+ "table-partition": {+ "drop-partition-confirm": "¿Eliminar partición de mesa?",+ "drop-partition-with-subpartitions-confirm": "También eliminará todas sus subparticiones."+ }}}\ No newline at end of filediff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 43bd97af2f..34c6a7324d 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -2336,7 +2336,15 @@"edit-indexes": "インデックスの編集","unique": "ユニーク"},- "generated-ddl-is-empty": "生成されたDDLは空です"+ "generated-ddl-is-empty": "生成されたDDLは空です",+ "table-partition": {+ "partitions": "パーティション",+ "edit-partitions": "パーティションの編集",+ "expression": "表現",+ "value": "価値",+ "add-sub-partition": "サブパーティションの追加",+ "type": "タイプ"+ }},"label": {"empty-label-value": "空です",@@ -3404,6 +3412,10 @@"schema-text": "スキーマテキスト","baseline": "ベースライン","head": "頭",- "show-diff-with-branch-baseline": "ブランチベースラインとの差分を表示"+ "show-diff-with-branch-baseline": "ブランチベースラインとの差分を表示",+ "table-partition": {+ "drop-partition-confirm": "テーブルパーティションを削除しますか?",+ "drop-partition-with-subpartitions-confirm": "また、そのサブパーティションもすべて削除されます。"+ }}}\ No newline at end of filediff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex 26ae2c1376..9fba614fd8 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -2336,7 +2336,15 @@"edit-indexes": "Chỉnh sửa chỉ mục","unique": "Độc nhất"},- "generated-ddl-is-empty": "DDL được tạo trống"+ "generated-ddl-is-empty": "DDL được tạo trống",+ "table-partition": {+ "partitions": "Phân vùng",+ "edit-partitions": "Chỉnh sửa phân vùng",+ "expression": "Sự biểu lộ",+ "value": "Giá trị",+ "add-sub-partition": "Thêm phân vùng phụ",+ "type": "Kiểu"+ }},"label": {"empty-label-value": "Trống",@@ -3404,6 +3412,10 @@"schema-text": "Văn bản lược đồ","baseline": "Đường cơ sở","head": "Cái đầu",- "show-diff-with-branch-baseline": "Hiển thị sự khác biệt với đường cơ sở của nhánh"+ "show-diff-with-branch-baseline": "Hiển thị sự khác biệt với đường cơ sở của nhánh",+ "table-partition": {+ "drop-partition-confirm": "Bỏ phân vùng bảng?",+ "drop-partition-with-subpartitions-confirm": "Cũng sẽ bỏ tất cả các phân vùng của nó."+ }}}\ No newline at end of filediff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex e10acaffb5..48be11c331 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -2336,7 +2336,15 @@"edit-indexes": "编辑索引","unique": "唯一"},- "generated-ddl-is-empty": "生成的 DDL 为空"+ "generated-ddl-is-empty": "生成的 DDL 为空",+ "table-partition": {+ "partitions": "分区",+ "edit-partitions": "编辑分区",+ "expression": "表达式",+ "value": "值",+ "add-sub-partition": "添加子分区",+ "type": "类型"+ }},"label": {"empty-label-value": "空",@@ -3404,6 +3412,10 @@"schema-text": "文本 Schema","baseline": "基线","head": "当前",- "show-diff-with-branch-baseline": "显示与分支基线之间的差异"+ "show-diff-with-branch-baseline": "显示与分支基线之间的差异",+ "table-partition": {+ "drop-partition-confirm": "删除表分区?",+ "drop-partition-with-subpartitions-confirm": "将会同时删除其所有子分区。"+ }}}\ No newline at end of filediff --git a/frontend/src/utils/schemaEditor/filter.ts b/frontend/src/utils/schemaEditor/filter.tsindex 62783af9cd..cebb6a3a76 100644--- a/frontend/src/utils/schemaEditor/filter.ts+++ b/frontend/src/utils/schemaEditor/filter.ts@@ -5,6 +5,7 @@ import {IndexMetadata,SchemaMetadata,TableMetadata,+ TablePartitionMetadata,} from "@/types/proto/v1/database_service";// filterDatabaseMetadata filter out the objects/attributes we do not support.@@ -61,6 +62,21 @@ export const filterForeignKeyMetadata = (fk: ForeignKeyMetadata) => {});};+export const filterTablePartitionMetadata = (+ partition: TablePartitionMetadata+): TablePartitionMetadata => {+ return TablePartitionMetadata.fromPartial({+ name: partition.name,+ type: partition.type,+ useDefault: partition.useDefault,+ value: partition.value,+ expression: partition.expression,+ subpartitions: partition.subpartitions+ ? partition.subpartitions.map((sub) => filterTablePartitionMetadata(sub))+ : undefined,+ });+};+export const filterTableMetadata = (table: TableMetadata) => {return TableMetadata.fromPartial({name: table.name,@@ -72,6 +88,9 @@ export const filterTableMetadata = (table: TableMetadata) => {columns: table.columns.map((column) => filterColumnMetadata(column)),indexes: table.indexes.map((index) => filterIndexMetadata(index)),foreignKeys: table.foreignKeys.map((fk) => filterForeignKeyMetadata(fk)),+ partitions: table.partitions.map((partition) =>+ filterTablePartitionMetadata(partition)+ ),});};@@ -98,6 +117,8 @@ export const ComparableForeignKeyFields: (keyof ForeignKeyMetadata)[] = ["referencedTable","referencedColumns",];+export const ComparableTablePartitionFields: (keyof TablePartitionMetadata)[] =+ ["name", "type", "expression", "value"];export const ComparableColumnFields: (keyof ColumnMetadata)[] = ["name","comment",diff --git a/frontend/src/utils/v1/instance.ts b/frontend/src/utils/v1/instance.tsindex fbf61bda08..3e9710a0ec 100644--- a/frontend/src/utils/v1/instance.ts+++ b/frontend/src/utils/v1/instance.ts@@ -230,6 +230,13 @@ export const instanceV1SupportsConciseSchema = (return [Engine.ORACLE].includes(engine);};+export const instanceV1SupportsTablePartition = (+ instanceOrEngine: Instance | Engine+) => {+ const engine = engineOfInstanceV1(instanceOrEngine);+ return [Engine.MYSQL, Engine.TIDB].includes(engine);+};+export const engineOfInstanceV1 = (instanceOrEngine: Instance | Engine) => {if (typeof instanceOrEngine === "number") {return instanceOrEngine;