Skip to content

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

feat: export schema in sdl format (#17841)

p0ny50bc95bb1c3d
diff --git a/frontend/src/components/DatabaseDetail/ExportSchemaButton.vue b/frontend/src/components/DatabaseDetail/ExportSchemaButton.vue
new file mode 100644
index 0000000000..10b9e4612c
--- /dev/null
+++ b/frontend/src/components/DatabaseDetail/ExportSchemaButton.vue
@@ -0,0 +1,121 @@
+<template>
+ <div v-if="available">
+ <NDropdown
+ :options="exportOptions"
+ @select="handleExportFormat"
+ trigger="click"
+ >
+ <NButton :loading="exporting">
+ {{ $t("database.export-schema") }}
+ </NButton>
+ </NDropdown>
+ </div>
+</template>
+
+<script setup lang="ts">
+import { create } from "@bufbuild/protobuf";
+import { ConnectError } from "@connectrpc/connect";
+import { NButton, NDropdown } from "naive-ui";
+import { computed, ref } from "vue";
+import { useI18n } from "vue-i18n";
+import { databaseServiceClientConnect } from "@/grpcweb";
+import { pushNotification } from "@/store";
+import { isValidDatabaseName } from "@/types";
+import type { ComposedDatabase } from "@/types";
+import {
+ GetDatabaseSDLSchemaRequestSchema,
+ GetDatabaseSDLSchemaRequest_SDLFormat,
+} from "@/types/proto-es/v1/database_service_pb";
+import { hasProjectPermissionV2 } from "@/utils";
+
+const props = defineProps<{
+ database: ComposedDatabase;
+}>();
+
+const exporting = ref(false);
+const { t } = useI18n();
+
+const available = computed(() => {
+ if (!isValidDatabaseName(props.database.name)) {
+ return false;
+ }
+
+ return hasProjectPermissionV2(
+ props.database.projectEntity,
+ "bb.databases.getSchema"
+ );
+});
+
+const exportOptions = computed(() => [
+ {
+ label: t("database.export-schema-single-file"),
+ key: "single",
+ },
+ {
+ label: t("database.export-schema-multi-file"),
+ key: "multi",
+ },
+]);
+
+const handleExportFormat = (key: string) => {
+ if (key === "single") {
+ exportSchema(GetDatabaseSDLSchemaRequest_SDLFormat.SINGLE_FILE);
+ } else if (key === "multi") {
+ exportSchema(GetDatabaseSDLSchemaRequest_SDLFormat.MULTI_FILE);
+ }
+};
+
+const exportSchema = async (format: GetDatabaseSDLSchemaRequest_SDLFormat) => {
+ exporting.value = true;
+
+ try {
+ const request = create(GetDatabaseSDLSchemaRequestSchema, {
+ name: `${props.database.name}/sdlSchema`,
+ format,
+ });
+
+ const response =
+ await databaseServiceClientConnect.getDatabaseSDLSchema(request);
+
+ let filename: string;
+ let blob: Blob;
+
+ // Check the content type to determine how to handle the response
+ if (response.contentType.includes("application/zip")) {
+ // Multi-file format - ZIP archive
+ filename = `${props.database.databaseName}_schema.zip`;
+ // Create a proper ArrayBuffer by slicing
+ const arrayBuffer = response.schema.slice().buffer;
+ blob = new Blob([arrayBuffer], { type: "application/zip" });
+ } else {
+ // Single file format - text file
+ filename = `${props.database.databaseName}_schema.sql`;
+ const content = new TextDecoder().decode(response.schema);
+ blob = new Blob([content], { type: "text/plain" });
+ }
+
+ const downloadLink = document.createElement("a");
+ downloadLink.href = URL.createObjectURL(blob);
+ downloadLink.download = filename;
+ document.body.appendChild(downloadLink);
+ downloadLink.click();
+ document.body.removeChild(downloadLink);
+ URL.revokeObjectURL(downloadLink.href);
+
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("database.successfully-exported-schema"),
+ });
+ } catch (error) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("database.failed-to-export-schema"),
+ description: (error as ConnectError).message,
+ });
+ } finally {
+ exporting.value = false;
+ }
+};
+</script>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 2032da5d3c..dcecf4257c 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1596,6 +1596,11 @@
"no-environment": "This database has not been bound to the environment",
"set-environment": "Set environment",
"sync-database": "Sync database",
+ "export-schema": "Export Schema",
+ "export-schema-single-file": "Single File",
+ "export-schema-multi-file": "Multi-File (ZIP)",
+ "successfully-exported-schema": "Successfully exported schema",
+ "failed-to-export-schema": "Failed to export schema",
"select": "Select database",
"sync-status": "Sync status",
"last-successful-sync": "Last successful sync",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index d0d3c23d18..5949155a2f 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1596,6 +1596,11 @@
"no-environment": "Esta base de datos no ha sido vinculada al entorno.",
"set-environment": "Establecer entorno",
"sync-database": "Sincronizar base de datos",
+ "export-schema": "Exportar esquema",
+ "export-schema-single-file": "Archivo único",
+ "export-schema-multi-file": "Multi-archivo (ZIP)",
+ "successfully-exported-schema": "Esquema exportado con éxito",
+ "failed-to-export-schema": "Error al exportar el esquema",
"select": "Seleccionar base de datos",
"sync-status": "Estado de sincronización",
"last-successful-sync": "Última sincronización exitosa",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index dc02bfaef9..33def78570 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -1596,6 +1596,11 @@
"no-environment": "このデータベースは環境にバインドされていません",
"set-environment": "環境を設定する",
"sync-database": "データベースを同期",
+ "export-schema": "スキーマをエクスポート",
+ "export-schema-single-file": "単一ファイル",
+ "export-schema-multi-file": "複数ファイル (ZIP)",
+ "successfully-exported-schema": "スキーマのエクスポートに成功しました",
+ "failed-to-export-schema": "スキーマのエクスポートに失敗しました",
"select": "データベースの選択",
"sync-status": "同期ステータス",
"last-successful-sync": "最後に成功した同期",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index d2f7b9c1b0..ab6648a9ff 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -1596,6 +1596,11 @@
"no-environment": "Cơ sở dữ liệu này không bị ràng buộc với môi trường",
"set-environment": "Thiết lập môi trường",
"sync-database": "Đồng bộ cơ sở dữ liệu",
+ "export-schema": "Xuất schema",
+ "export-schema-single-file": "Tệp đơn",
+ "export-schema-multi-file": "Nhiều tệp (ZIP)",
+ "successfully-exported-schema": "Đã xuất schema thành công",
+ "failed-to-export-schema": "Không thể xuất schema",
"select": "Chọn cơ sở dữ liệu",
"sync-status": "Trạng thái đồng bộ",
"last-successful-sync": "Lần đồng bộ thành công cuối cùng",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index cbb409b510..ea56c39ed1 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1596,6 +1596,11 @@
"no-environment": "该数据库尚未绑定到环境",
"set-environment": "设置环境",
"sync-database": "同步数据库",
+ "export-schema": "导出 Schema",
+ "export-schema-single-file": "单文件",
+ "export-schema-multi-file": "多文件 (ZIP)",
+ "successfully-exported-schema": "成功导出 Schema",
+ "failed-to-export-schema": "导出 Schema 失败",
"select": "选择数据库",
"sync-status": "同步状态",
"last-successful-sync": "最后一次成功的同步",
diff --git a/frontend/src/views/DatabaseDetail/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
index 9d61af8445..22bbe428d3 100644
--- a/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail/DatabaseDetail.vue
@@ -92,6 +92,7 @@
:text="false"
:database="database"
/>
+ <ExportSchemaButton :database="database" />
<NButton
v-if="allowTransferDatabase"
@click.prevent="tryTransferProject"
@@ -210,6 +211,7 @@ import {
SchemaDiagramButton,
} from "@/components/DatabaseDetail";
import DriftedDatabaseAlert from "@/components/DatabaseDetail/DriftedDatabaseAlert.vue";
+import ExportSchemaButton from "@/components/DatabaseDetail/ExportSchemaButton.vue";
import SyncDatabaseButton from "@/components/DatabaseDetail/SyncDatabaseButton.vue";
import TransferOutDatabaseForm from "@/components/TransferOutDatabaseForm";
import { Drawer } from "@/components/v2";