Skip to content

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

refactor: extract file upload and parse logic (#11915)

Liu Jibda0ace07d37
diff --git a/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue
index 023b93c0ea..6d5cb61108 100644
--- a/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue
+++ b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue
@@ -1,7 +1,9 @@
<template>
<NUpload
+ v-model:file-list="uploadFileList"
abstract
accept="application/x-zip,.zip,application/sql,.sql"
+ :multiple="false"
@change="handleFileChange"
>
<NUploadTrigger #="{ handleClick }" abstract>
@@ -24,8 +26,6 @@
</template>
<script setup lang="ts">
-import JSZip from "jszip";
-import { orderBy } from "lodash-es";
import { UploadIcon } from "lucide-vue-next";
import {
NButton,
@@ -34,6 +34,7 @@ import {
NUploadTrigger,
type UploadFileInfo,
} from "naive-ui";
+import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { pushNotification, useChangelistStore, useSheetV1Store } from "@/store";
import {
@@ -41,76 +42,23 @@ import {
Changelist,
} from "@/types/proto/v1/changelist_service";
import { Sheet } from "@/types/proto/v1/sheet_service";
-import { defer, setSheetStatement } from "@/utils";
+import { setSheetStatement } from "@/utils";
+import { readUpload, type ParsedFile } from "../../import";
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 uploadFileList = ref<UploadFileInfo[]>([]);
const handleFileChange = async (options: { file: UploadFileInfo }) => {
- const fileInfo = options.file;
- if (!fileInfo.file) {
- return;
- }
-
const cleanup = () => {
isUpdating.value = false;
+ uploadFileList.value = [];
};
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);
- }
+ const files = await readUpload(options);
if (files.length === 0) {
pushNotification({
diff --git a/frontend/src/components/Changelist/import.ts b/frontend/src/components/Changelist/import.ts
new file mode 100644
index 0000000000..a571a2c467
--- /dev/null
+++ b/frontend/src/components/Changelist/import.ts
@@ -0,0 +1,64 @@
+import JSZip from "jszip";
+import { orderBy } from "lodash-es";
+import type { UploadFileInfo } from "naive-ui";
+import { defer } from "@/utils";
+
+export type ParsedFile = {
+ name: string;
+ content: string;
+};
+
+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;
+};
+
+export const readUpload = async (options: { file: UploadFileInfo }) => {
+ const fileInfo = options.file;
+ if (!fileInfo.file) {
+ return [];
+ }
+
+ 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);
+ }
+ return files;
+};