Skip to content

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

chore: allow to select encoding in changelist (#13691)

boojack6eee94f34d0a
diff --git a/frontend/src/components/Changelist/ChangelistDashboard/CreateChangelistPanel.vue b/frontend/src/components/Changelist/ChangelistDashboard/CreateChangelistPanel.vue
index b098092ec4..b14e7cd1bf 100644
--- a/frontend/src/components/Changelist/ChangelistDashboard/CreateChangelistPanel.vue
+++ b/frontend/src/components/Changelist/ChangelistDashboard/CreateChangelistPanel.vue
@@ -55,22 +55,34 @@
:multiple="false"
@change="uploadFileList = [$event.file]"
>
- <NUploadTrigger #="{ handleClick }" abstract>
- <NButton
- icon
- style="--n-padding: 0 10px"
- class="self-start"
- :loading="isParsingUploadFile"
- @click="handleClick"
- >
- <template #icon>
- <UploadIcon class="w-4 h-4" />
- </template>
- {{
- $t("changelist.import.optional-upload-sql-or-zip-file")
- }}
- </NButton>
- </NUploadTrigger>
+ <div
+ class="w-full flex flex-row justify-start items-center gap-2"
+ >
+ <NUploadTrigger #="{ handleClick }" abstract>
+ <NButton
+ icon
+ style="--n-padding: 0 10px"
+ class="self-start"
+ :loading="isParsingUploadFile"
+ @click="handleClick"
+ >
+ <template #icon>
+ <UploadIcon class="w-4 h-4" />
+ </template>
+ {{
+ $t("changelist.import.optional-upload-sql-or-zip-file")
+ }}
+ </NButton>
+ </NUploadTrigger>
+ <NSelect
+ v-if="uploadFileList.length > 0"
+ v-model:value="state.encoding"
+ class="!w-24"
+ filterable
+ :options="encodingOptions"
+ :consistent-menu-width="false"
+ />
+ </div>
<div class="flex flex-col gap-1">
<NUploadFileList />
</div>
@@ -130,10 +142,11 @@ import {
NUploadFileList,
NUploadTrigger,
type UploadFileInfo,
+ NSelect,
} from "naive-ui";
import { Status } from "nice-grpc-common";
import { zindexable as vZindexable } from "vdirs";
-import { ref, watch } from "vue";
+import { computed, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import { BBSpin } from "@/bbkit";
@@ -159,12 +172,21 @@ import {
} from "@/types/proto/v1/changelist_service";
import { Engine } from "@/types/proto/v1/common";
import { Sheet } from "@/types/proto/v1/sheet_service";
-import { extractChangelistResourceName, setSheetStatement } from "@/utils";
+import {
+ ENCODINGS,
+ extractChangelistResourceName,
+ setSheetStatement,
+ type Encoding,
+} from "@/utils";
import { getErrorCode } from "@/utils/grpcweb";
import { fallbackVersionForChange } from "../common";
import { readUpload, type ParsedFile } from "../import";
import { useChangelistDashboardContext } from "./context";
+interface LocalState {
+ encoding: Encoding;
+}
+
const props = defineProps<{
project?: ComposedProject;
disableProjectSelect?: boolean;
@@ -173,6 +195,9 @@ const props = defineProps<{
const router = useRouter();
const { t } = useI18n();
const { showCreatePanel, events } = useChangelistDashboardContext();
+const state = reactive<LocalState>({
+ encoding: "utf-8",
+});
const title = ref("");
const projectName = ref<string | undefined>(props.project?.name);
@@ -180,6 +205,13 @@ const isLoading = ref(false);
const resourceId = ref("");
const resourceIdField = ref<InstanceType<typeof ResourceIdField>>();
+const encodingOptions = computed(() =>
+ ENCODINGS.map((encoding) => ({
+ label: encoding,
+ value: encoding,
+ }))
+);
+
const errors = asyncComputed(() => {
const errors: string[] = [];
if (!isValidProjectName(projectName.value)) {
@@ -276,11 +308,12 @@ const doCreate = async () => {
const createdSheets = await Promise.all(
files.value.map(async (f) => {
- const { name, content } = f;
+ const { name, arrayBuffer } = f;
const sheet = Sheet.fromPartial({
title: name,
engine: Engine.ENGINE_UNSPECIFIED, // TODO(jim)
});
+ const content = new TextDecoder(state.encoding).decode(arrayBuffer);
setSheetStatement(sheet, content);
const created = await useSheetV1Store().createSheet(
project.name,
diff --git a/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue
index ef0473c23f..dd3ce2593f 100644
--- a/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue
+++ b/frontend/src/components/Changelist/ChangelistDetail/NavBar/ImportButton.vue
@@ -23,19 +23,53 @@
</NTooltip>
</NUploadTrigger>
</NUpload>
+
+ <BBModal
+ :show="state.showModal"
+ :title="$t('sql-editor.select-encoding')"
+ class="shadow-inner outline outline-gray-200"
+ @close="closeModal"
+ >
+ <div class="w-80">
+ <div class="w-full flex flex-row justify-start items-center gap-2">
+ <span class="textinfolabel">{{ $t("common.encoding") }}</span>
+ <NSelect
+ v-model:value="state.encoding"
+ class="!w-24"
+ filterable
+ :options="encodingOptions"
+ :consistent-menu-width="false"
+ />
+ </div>
+ <div>
+ <NUpload :file-list="uploadFileList" disabled trigger-class="!hidden">
+ </NUpload>
+ </div>
+ </div>
+ <div class="w-full flex items-center justify-end mt-2 space-x-3">
+ <NButton @click="closeModal">
+ {{ $t("common.cancel") }}
+ </NButton>
+ <NButton type="primary" @click="save">
+ {{ $t("common.save") }}
+ </NButton>
+ </div>
+ </BBModal>
</template>
<script setup lang="ts">
import { UploadIcon } from "lucide-vue-next";
import {
NButton,
+ NSelect,
NTooltip,
NUpload,
NUploadTrigger,
type UploadFileInfo,
} from "naive-ui";
-import { ref } from "vue";
+import { computed, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
+import BBModal from "@/bbkit/BBModal.vue";
import { pushNotification, useChangelistStore, useSheetV1Store } from "@/store";
import {
Changelist_Change as Change,
@@ -43,54 +77,64 @@ import {
} from "@/types/proto/v1/changelist_service";
import { Engine } from "@/types/proto/v1/common";
import { Sheet } from "@/types/proto/v1/sheet_service";
-import { setSheetStatement } from "@/utils";
+import { ENCODINGS, setSheetStatement, type Encoding } from "@/utils";
+import { fallbackVersionForChange } from "../../common";
import { readUpload, type ParsedFile } from "../../import";
import { useChangelistDetailContext } from "../context";
-import { fallbackVersionForChange } from "../../common";
+
+interface LocalState {
+ encoding: Encoding;
+ files: ParsedFile[];
+ showModal: boolean;
+}
const { t } = useI18n();
const { changelist, project, isUpdating } = useChangelistDetailContext();
+const state = reactive<LocalState>({
+ encoding: "utf-8",
+ files: [],
+ showModal: false,
+});
const uploadFileList = ref<UploadFileInfo[]>([]);
-const handleFileChange = async (options: { file: UploadFileInfo }) => {
- const cleanup = () => {
- isUpdating.value = false;
- uploadFileList.value = [];
- };
-
- try {
- isUpdating.value = true;
- const files = await readUpload(options.file);
+const encodingOptions = computed(() =>
+ ENCODINGS.map((encoding) => ({
+ label: encoding,
+ value: encoding,
+ }))
+);
- if (files.length === 0) {
- pushNotification({
- module: "bytebase",
- style: "WARN",
- title: t("changelist.import.no-file-to-upload"),
- });
- return cleanup();
- }
-
- await save(files);
+const cleanup = () => {
+ isUpdating.value = false;
+ uploadFileList.value = [];
+ state.files = [];
+};
+const handleFileChange = async (options: { file: UploadFileInfo }) => {
+ isUpdating.value = true;
+ const files = await readUpload(options.file);
+ if (files.length === 0) {
pushNotification({
module: "bytebase",
- style: "SUCCESS",
- title: t("common.updated"),
+ style: "WARN",
+ title: t("changelist.import.no-file-to-upload"),
});
- } finally {
- cleanup();
+ uploadFileList.value = [];
}
+ state.files = files;
+ state.showModal = true;
};
-const save = async (files: ParsedFile[]) => {
+const save = async () => {
+ isUpdating.value = true;
const createdSheets = await Promise.all(
- files.map(async (f) => {
- const { name, content } = f;
+ state.files.map(async (f) => {
+ const { name, arrayBuffer } = f;
const sheet = Sheet.fromPartial({
title: name,
engine: Engine.ENGINE_UNSPECIFIED, // TODO(jim)
});
+ const content = new TextDecoder(state.encoding).decode(arrayBuffer);
setSheetStatement(sheet, content);
const created = await useSheetV1Store().createSheet(
project.value.name,
@@ -102,7 +146,7 @@ const save = async (files: ParsedFile[]) => {
const newChanges = createdSheets.map((sheet) =>
Change.fromPartial({
sheet: sheet.name,
- version: fallbackVersionForChange()
+ version: fallbackVersionForChange(),
})
);
const changelistPatch = Changelist.fromPartial({
@@ -110,5 +154,16 @@ const save = async (files: ParsedFile[]) => {
changes: [...changelist.value.changes, ...newChanges],
});
await useChangelistStore().patchChangelist(changelistPatch, ["changes"]);
+ closeModal();
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("common.updated"),
+ });
+};
+
+const closeModal = () => {
+ state.showModal = false;
+ cleanup();
};
</script>
diff --git a/frontend/src/components/Changelist/import.ts b/frontend/src/components/Changelist/import.ts
index fc6f028faa..f21694ebe7 100644
--- a/frontend/src/components/Changelist/import.ts
+++ b/frontend/src/components/Changelist/import.ts
@@ -1,11 +1,10 @@
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;
+ arrayBuffer: ArrayBuffer;
};
const unzip = async (file: File) => {
@@ -15,34 +14,16 @@ const unzip = async (file: File) => {
);
const results = await Promise.all(
files.map<Promise<ParsedFile>>(async (f) => {
- const content = await f.async("string");
+ const arrayBuffer = await f.async("arraybuffer");
return {
name: f.name,
- content,
+ arrayBuffer,
};
})
);
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 (fileInfo: UploadFileInfo) => {
if (!fileInfo.file) {
return [];
@@ -50,10 +31,14 @@ export const readUpload = async (fileInfo: UploadFileInfo) => {
const files: ParsedFile[] = [];
if (fileInfo.name.toLowerCase().endsWith(".sql")) {
- const content = await readFile(fileInfo.file);
+ if (!fileInfo.file) {
+ // Should not reach here.
+ return [];
+ }
+ const arrayBuffer = await fileInfo.file.arrayBuffer();
files.push({
name: fileInfo.file.name,
- content,
+ arrayBuffer: arrayBuffer,
});
} else {
const results = await unzip(fileInfo.file);
diff --git a/frontend/src/components/FileContentPreviewModal.vue b/frontend/src/components/FileContentPreviewModal.vue
index 56ad56bc57..98cf0e3f77 100644
--- a/frontend/src/components/FileContentPreviewModal.vue
+++ b/frontend/src/components/FileContentPreviewModal.vue
@@ -43,53 +43,7 @@ import { computed, reactive, ref, watch } from "vue";
import { BBModal } from "@/bbkit";
import { MonacoEditor } from "@/components/MonacoEditor";
import { pushNotification } from "@/store";
-import { readFileAsArrayBuffer } from "@/utils";
-
-// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
-const ENCODINGS = [
- "utf-8",
- "ibm866",
- "iso-8859-2",
- "iso-8859-3",
- "iso-8859-4",
- "iso-8859-5",
- "iso-8859-6",
- "iso-8859-7",
- "iso-8859-8",
- "iso-8859-8i",
- "iso-8859-10",
- "iso-8859-13",
- "iso-8859-14",
- "iso-8859-15",
- "iso-8859-16",
- "koi8-r",
- "koi8-u",
- "macintosh",
- "windows-874",
- "windows-1250",
- "windows-1251",
- "windows-1252",
- "windows-1253",
- "windows-1254",
- "windows-1255",
- "windows-1256",
- "windows-1257",
- "windows-1258",
- "x-mac-cyrillic",
- "gbk",
- "gb18030",
- "hz-gb-2312",
- "big5",
- "euc-jp",
- "iso-2022-jp",
- "shift-jis",
- "euc-kr",
- "iso-2022-kr",
- "utf-16be",
- "utf-16le",
-];
-
-type Encoding = (typeof ENCODINGS)[number];
+import { ENCODINGS, readFileAsArrayBuffer, type Encoding } from "@/utils";
interface LocalState {
encoding: Encoding;
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 3da8623abd..1a0dd370cf 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -288,7 +288,8 @@
"n-rows": "{n} rows"
},
"back-to-workspace": "Back to workspace",
- "override": "Override"
+ "override": "Override",
+ "encoding": "Encoding"
},
"error-page": {
"go-back-home": "Go back home"
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index b2f7978ec2..e45bb7e9ac 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -288,7 +288,8 @@
"n-rows": "{n} filas"
},
"back-to-workspace": "Volver al espacio de trabajo",
- "override": "Anular"
+ "override": "Anular",
+ "encoding": "Codificación"
},
"error-page": {
"go-back-home": "Volver al inicio"
@@ -3698,4 +3699,4 @@
"worksheet": {
"self": "Hoja de trabajo"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index e5f47e1b28..830dc14755 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -288,7 +288,8 @@
"n-rows": "{n} 行"
},
"back-to-workspace": "ワークスペースに戻る",
- "override": "オーバーライド"
+ "override": "オーバーライド",
+ "encoding": "エンコーディング"
},
"error-page": {
"go-back-home": "ホームページに戻る"
@@ -3698,4 +3699,4 @@
"worksheet": {
"self": "ワークシート"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index b25eb693af..295f625949 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -288,7 +288,8 @@
"n-rows": "{n} 行"
},
"back-to-workspace": "返回工作空间",
- "override": "覆盖"
+ "override": "覆盖",
+ "encoding": "编码"
},
"error-page": {
"go-back-home": "回到主页"
@@ -3698,4 +3699,4 @@
"worksheet": {
"self": "工作表"
}
-}
+}
\ No newline at end of file
diff --git a/frontend/src/utils/v1/common.ts b/frontend/src/utils/v1/common.ts
index 63bb624860..a8f2144d2d 100644
--- a/frontend/src/utils/v1/common.ts
+++ b/frontend/src/utils/v1/common.ts
@@ -19,3 +19,50 @@ export const calcUpdateMask = (a: any, b: any, toSnakeCase = false) => {
export const humanizeDate = (date: Date | undefined) => {
return humanizeTs(Math.floor((date?.getTime() ?? 0) / 1000));
};
+
+// The list of supported encodings.
+// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
+export const ENCODINGS = [
+ "utf-8",
+ "ibm866",
+ "iso-8859-2",
+ "iso-8859-3",
+ "iso-8859-4",
+ "iso-8859-5",
+ "iso-8859-6",
+ "iso-8859-7",
+ "iso-8859-8",
+ "iso-8859-8i",
+ "iso-8859-10",
+ "iso-8859-13",
+ "iso-8859-14",
+ "iso-8859-15",
+ "iso-8859-16",
+ "koi8-r",
+ "koi8-u",
+ "macintosh",
+ "windows-874",
+ "windows-1250",
+ "windows-1251",
+ "windows-1252",
+ "windows-1253",
+ "windows-1254",
+ "windows-1255",
+ "windows-1256",
+ "windows-1257",
+ "windows-1258",
+ "x-mac-cyrillic",
+ "gbk",
+ "gb18030",
+ "hz-gb-2312",
+ "big5",
+ "euc-jp",
+ "iso-2022-jp",
+ "shift-jis",
+ "euc-kr",
+ "iso-2022-kr",
+ "utf-16be",
+ "utf-16le",
+];
+
+export type Encoding = (typeof ENCODINGS)[number];