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: import changelist via .sql or .zip file (#11846)
Liu Jia9537e5f15d7
diff --git a/frontend/src/components/Changelist/ChangelistDetail/NavBar/ExportButton.vue b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ExportButton.vueindex 4cb067358b..4843a20ac6 100644--- a/frontend/src/components/Changelist/ChangelistDetail/NavBar/ExportButton.vue+++ b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ExportButton.vue@@ -10,7 +10,7 @@@click="handleExport"><template #icon>- <heroicons:arrow-down-tray />+ <DownloadIcon class="w-4 h-4" /></template></ErrorTipsButton></template>@@ -19,6 +19,7 @@import dayjs from "dayjs";import saveAs from "file-saver";import JSZip from "jszip";+import { DownloadIcon } from "lucide-vue-next";import { computed, ref } from "vue";import { useI18n } from "vue-i18n";import { ErrorTipsButton } from "@/components/v2";diff --git a/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vuenew file mode 100644index 0000000000..023b93c0ea--- /dev/null+++ b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue@@ -0,0 +1,162 @@+<template>+ <NUpload+ abstract+ accept="application/x-zip,.zip,application/sql,.sql"+ @change="handleFileChange"+ >+ <NUploadTrigger #="{ handleClick }" abstract>+ <NTooltip>+ <template #trigger>+ <NButton icon style="--n-padding: 0 10px" @click="handleClick">+ <template #icon>+ <UploadIcon class="w-4 h-4" />+ </template>+ </NButton>+ </template>+ <template #default>+ <div class="whitespace-nowrap">+ {{ $t("changelist.import.upload-sql-or-zip-file") }}+ </div>+ </template>+ </NTooltip>+ </NUploadTrigger>+ </NUpload>+</template>++<script setup lang="ts">+import JSZip from "jszip";+import { orderBy } from "lodash-es";+import { UploadIcon } from "lucide-vue-next";+import {+ NButton,+ NTooltip,+ NUpload,+ NUploadTrigger,+ type UploadFileInfo,+} from "naive-ui";+import { useI18n } from "vue-i18n";+import { pushNotification, useChangelistStore, useSheetV1Store } from "@/store";+import {+ Changelist_Change as Change,+ Changelist,+} from "@/types/proto/v1/changelist_service";+import { Sheet } from "@/types/proto/v1/sheet_service";+import { defer, setSheetStatement } from "@/utils";+import { useChangelistDetailContext } from "../context";++type ParsedFile = {+ name: string;+ content: string;+};++const { t } = useI18n();+const { changelist, project, isUpdating } = useChangelistDetailContext();++const unzip = async (file: File) => {+ const zip = await JSZip.loadAsync(file);+ const files = orderBy(zip.files, (f) => f.name, "asc").filter(+ (f) => !f.dir && f.name.toLowerCase().endsWith(".sql")+ );+ const results = await Promise.all(+ files.map<Promise<ParsedFile>>(async (f) => {+ const content = await f.async("string");+ return {+ name: f.name,+ content,+ };+ })+ );+ return results;+};++const readFile = (file: File) => {+ const d = defer<string>();+ const fr = new FileReader();+ fr.addEventListener("load", (e) => {+ const result = fr.result;+ if (typeof result === "string") {+ d.resolve(result);+ return;+ }+ d.reject(new Error("Failed to read file content."));+ });+ fr.addEventListener("error", (e) => {+ d.reject(fr.error ?? new Error("Failed to read file content."));+ });+ fr.readAsText(file);+ return d.promise;+};++const handleFileChange = async (options: { file: UploadFileInfo }) => {+ const fileInfo = options.file;+ if (!fileInfo.file) {+ return;+ }++ const cleanup = () => {+ isUpdating.value = false;+ };++ try {+ isUpdating.value = true;++ const files: ParsedFile[] = [];+ if (fileInfo.name.toLowerCase().endsWith(".sql")) {+ const content = await readFile(fileInfo.file);+ files.push({+ name: fileInfo.file.name,+ content,+ });+ } else {+ const results = await unzip(fileInfo.file);+ files.push(...results);+ }++ if (files.length === 0) {+ pushNotification({+ module: "bytebase",+ style: "WARN",+ title: t("changelist.import.no-file-to-upload"),+ });+ return cleanup();+ }++ await save(files);++ pushNotification({+ module: "bytebase",+ style: "SUCCESS",+ title: t("common.updated"),+ });+ } finally {+ cleanup();+ }+};++const save = async (files: ParsedFile[]) => {+ const createdSheets = await Promise.all(+ files.map(async (f) => {+ const { name, content } = f;+ const sheet = Sheet.fromPartial({+ title: name,+ });+ setSheetStatement(sheet, content);+ const created = await useSheetV1Store().createSheet(+ project.value.name,+ sheet+ );+ return created;+ })+ );+ const newChanges = createdSheets.map((sheet) =>+ Change.fromPartial({+ sheet: sheet.name,+ })+ );+ const changelistPatch = Changelist.fromPartial({+ ...changelist.value,+ changes: [...changelist.value.changes, ...newChanges],+ });+ await useChangelistStore().patchChangelist(changelistPatch, ["changes"]);+};+</script>diff --git a/frontend/src/components/Changelist/ChangelistDetail/NavBar/NavBar.vue b/frontend/src/components/Changelist/ChangelistDetail/NavBar/NavBar.vueindex 27a97fa991..f5dee0f8e9 100644--- a/frontend/src/components/Changelist/ChangelistDetail/NavBar/NavBar.vue+++ b/frontend/src/components/Changelist/ChangelistDetail/NavBar/NavBar.vue@@ -7,6 +7,7 @@<template v-if="!reorderMode"><ReorderButton v-if="allowEdit" @click="beginReorder" /><ExportButton />+ <ImportButton v-if="allowEdit" /><AddChangeButton v-if="allowEdit" /><ApplyToDatabaseButton v-if="allowApply" /></template>@@ -29,6 +30,7 @@ import { useChangelistDetailContext } from "../context";import AddChangeButton from "./AddChangeButton.vue";import ApplyToDatabaseButton from "./ApplyToDatabaseButton.vue";import ExportButton from "./ExportButton.vue";+import ImportButton from "./ImportButton.vue";import ReorderButton from "./ReorderButton.vue";import TitleEditor from "./TitleEditor.vue";import { useReorderChangelist } from "./reorder";diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 44282dd34d..2bed04d200 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -3384,7 +3384,11 @@},"confirm-remove-change": "Remove this change?","delete-this-changelist": "Delete this changelist",- "confirm-delete-changelist": "Delete this changelist?"+ "confirm-delete-changelist": "Delete this changelist?",+ "import": {+ "no-file-to-upload": "No file to upload",+ "upload-sql-or-zip-file": "Upload .sql or .zip file"+ }},"export-data": {"export-rows": "Export rows",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex 1ea49ab7e1..ca73a3178a 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -3384,7 +3384,11 @@},"confirm-remove-change": "¿Eliminar este cambio?","delete-this-changelist": "Eliminar esta lista de cambios",- "confirm-delete-changelist": "¿Eliminar esta lista de cambios?"+ "confirm-delete-changelist": "¿Eliminar esta lista de cambios?",+ "import": {+ "no-file-to-upload": "No hay archivos para subir",+ "upload-sql-or-zip-file": "Cargue el archivo .sql o .zip"+ }},"export-data": {"export-rows": "Exportar filas",diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 1b939c8125..2c1a2f2805 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -3384,7 +3384,11 @@},"confirm-remove-change": "この変更を削除しますか?","delete-this-changelist": "この変更リストを削除します",- "confirm-delete-changelist": "変更リストを削除しますか?"+ "confirm-delete-changelist": "変更リストを削除しますか?",+ "import": {+ "no-file-to-upload": "アップロードするファイルがありません",+ "upload-sql-or-zip-file": ".sql または .zip ファイルをアップロードする"+ }},"export-data": {"export-rows": "エクスポート行数",diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex 43062603cf..b1c6faa0b0 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -3384,7 +3384,11 @@},"confirm-remove-change": "Xóa thay đổi này?","delete-this-changelist": "Xóa danh sách thay đổi này",- "confirm-delete-changelist": "Xóa danh sách thay đổi này?"+ "confirm-delete-changelist": "Xóa danh sách thay đổi này?",+ "import": {+ "no-file-to-upload": "Không có tập tin để tải lên",+ "upload-sql-or-zip-file": "Tải lên tệp .sql hoặc .zip"+ }},"export-data": {"export-rows": "Xuất hàng",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex 585961c67b..4561a7f27a 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -3384,7 +3384,11 @@},"confirm-remove-change": "移除这项变更?","delete-this-changelist": "删除这个变更列表",- "confirm-delete-changelist": "删除变更列表?"+ "confirm-delete-changelist": "删除变更列表?",+ "import": {+ "no-file-to-upload": "没有可以上传的文件",+ "upload-sql-or-zip-file": "上传 .sql 或 .zip 文件"+ }},"export-data": {"export-rows": "导出行数",