Skip to content

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

chore: stringify IndexMetadata (#9861)

boojackebc2a1cd4676
diff --git a/backend/api/v1/schema_design_transformer.go b/backend/api/v1/schema_design_transformer.go
index d32c0a0674..b6ccf8208f 100644
--- a/backend/api/v1/schema_design_transformer.go
+++ b/backend/api/v1/schema_design_transformer.go
@@ -176,19 +176,6 @@ type tableState struct {
comment string
}
-func (t *tableState) removeUnsupportedIndex() {
- unsupported := []string{}
- for name, index := range t.indexes {
- if index.primary {
- continue
- }
- unsupported = append(unsupported, name)
- }
- for _, name := range unsupported {
- delete(t.indexes, name)
- }
-}
-
func (t *tableState) toString(buf *strings.Builder) error {
if _, err := buf.WriteString(fmt.Sprintf("CREATE TABLE `%s` (\n ", t.name)); err != nil {
return err
@@ -212,7 +199,6 @@ func (t *tableState) toString(buf *strings.Builder) error {
}
indexes := []*indexState{}
- t.removeUnsupportedIndex()
for _, index := range t.indexes {
indexes = append(indexes, index)
}
@@ -468,8 +454,34 @@ func (i *indexState) toString(buf *strings.Builder) error {
if _, err := buf.WriteString(")"); err != nil {
return err
}
+ } else {
+ if i.unique {
+ if _, err := buf.WriteString("UNIQUE INDEX "); err != nil {
+ return err
+ }
+ } else {
+ if _, err := buf.WriteString("INDEX "); err != nil {
+ return err
+ }
+ }
+
+ if _, err := buf.WriteString(fmt.Sprintf("`%s` (", i.name)); err != nil {
+ return err
+ }
+ for j, key := range i.keys {
+ if j > 0 {
+ if _, err := buf.WriteString(", "); err != nil {
+ return err
+ }
+ }
+ if _, err := buf.WriteString(fmt.Sprintf("`%s`", key)); err != nil {
+ return err
+ }
+ }
+ if _, err := buf.WriteString(")"); err != nil {
+ return err
+ }
}
- // TODO: support other type indexes.
return nil
}
diff --git a/backend/plugin/schema-engine/pg/structurer.go b/backend/plugin/schema-engine/pg/structurer.go
index 3a24b810b0..187586548c 100644
--- a/backend/plugin/schema-engine/pg/structurer.go
+++ b/backend/plugin/schema-engine/pg/structurer.go
@@ -353,19 +353,6 @@ func (t *tableState) commentToString(buf *strings.Builder, schemaName string) er
return nil
}
-func (t *tableState) removeUnsupportedIndex() {
- unsupported := []string{}
- for name, index := range t.indexes {
- if index.primary {
- continue
- }
- unsupported = append(unsupported, name)
- }
- for _, name := range unsupported {
- delete(t.indexes, name)
- }
-}
-
func (t *tableState) toString(buf *strings.Builder, schemaName string) error {
if !t.ignoreTable {
if _, err := buf.WriteString(fmt.Sprintf("CREATE TABLE \"%s\".\"%s\" (\n ", schemaName, t.name)); err != nil {
@@ -394,7 +381,6 @@ func (t *tableState) toString(buf *strings.Builder, schemaName string) error {
}
indexes := []*indexState{}
- t.removeUnsupportedIndex()
for _, index := range t.indexes {
indexes = append(indexes, index)
}
@@ -660,8 +646,35 @@ func (i *indexState) toString(buf *strings.Builder, schemaName, tableName string
if _, err := buf.WriteString(");\n"); err != nil {
return err
}
+ } else {
+ indexType := "INDEX"
+ if i.unique {
+ indexType = "UNIQUE INDEX"
+ }
+
+ if _, err := buf.WriteString(fmt.Sprintf("CREATE %s \"%s\".\"%s\" ON \"%s\".\"%s\" (", indexType, schemaName, i.name, schemaName, tableName)); err != nil {
+ return err
+ }
+ for j, key := range i.keys {
+ if j > 0 {
+ if _, err := buf.WriteString(", "); err != nil {
+ return err
+ }
+ }
+ if _, err := buf.WriteString("\""); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(key); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString("\""); err != nil {
+ return err
+ }
+ }
+ if _, err := buf.WriteString(");\n"); err != nil {
+ return err
+ }
}
- // TODO: support other type indexes.
return nil
}
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index ed473b4cc2..4633e33754 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2278,7 +2278,8 @@
"executing-query": "Executing query",
"grouping": "Grouping",
"previous-row": "Previous row",
- "next-row": "Next row"
+ "next-row": "Next row",
+ "copy-success": "Copy success"
},
"schema-editor": {
"self": "Schema Editor",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 989338e4b6..6416cc130f 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2274,7 +2274,8 @@
"executing-query": "Ejecutando consulta",
"grouping": "Agrupamiento",
"previous-row": "Fila anterior",
- "next-row": "siguiente fila"
+ "next-row": "siguiente fila",
+ "copy-success": "Copiado al portapapeles"
},
"schema-editor": {
"self": "Editor de esquema",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 4679c9f52a..faa4686f7a 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2273,7 +2273,8 @@
"executing-query": "执行查询",
"grouping": "分组",
"previous-row": "上一行",
- "next-row": "下一行"
+ "next-row": "下一行",
+ "copy-success": "复制成功"
},
"schema-editor": {
"self": "Schema 编辑器",
diff --git a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/StringifyMetadataButton.vue b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/StringifyMetadataButton.vue
index de04fc0689..e764f9b5ea 100644
--- a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/StringifyMetadataButton.vue
+++ b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/StringifyMetadataButton.vue
@@ -6,30 +6,56 @@
>
<template #trigger>
<NButton quaternary size="tiny" class="!px-1" v-bind="$attrs">
- <Code class="w-4 h-4" />
+ <CodeIcon class="w-4 h-4" />
</NButton>
</template>
<div class="w-[32rem] h-[20rem] flex flex-col justify-center items-center">
<BBSpin v-if="isFetching" />
- <MonacoEditor
- v-else
- :content="schemaString || ''"
- :readonly="true"
- class="border w-full h-full"
- />
+ <template v-else>
+ <div
+ class="w-full flex flex-row justify-between items-center gap-x-2 mb-2"
+ >
+ <div class="textlabel flex-1 truncate">
+ {{ resourceName }}
+ </div>
+ <div class="flex flex-row justify-end items-center">
+ <NTooltip>
+ <template #trigger>
+ <NButton
+ quaternary
+ size="tiny"
+ class="!px-1"
+ @click="handleCopySchemaString"
+ >
+ <ClipboardIcon class="w-4 h-4" />
+ </NButton>
+ </template>
+ {{ $t("common.copy") }}
+ </NTooltip>
+ </div>
+ </div>
+ <MonacoEditor
+ :content="schemaString || ''"
+ :readonly="true"
+ class="border w-full h-full"
+ />
+ </template>
</div>
</NPopover>
</template>
<script lang="ts" setup>
+import { useClipboard } from "@vueuse/core";
import { cloneDeep } from "lodash-es";
-import { Code } from "lucide-vue-next";
-import { NButton, NPopover } from "naive-ui";
-import { onMounted, ref } from "vue";
+import { CodeIcon, ClipboardIcon } from "lucide-vue-next";
+import { NButton, NPopover, NTooltip } from "naive-ui";
+import { onMounted, ref, computed } from "vue";
+import { useI18n } from "vue-i18n";
import { MonacoEditor } from "@/components/MonacoEditor";
import { sqlServiceClient } from "@/grpcweb";
-import { useDBSchemaV1Store } from "@/store";
+import { pushNotification, useDBSchemaV1Store } from "@/store";
import type { ComposedDatabase } from "@/types";
+import { Engine } from "@/types/proto/v1/common";
import {
DatabaseMetadata,
SchemaMetadata,
@@ -42,10 +68,42 @@ const props = defineProps<{
table?: TableMetadata;
}>();
+const { t } = useI18n();
const dbSchemaStore = useDBSchemaV1Store();
+const { copy: copyTextToClipboard } = useClipboard();
const schemaString = ref<string | null>(null);
const isFetching = ref<boolean>(false);
+const engine = computed(() => {
+ return props.database.instanceEntity.engine;
+});
+
+const resourceName = computed(() => {
+ if (props.table) {
+ if (hasSchemaProperty.value) {
+ return `${props.schema?.name}.${props.table.name}`;
+ } else {
+ return props.table.name;
+ }
+ }
+ if (props.schema) {
+ return props.schema.name;
+ }
+ return props.database.name;
+});
+
+const hasSchemaProperty = computed(() => {
+ return (
+ engine.value === Engine.POSTGRES ||
+ engine.value === Engine.SNOWFLAKE ||
+ engine.value === Engine.ORACLE ||
+ engine.value === Engine.DM ||
+ engine.value === Engine.MSSQL ||
+ engine.value === Engine.REDSHIFT ||
+ engine.value === Engine.RISINGWAVE
+ );
+});
+
onMounted(async () => {
await dbSchemaStore.getOrFetchDatabaseMetadata({
database: props.database.name,
@@ -53,6 +111,15 @@ onMounted(async () => {
});
});
+const handleCopySchemaString = () => {
+ copyTextToClipboard(schemaString.value || "");
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("sql-editor.copy-success"),
+ });
+};
+
const handleShowSchemaString = async () => {
const databaseMetadata = DatabaseMetadata.fromPartial({
name: props.database.name,
@@ -75,7 +142,7 @@ const handleShowSchemaString = async () => {
isFetching.value = true;
const { schema } = await sqlServiceClient.stringifyMetadata({
metadata: databaseMetadata,
- engine: props.database.instanceEntity.engine,
+ engine: engine.value,
});
schemaString.value = schema.trim();
isFetching.value = false;