Skip to content

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

chore: support table template in schema editor (#8378)

ecmadaoa923a071569f
diff --git a/frontend/src/components/Branch/utils.ts b/frontend/src/components/Branch/utils.ts
index 153a2c03de..5763de7144 100644
--- a/frontend/src/components/Branch/utils.ts
+++ b/frontend/src/components/Branch/utils.ts
@@ -411,14 +411,7 @@ export const rebuildEditableSchemas = (
(item) => item.name === schema.name
);
if (!editableSchema) {
- const newSchema = convertSchemaMetadataToSchema(schema);
- newSchema.status = "created";
- for (const table of newSchema.tableList) {
- table.status = "created";
- for (const column of table.columnList) {
- column.status = "created";
- }
- }
+ const newSchema = convertSchemaMetadataToSchema(schema, "created");
editableSchemas.push(newSchema);
}
}
diff --git a/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue b/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
index 270e58eedd..a3d6c08b45 100644
--- a/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
@@ -79,9 +79,9 @@ const handleConfirmButtonClick = async () => {
const schema = convertSchemaMetadataToSchema(
SchemaMetadata.fromPartial({
name: state.schemaName,
- })
+ }),
+ "created"
);
- schema.status = "created";
parentResource.schemaList.push(schema);
// TODO(steven): Open the schema tab.
dismissModal();
diff --git a/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue b/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
index 5ca1b7299a..fe09d5f81b 100644
--- a/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
@@ -44,7 +44,6 @@ import {
} from "@/types/proto/v1/database_service";
import {
SchemaEditorTabType,
- convertColumnMetadataToColumn,
convertTableMetadataToTable,
} from "@/types/v1/schemaEditor";
@@ -116,8 +115,6 @@ const handleConfirmButtonClick = async () => {
}
if (isCreatingTable.value) {
- const table = TableMetadata.fromPartial({});
- table.name = state.tableName;
const column = ColumnMetadata.fromPartial({});
column.name = "id";
if (engine.value === Engine.POSTGRES) {
@@ -126,12 +123,14 @@ const handleConfirmButtonClick = async () => {
column.type = "INT";
}
column.comment = "ID";
- const columnEdit = convertColumnMetadataToColumn(column);
- columnEdit.status = "created";
- const tableEdit = convertTableMetadataToTable(table);
- tableEdit.status = "created";
- tableEdit.columnList.push(columnEdit);
- tableEdit.primaryKey.columnIdList.push(columnEdit.id);
+ const table = TableMetadata.fromPartial({
+ name: state.tableName,
+ columns: [column],
+ });
+ const tableEdit = convertTableMetadataToTable(table, "created");
+ tableEdit.primaryKey.columnIdList.push(
+ ...tableEdit.columnList.map((col) => col.id)
+ );
schema.tableList.push(tableEdit);
schemaEditorV1Store.addTab({
id: generateUniqueTabId(),
diff --git a/frontend/src/components/SchemaEditorV1/Panels/DatabaseEditor.vue b/frontend/src/components/SchemaEditorV1/Panels/DatabaseEditor.vue
index fd652fd004..e05d98689c 100644
--- a/frontend/src/components/SchemaEditorV1/Panels/DatabaseEditor.vue
+++ b/frontend/src/components/SchemaEditorV1/Panels/DatabaseEditor.vue
@@ -6,10 +6,10 @@
v-if="state.selectedSubtab === 'table-list'"
class="w-full flex justify-between items-center space-x-2"
>
- <div class="flex flex-row justify-start items-center">
+ <div class="flex flex-row justify-start items-center space-x-3">
<div
v-if="shouldShowSchemaSelector"
- class="ml-2 flex flex-row justify-start items-center mr-3 text-sm"
+ class="ml-2 flex flex-row justify-start items-center text-sm"
>
<span class="mr-1">Schema:</span>
<n-select
@@ -26,6 +26,15 @@
<heroicons-outline:plus class="w-4 h-auto mr-1 text-gray-400" />
{{ $t("schema-editor.actions.create-table") }}
</button>
+ <button
+ class="flex flex-row justify-center items-center border px-3 py-1 leading-6 rounded text-sm hover:opacity-80 disabled:cursor-not-allowed disabled:opacity-60"
+ :disabled="!allowCreateTable"
+ @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") }}
+ </button>
</div>
</div>
</div>
@@ -167,6 +176,21 @@
:table-name="state.tableNameModalContext.tableName"
@close="state.tableNameModalContext = undefined"
/>
+
+ <Drawer
+ :show="state.showSchemaTemplateDrawer"
+ @close="state.showSchemaTemplateDrawer = false"
+ >
+ <DrawerContent :title="$t('schema-template.table-template.self')">
+ <div class="w-[calc(100vw-36rem)] min-w-[64rem] max-w-[calc(100vw-8rem)]">
+ <TableTemplates
+ :engine="databaseEngine"
+ :readonly="true"
+ @apply="handleApplyTemplate"
+ />
+ </div>
+ </DrawerContent>
+ </Drawer>
</template>
<script lang="ts" setup>
@@ -176,6 +200,7 @@ import scrollIntoView from "scroll-into-view-if-needed";
import { computed, nextTick, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { SchemaDiagram, SchemaDiagramIcon } from "@/components/SchemaDiagram";
+import { Drawer, DrawerContent } from "@/components/v2";
import {
generateUniqueTabId,
useDatabaseV1Store,
@@ -187,13 +212,16 @@ import {
SchemaMetadata,
TableMetadata,
} from "@/types/proto/v1/database_service";
+import { SchemaTemplateSetting_TableTemplate } from "@/types/proto/v1/setting_service";
import {
Table,
DatabaseTabContext,
DatabaseSchema,
SchemaEditorTabType,
+ convertTableMetadataToTable,
} from "@/types/v1/schemaEditor";
import { bytesToString } from "@/utils";
+import TableTemplates from "@/views/SchemaTemplate/TableTemplates.vue";
import TableNameModal from "../Modals/TableNameModal.vue";
import { useMetadataForDiagram } from "../utils/useMetadataForDiagram";
@@ -204,6 +232,7 @@ interface LocalState {
selectedSchemaId: string;
isFetchingDDL: boolean;
statement: string;
+ showSchemaTemplateDrawer: boolean;
tableNameModalContext?: {
parentName: string;
schemaId: string;
@@ -220,6 +249,7 @@ const state = reactive<LocalState>({
selectedSchemaId: "",
isFetchingDDL: false,
statement: "",
+ showSchemaTemplateDrawer: false,
});
const databaseSchema = computed(() => {
return editorStore.resourceMap["database"].get(
@@ -411,6 +441,27 @@ const tryEditColumn = async (
}
}
};
+
+const handleApplyTemplate = (template: SchemaTemplateSetting_TableTemplate) => {
+ if (template.table) {
+ const tableEdit = convertTableMetadataToTable(template.table, "created");
+
+ const selectedSchema = schemaList.value.find(
+ (schema) => schema.id === state.selectedSchemaId
+ );
+ if (selectedSchema) {
+ selectedSchema.tableList.push(tableEdit);
+ editorStore.addTab({
+ id: generateUniqueTabId(),
+ type: SchemaEditorTabType.TabForTable,
+ parentName: database.value.name,
+ schemaId: state.selectedSchemaId,
+ tableId: tableEdit.id,
+ });
+ }
+ }
+ state.showSchemaTemplateDrawer = false;
+};
</script>
<style scoped>
diff --git a/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
index ba777527ee..d39939adf4 100644
--- a/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
@@ -258,8 +258,10 @@ const setColumnPrimaryKey = (column: Column, isPrimaryKey: boolean) => {
};
const handleAddColumn = () => {
- const column = convertColumnMetadataToColumn(ColumnMetadata.fromPartial({}));
- column.status = "created";
+ const column = convertColumnMetadataToColumn(
+ ColumnMetadata.fromPartial({}),
+ "created"
+ );
table.value.columnList.push(column);
nextTick(() => {
const container = document.querySelector("#table-editor-container");
@@ -281,8 +283,7 @@ const handleApplyColumnTemplate = (
if (template.engine !== engine.value || !template.column) {
return;
}
- const column = convertColumnMetadataToColumn(template.column);
- column.status = "created";
+ const column = convertColumnMetadataToColumn(template.column, "created");
table.value.columnList.push(column);
state.showSchemaTemplateDrawer = false;
};
diff --git a/frontend/src/components/SchemaEditorV1/utils/metadata.ts b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
index 22a3167b5a..789f5423bd 100644
--- a/frontend/src/components/SchemaEditorV1/utils/metadata.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
@@ -366,8 +366,7 @@ export const rebuildEditableSchemas = (
(item) => item.name === column.name
);
if (!editableColumn) {
- const newColumn = convertColumnMetadataToColumn(column);
- newColumn.status = "created";
+ const newColumn = convertColumnMetadataToColumn(column, "created");
editableTable.columnList.push(newColumn);
}
}
@@ -397,11 +396,7 @@ export const rebuildEditableSchemas = (
(item) => item.name === table.name
);
if (!editableTable) {
- const newTable = convertTableMetadataToTable(table);
- newTable.status = "created";
- for (const column of newTable.columnList) {
- column.status = "created";
- }
+ const newTable = convertTableMetadataToTable(table, "created");
editableSchema.tableList.push(newTable);
}
}
@@ -412,14 +407,7 @@ export const rebuildEditableSchemas = (
(item) => item.name === schema.name
);
if (!editableSchema) {
- const newSchema = convertSchemaMetadataToSchema(schema);
- newSchema.status = "created";
- for (const table of newSchema.tableList) {
- table.status = "created";
- for (const column of table.columnList) {
- column.status = "created";
- }
- }
+ const newSchema = convertSchemaMetadataToSchema(schema, "created");
editableSchemas.push(newSchema);
}
}
diff --git a/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts b/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts
index 849ea27639..35c45ea920 100644
--- a/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts
@@ -364,8 +364,7 @@ export const rebuildEditableSchemas = (
(item) => item.name === column.name
);
if (!editableColumn) {
- const newColumn = convertColumnMetadataToColumn(column);
- newColumn.status = "created";
+ const newColumn = convertColumnMetadataToColumn(column, "created");
editableTable.columnList.push(newColumn);
}
}
@@ -395,11 +394,7 @@ export const rebuildEditableSchemas = (
(item) => item.name === table.name
);
if (!editableTable) {
- const newTable = convertTableMetadataToTable(table);
- newTable.status = "created";
- for (const column of newTable.columnList) {
- column.status = "created";
- }
+ const newTable = convertTableMetadataToTable(table, "created");
editableSchema.tableList.push(newTable);
}
}
@@ -410,14 +405,7 @@ export const rebuildEditableSchemas = (
(item) => item.name === schema.name
);
if (!editableSchema) {
- const newSchema = convertSchemaMetadataToSchema(schema);
- newSchema.status = "created";
- for (const table of newSchema.tableList) {
- table.status = "created";
- for (const column of table.columnList) {
- column.status = "created";
- }
- }
+ const newSchema = convertSchemaMetadataToSchema(schema, "created");
editableSchemas.push(newSchema);
}
}
diff --git a/frontend/src/types/v1/schemaEditor/atomType.ts b/frontend/src/types/v1/schemaEditor/atomType.ts
index f6b3f8394d..068b27d376 100644
--- a/frontend/src/types/v1/schemaEditor/atomType.ts
+++ b/frontend/src/types/v1/schemaEditor/atomType.ts
@@ -116,7 +116,8 @@ export const convertTableMetadataToTable = (
};
export const convertSchemaMetadataToSchema = (
- schemaMetadata: SchemaMetadata
+ schemaMetadata: SchemaMetadata,
+ status: Status = "normal"
): Schema => {
const tableList: Table[] = [];
@@ -126,7 +127,7 @@ export const convertSchemaMetadataToSchema = (
continue;
}
- const table = convertTableMetadataToTable(tableMetadata);
+ const table = convertTableMetadataToTable(tableMetadata, status);
tableList.push(table);
}
@@ -134,7 +135,7 @@ export const convertSchemaMetadataToSchema = (
id: uuidv1(),
name: schemaMetadata.name,
tableList: tableList,
- status: "normal",
+ status,
};
};