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(branch): support index in schema editor (#10190)
Liu Jic58dc7251411
diff --git a/frontend/src/components/Icon/IndexIcon.vue b/frontend/src/components/Icon/IndexIcon.vuenew file mode 100644index 0000000000..d02eae4d3d--- /dev/null+++ b/frontend/src/components/Icon/IndexIcon.vue@@ -0,0 +1,3 @@+<template>+ <tabler:diamonds class="w-3 h-3" />+</template>diff --git a/frontend/src/components/Icon/index.ts b/frontend/src/components/Icon/index.tsindex 789ebd1af5..33cb42487d 100644--- a/frontend/src/components/Icon/index.ts+++ b/frontend/src/components/Icon/index.ts@@ -1,8 +1,17 @@import DatabaseIcon from "./DatabaseIcon.vue";import EngineIcon from "./EngineIcon.vue";+import IndexIcon from "./IndexIcon.vue";import SchemaIcon from "./SchemaIcon.vue";import SkipIcon from "./SkipIcon.vue";import TableIcon from "./TableIcon.vue";import ViewIcon from "./ViewIcon.vue";-export { SkipIcon, EngineIcon, DatabaseIcon, SchemaIcon, TableIcon, ViewIcon };+export {+ SkipIcon,+ EngineIcon,+ DatabaseIcon,+ SchemaIcon,+ TableIcon,+ ViewIcon,+ IndexIcon,+};diff --git a/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/IndexesEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/IndexesEditor.vuenew file mode 100644index 0000000000..9326b440af--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/IndexesEditor.vue@@ -0,0 +1,268 @@+<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="getIndexKey"+ :columns="columns"+ :data="layoutReady ? indexList : []"+ :max-height="tableBodyHeight"+ :virtual-scroll="true"+ :striped="true"+ :bordered="true"+ :bottom-bordered="true"+ class="schema-editor-table-indexes-editor"+ />+ </div>+</template>++<script lang="ts" setup>+import { useElementSize } from "@vueuse/core";+import { pull } from "lodash-es";+import {+ DataTableColumn,+ DataTableInst,+ NCheckbox,+ NDataTable,+} from "naive-ui";+import { computed, h, ref } from "vue";+import { useI18n } from "vue-i18n";+import { InlineInput } from "@/components/v2";+import { ComposedDatabase } from "@/types";+import {+ DatabaseMetadata,+ IndexMetadata,+ SchemaMetadata,+ TableMetadata,+} from "@/types/proto/v1/database_service";+import { markUUID } from "../common";+import { ColumnsCell, OperationCell } from "./components";++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 dataTableRef = ref<DataTableInst>();+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 { t } = useI18n();++const indexList = computed(() => {+ return props.table.indexes;+});++const getIndexKey = (index: IndexMetadata) => {+ return markUUID(index);+};++const columns = computed(() => {+ const columns: (DataTableColumn<IndexMetadata> & { hide?: boolean })[] = [+ {+ key: "name",+ title: t("schema-editor.column.name"),+ resizable: true,+ minWidth: 140,+ className: "input-cell",+ render: (index) => {+ return h(InlineInput, {+ value: index.name,+ disabled: props.readonly,+ placeholder: t("common.name"),+ style: {+ "--n-padding-left": "6px",+ "--n-padding-right": "4px",+ "--n-text-color-disabled": "rgb(var(--color-main))",+ },+ "onUpdate:value": (value: string) => {+ index.name = value;+ emit("update");+ },+ });+ },+ },+ {+ key: "columns",+ title: t("schema-editor.columns"),+ resizable: true,+ minWidth: 400,+ maxWidth: 480,+ className: "input-cell",+ render: (index) => {+ return h(ColumnsCell, {+ readonly: props.readonly,+ db: props.db,+ database: props.database,+ schema: props.schema,+ table: props.table,+ index,+ "onUpdate:expressions": (expressions) => {+ index.expressions = expressions;+ emit("update");+ },+ });+ },+ },+ {+ key: "comment",+ title: t("schema-editor.column.comment"),+ resizable: true,+ minWidth: 140,+ maxWidth: 320,+ className: "input-cell",+ render: (index) => {+ return h(InlineInput, {+ value: index.comment,+ disabled: props.readonly,+ placeholder: "comment",+ style: {+ "--n-padding-left": "6px",+ "--n-padding-right": "4px",+ "--n-text-color-disabled": "rgb(var(--color-main))",+ },+ "onUpdate:value": (value) => {+ index.comment = value;+ emit("update");+ },+ });+ },+ },+ {+ key: "primary",+ title: t("schema-editor.column.primary"),+ resizable: false,+ width: 80,+ className: "checkbox-cell",+ render: (index) => {+ const allowTurnOnOrOffPrimary = () => {+ if (index.primary) return true;+ return !props.table.indexes.some((idx) => idx.primary);+ };+ return h(NCheckbox, {+ checked: index.primary,+ disabled: props.readonly || !allowTurnOnOrOffPrimary(),+ "onUpdate:checked": (checked: boolean) => {+ index.primary = checked;+ if (checked) {+ index.unique = false;+ }+ emit("update");+ },+ });+ },+ },+ {+ key: "unique",+ title: t("schema-editor.index.unique"),+ resizable: false,+ width: 80,+ className: "checkbox-cell",+ render: (index) => {+ return h(NCheckbox, {+ checked: index.unique,+ disabled: props.readonly,+ "onUpdate:checked": (checked: boolean) => {+ index.unique = checked;+ if (checked) {+ index.primary = false;+ }+ emit("update");+ },+ });+ },+ },+ {+ key: "operations",+ title: "",+ resizable: false,+ width: 30,+ hide: props.readonly,+ className: "!px-0",+ render: (index) => {+ return h(OperationCell, {+ index,+ onDrop: () => {+ pull(props.table.indexes, index);+ emit("update");+ },+ });+ },+ },+ ];+ return columns.filter((header) => !header.hide);+});+</script>++<style lang="postcss" scoped>+.schema-editor-table-indexes-editor+ :deep(.n-data-table-th .n-data-table-resize-button::after) {+ @apply bg-control-bg h-2/3;+}+.schema-editor-table-indexes-editor :deep(.n-data-table-td.input-cell) {+ @apply pl-0.5 pr-1 py-0;+}+.schema-editor-table-indexes-editor+ :deep(.n-data-table-td.input-cell .n-input__placeholder),+.schema-editor-table-indexes-editor+ :deep(.n-data-table-td.input-cell .n-base-selection-placeholder) {+ @apply italic;+}+.schema-editor-table-indexes-editor :deep(.n-data-table-td.checkbox-cell) {+ @apply pr-1 py-0;+}+.schema-editor-table-indexes-editor :deep(.n-data-table-td.text-cell) {+ @apply pr-1 py-0;+}+.schema-editor-table-indexes-editor+ :deep(.n-data-table-tr.created .n-data-table-td) {+ @apply text-green-700 !bg-green-50;+}+.schema-editor-table-indexes-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-indexes-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/IndexesEditor/components/ColumnsCell.vue b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/ColumnsCell.vuenew file mode 100644index 0000000000..8e37a421ec--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/ColumnsCell.vue@@ -0,0 +1,89 @@+<template>+ <NSelect+ :value="index.expressions"+ :options="columnOptions"+ :disabled="readonly"+ :consistent-menu-width="true"+ :style="style"+ :multiple="true"+ :max-tag-count="'responsive'"+ :placeholder="$t('schema-editor.columns')"+ :filterable="true"+ suffix-style="right: 3px"+ class="bb-schema-editor--index-columns-select"+ @focus="focused = true"+ @blur="focused = false"+ @update:value="$emit('update:expressions', $event as string[])"+ />+</template>++<script lang="ts" setup>+import { NSelect, SelectOption } from "naive-ui";+import { CSSProperties, computed, ref } from "vue";+import { ComposedDatabase } from "@/types";+import {+ ColumnMetadata,+ DatabaseMetadata,+ IndexMetadata,+ SchemaMetadata,+ TableMetadata,+} from "@/types/proto/v1/database_service";++type ColumnOption = SelectOption & {+ label: string;+ value: string;+ column: ColumnMetadata;+};++const props = defineProps<{+ readonly?: boolean;+ db: ComposedDatabase;+ database: DatabaseMetadata;+ schema: SchemaMetadata;+ table: TableMetadata;+ index: IndexMetadata;+}>();+defineEmits<{+ (event: "update:expressions", expressions: string[]): void;+}>();++const focused = ref(false);++const columnOptions = computed(() => {+ return props.table.columns.map<ColumnOption>((column) => ({+ label: column.name,+ value: column.name,+ column,+ }));+});++const style = computed(() => {+ const style: CSSProperties = {+ "--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--index-columns-select :deep(.n-base-selection) {+ --n-padding-multiple: 2px 16px 0px 2px !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--index-columns-select+ :deep(.n-base-selection .n-base-suffix) {+ right: 4px;+}+</style>diff --git a/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/OperationCell.vue b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/OperationCell.vuenew file mode 100644index 0000000000..584687e418--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/OperationCell.vue@@ -0,0 +1,25 @@+<template>+ <div class="flex justify-start items-center">+ <NPopconfirm @positive-click="$emit('drop')">+ <template #trigger>+ <MiniActionButton tag="div">+ <TrashIcon class="w-4 h-4" />+ </MiniActionButton>+ </template>+ <template #default> asdf </template>+ </NPopconfirm>+ </div>+</template>+<script lang="ts" setup>+import { TrashIcon } from "lucide-vue-next";+import { NPopconfirm } from "naive-ui";+import { MiniActionButton } from "@/components/v2";+import { IndexMetadata } from "@/types/proto/v1/database_service";++defineProps<{+ index: IndexMetadata;+}>();+defineEmits<{+ (event: "drop"): void;+}>();+</script>diff --git a/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/index.ts b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/index.tsnew file mode 100644index 0000000000..4bf7658e0c--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/components/index.ts@@ -0,0 +1,4 @@+import ColumnsCell from "./ColumnsCell.vue";+import OperationCell from "./OperationCell.vue";++export { ColumnsCell, OperationCell };diff --git a/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/index.ts b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/index.tsnew file mode 100644index 0000000000..7cbc05f093--- /dev/null+++ b/frontend/src/components/SchemaEditorLite/Panels/IndexesEditor/index.ts@@ -0,0 +1,3 @@+import IndexesEditor from "./IndexesEditor.vue";++export default IndexesEditor;diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vueindex ee8dc96f67..90d0db5f93 100644--- a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue+++ b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue@@ -1,5 +1,6 @@<template><div+ v-show="show"ref="containerElRef"class="w-full h-full overflow-x-auto":data-height="containerHeight"@@ -113,6 +114,7 @@ interface LocalState {const props = withDefaults(defineProps<{+ show?: boolean;readonly: boolean;showForeignKey?: boolean;db: ComposedDatabase;@@ -129,6 +131,7 @@ const props = withDefaults(getColumnItemComputedClassList?: (column: ColumnMetadata) => string;}>(),{+ show: true,showForeignKey: true,disableChangeTable: false,allowReorderColumns: false,diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vueindex d2e73b3b2b..8dbb6e2c89 100644--- a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue+++ b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue@@ -1,51 +1,77 @@<template>- <div class="flex flex-col pt-2 gap-y-2 w-full h-full overflow-y-hidden">- <div- v-if="!readonly || selectionEnabled"- class="w-full flex flex-row justify-between items-center"- >- <div- v-if="!readonly"- class="w-full flex justify-start items-center space-x-2"- >- <NButton- size="small"- :disabled="disableChangeTable"- @click="handleAddColumn"- >- <heroicons-outline:plus class="w-4 h-auto mr-1 text-gray-400" />- {{ $t("schema-editor.actions.add-column") }}- </NButton>- <NButton- size="small"- :disabled="disableChangeTable"- @click="state.showSchemaTemplateDrawer = true"- >- <FeatureBadge feature="bb.feature.schema-template" />- <heroicons-outline:plus class="w-4 h-auto mr-1 text-gray-400" />- {{ $t("schema-editor.actions.add-from-template") }}- </NButton>+ <div+ class="flex flex-col pt-2 gap-y-2 w-full h-full overflow-y-hidden px-1 -mx-1"+ >+ <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'">+ <NButton size="small" @click="state.mode = 'COLUMNS'">+ <ArrowLeftIcon class="w-4 h-4" />+ </NButton>+ <template v-if="!readonly">+ <NButton size="small" @click="handleAddIndex">+ <PlusIcon class="w-4 h-4 mr-1" />+ {{ $t("common.add") }}+ </NButton>+ </template>+ </template>+ <template v-if="state.mode === 'COLUMNS'">+ <template v-if="!readonly">+ <NButton+ size="small"+ :disabled="disableChangeTable"+ @click="handleAddColumn"+ >+ <PlusIcon class="w-4 h-auto mr-1 text-gray-400" />+ {{ $t("schema-editor.actions.add-column") }}+ </NButton>+ <NButton+ size="small"+ :disabled="disableChangeTable"+ @click="state.showSchemaTemplateDrawer = true"+ >+ <FeatureBadge feature="bb.feature.schema-template" />+ <PlusIcon class="w-4 h-auto mr-1 text-gray-400" />+ {{ $t("schema-editor.actions.add-from-template") }}+ </NButton>+ </template>+ <NButton+ size="small"+ :disabled="disableChangeTable"+ @click="state.mode = 'INDEXES'"+ >+ <IndexIcon class="mr-1" />+ {{+ readonly+ ? $t("schema-editor.index.indexes")+ : $t("schema-editor.index.edit-indexes")+ }}+ </NButton>+ </template></div>- <div- v-if="selectionEnabled"- class="text-sm flex flex-row items-center gap-x-2 h-[28px] whitespace-nowrap"- >- <span class="text-main">- {{ $t("branch.select-tables-to-rollout") }}- </span>- <ColumnSelectionSummary- :db="db"- :metadata="{- database,- schema,- table,- }"- />+ <div class="text-sm flex flex-row items-center justify-end gap-x-2">+ <div+ v-if="selectionEnabled"+ class="text-sm flex flex-row items-center gap-x-2 h-[28px] whitespace-nowrap"+ >+ <span class="text-main">+ {{ $t("branch.select-tables-to-rollout") }}+ </span>+ <ColumnSelectionSummary+ :db="db"+ :metadata="{+ database,+ schema,+ table,+ }"+ />+ </div></div></div><div class="flex-1 overflow-y-hidden"><TableColumnEditor+ :show="state.mode === 'COLUMNS'":readonly="readonly":show-foreign-key="true":db="db"@@ -66,6 +92,15 @@@foreign-key-edit="handleEditColumnForeignKey"@foreign-key-click="gotoForeignKeyReferencedTable"/>+ <IndexesEditor+ :show="state.mode === 'INDEXES'"+ :readonly="readonly"+ :db="db"+ :database="database"+ :schema="schema"+ :table="table"+ @update="markTableStatus('updated')"+ /></div></div>@@ -103,8 +138,11 @@<script lang="ts" setup>import { cloneDeep, pull } from "lodash-es";+import { PlusIcon } from "lucide-vue-next";+import { ArrowLeftIcon } from "lucide-vue-next";import { computed, reactive, ref } from "vue";import { useI18n } from "vue-i18n";+import { IndexIcon } from "@/components/Icon";import { Drawer, DrawerContent } from "@/components/v2";import { hasFeature, pushNotification } from "@/store/modules";import { ComposedDatabase } from "@/types";@@ -112,11 +150,16 @@ import {ColumnMetadata,DatabaseMetadata,ForeignKeyMetadata,+ IndexMetadata,SchemaMetadata,TableMetadata,} from "@/types/proto/v1/database_service";import { SchemaTemplateSetting_FieldTemplate } from "@/types/proto/v1/setting_service";-import { arraySwap, instanceV1AllowsReorderColumns } from "@/utils";+import {+ arraySwap,+ instanceV1AllowsReorderColumns,+ randomString,+} from "@/utils";import FieldTemplates from "@/views/SchemaTemplate/FieldTemplates.vue";import EditColumnForeignKeyModal from "../Modals/EditColumnForeignKeyModal.vue";import { useSchemaEditorContext } from "../context";@@ -127,8 +170,11 @@ import {} from "../edit";import { EditStatus } from "../types";import ColumnSelectionSummary from "./ColumnSelectionSummary.vue";+import IndexesEditor from "./IndexesEditor";import TableColumnEditor from "./TableColumnEditor";+type EditMode = "COLUMNS" | "INDEXES";+const props = withDefaults(defineProps<{db: ComposedDatabase;@@ -143,6 +189,7 @@ const props = withDefaults();interface LocalState {+ mode: EditMode;showEditColumnForeignKeyModal: boolean;showSchemaTemplateDrawer: boolean;showFeatureModal: boolean;@@ -167,6 +214,7 @@ const engine = computed(() => {return props.db.instanceEntity.engine;});const state = reactive<LocalState>({+ mode: "COLUMNS",showEditColumnForeignKeyModal: false,showSchemaTemplateDrawer: false,showFeatureModal: false,@@ -308,6 +356,15 @@ const handleApplyColumnTemplate = (openFirstChild: false,});};+const handleAddIndex = () => {+ // eslint-disable-next-line vue/no-mutating-props+ props.table.indexes.push(+ IndexMetadata.fromPartial({+ name: `${props.table.name}-index-${randomString(8).toLowerCase()}`,+ })+ );+ markTableStatus("updated");+};const gotoForeignKeyReferencedTable = (column: ColumnMetadata,@@ -416,4 +473,23 @@ const handleReorderColumn = (if (target >= columns.length) return;arraySwap(columns, index, target);};++const markTableStatus = (status: EditStatus) => {+ const oldStatus = statusForTable();+ if (+ (oldStatus === "created" || oldStatus === "dropped") &&+ status === "updated"+ ) {+ return;+ }+ markEditStatus(+ props.db,+ {+ database: props.database,+ schema: props.schema,+ table: props.table,+ },+ status+ );+};</script>diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 3c38eda317..a88601b16e 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -2363,7 +2363,12 @@"edit-foreign-key": "Edit foreign key","select-reference-schema": "Select a schema to reference to","select-reference-table": "Select a table to reference to",- "select-reference-column": "Select a column to reference to"+ "select-reference-column": "Select a column to reference to",+ "index": {+ "indexes": "Indexes",+ "edit-indexes": "Edit indexes",+ "unique": "Unique"+ }},"label": {"empty-label-value": "Empty",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 9a91a31713..2e076e0794 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -2359,7 +2359,12 @@"edit-foreign-key": "Editar clave externa","select-reference-schema": "Seleccionar un esquema de referencia","select-reference-table": "Seleccionar una tabla de referencia",- "select-reference-column": "Seleccionar una columna de referencia"+ "select-reference-column": "Seleccionar una columna de referencia",+ "index": {+ "indexes": "Índices",+ "edit-indexes": "Editar índices",+ "unique": "Único"+ }},"label": {"empty-label-value": "Valor",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex 052c9aabdb..bc04f52aac 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -2359,7 +2359,12 @@"edit-foreign-key": "修改外键","select-reference-schema": "选择外键的 Schema","select-reference-table": "选择外键的表",- "select-reference-column": "选择外键的列"+ "select-reference-column": "选择外键的列",+ "index": {+ "indexes": "索引",+ "edit-indexes": "编辑索引",+ "unique": "唯一"+ }},"label": {"empty-label-value": "空",