Skip to content

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

feat(frontend): BYT-947 issue SQL statement supports import SQL scripts (#1986)

Liu Ji8398e4189389
diff --git a/frontend/src/components/Issue/IssueTaskStatementPanel.vue b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
index 94597ae254..3d72c67871 100644
--- a/frontend/src/components/Issue/IssueTaskStatementPanel.vue
+++ b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
@@ -32,6 +32,19 @@
/>
<span class="textlabel">{{ $t("issue.format-on-save") }}</span>
</label>
+ <button
+ v-if="state.editing"
+ type="button"
+ class="mt-0.5 px-3 border border-control-border rounded-sm text-control bg-control-bg hover:bg-control-bg-hover text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2 relative"
+ >
+ {{ $t("issue.upload-file") }}
+ <input
+ type="file"
+ accept=".sql,.txt,application/sql,text/plain"
+ class="absolute inset-0 z-1 opacity-0"
+ @change="handleUploadFile"
+ />
+ </button>
</template>
<template v-else>
<button
@@ -57,10 +70,15 @@
<button
v-if="state.editing"
type="button"
- class="mt-0.5 px-3 rounded-sm text-control hover:bg-control-bg-hover disabled:bg-control-bg disabled:opacity-50 disabled:cursor-not-allowed text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
- @click.prevent="cancelEdit"
+ class="mt-0.5 px-3 border border-control-border rounded-sm text-control bg-control-bg hover:bg-control-bg-hover text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2 relative"
>
- {{ $t("common.cancel") }}
+ {{ $t("issue.upload-file") }}
+ <input
+ type="file"
+ accept=".sql,.txt,application/sql,text/plain"
+ class="absolute inset-0 z-1 opacity-0"
+ @change="handleUploadFile"
+ />
</button>
<button
v-if="state.editing"
@@ -71,6 +89,14 @@
>
{{ $t("common.save") }}
</button>
+ <button
+ v-if="state.editing"
+ type="button"
+ class="mt-0.5 px-3 rounded-sm text-control hover:bg-control-bg-hover disabled:bg-control-bg disabled:opacity-50 disabled:cursor-not-allowed text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
+ @click.prevent="cancelEdit"
+ >
+ {{ $t("common.cancel") }}
+ </button>
</template>
<template v-if="issue.project.workflowType === 'VCS'">
<!--
@@ -141,10 +167,17 @@ import {
ref,
Ref,
} from "vue";
-import { useRepositoryStore, useTableStore, useUIStateStore } from "@/store";
+import { useDialog } from "naive-ui";
+import {
+ pushNotification,
+ useRepositoryStore,
+ useTableStore,
+ useUIStateStore,
+} from "@/store";
import { useIssueLogic } from "./logic";
import MonacoEditor from "../MonacoEditor/MonacoEditor.vue";
import { baseDirectoryWebUrl, Issue, Repository } from "@/types";
+import { useI18n } from "vue-i18n";
interface LocalState {
editing: boolean;
@@ -157,6 +190,8 @@ const EDITOR_MIN_HEIGHT = {
EDITABLE: 120, // ~= 6 lines, a reasonable size to start writing SQL
};
+const MAX_UPLOAD_FILE_SIZE_MB = 1;
+
export default defineComponent({
name: "IssueTaskStatementPanel",
components: {
@@ -181,6 +216,7 @@ export default defineComponent({
} = useIssueLogic();
const uiStateStore = useUIStateStore();
+ const { t } = useI18n();
const state = reactive<LocalState>({
editing: false,
@@ -189,6 +225,7 @@ export default defineComponent({
});
const editorRef = ref<InstanceType<typeof MonacoEditor>>();
+ const overrideSQLDialog = useDialog();
const formatOnSave = computed({
get: () => uiStateStore.issueFormatStatementOnSave,
@@ -257,6 +294,65 @@ export default defineComponent({
state.editing = false;
};
+ const handleUploadFile = (e: Event) => {
+ const target = e.target as HTMLInputElement;
+ const file = (target.files || [])[0];
+ const cleanup = () => {
+ // Note that once selected a file, selecting the same file again will not
+ // trigger <input type="file">'s change event.
+ // So we need to do some cleanup stuff here.
+ target.files = null;
+ target.value = "";
+ };
+
+ if (!file) {
+ return cleanup();
+ }
+ if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("issue.upload-sql-file-max-size-exceeded", {
+ size: `${MAX_UPLOAD_FILE_SIZE_MB}MB`,
+ }),
+ });
+ return cleanup();
+ }
+ const fr = new FileReader();
+ fr.onload = () => {
+ const sql = fr.result as string;
+ if (state.editStatement) {
+ // Show a confirm dialog before replacing if the editing statement
+ // is not empty
+ overrideSQLDialog.create({
+ positiveText: t("common.confirm"),
+ negativeText: t("common.cancel"),
+ title: t("issue.override-current-statement"),
+ onNegativeClick: () => {
+ // nothing to do
+ },
+ onPositiveClick: () => {
+ onStatementChange(sql);
+ },
+ });
+ } else {
+ onStatementChange(sql);
+ }
+ };
+ fr.onerror = (e) => {
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: `Read file error`,
+ description: String(fr.error),
+ });
+ return;
+ };
+ fr.readAsText(file);
+
+ cleanup();
+ };
+
const onStatementChange = (value: string) => {
state.editStatement = value;
if (create.value) {
@@ -293,6 +389,7 @@ export default defineComponent({
beginEdit,
saveEdit,
cancelEdit,
+ handleUploadFile,
onStatementChange,
goToVCS,
handleMonacoEditorReady,
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index a8c301711c..f3d5351e26 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -490,7 +490,10 @@
"format-on-save": "Format on save",
"action-to-current-stage": "{action} current stage",
"edit-sql-statement-in-vcs": "This project has enabled VCS based version control. Please modify the SQL file in the corresponding Git repository, and commit it to create a new issue.",
- "edit-sql-statement": "Edit SQL statement"
+ "edit-sql-statement": "Edit SQL statement",
+ "upload-file": "Upload file",
+ "override-current-statement": "Override current SQL statement",
+ "upload-sql-file-max-size-exceeded": "Max file size ({size}) exceeded."
},
"alter-schema": {
"vcs-enabled": "This project has enabled VCS based version control and selecting database below will navigate you to the corresponding Git repository to initiate the change process.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 3a1c3ae2da..20414d42cc 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -491,7 +491,10 @@
"format-on-save": "保存时格式化",
"action-to-current-stage": "{action}当前阶段",
"edit-sql-statement-in-vcs": "该项目开启了基于 VCS 的版本管理。请在对应的 Git 仓库修改 SQL 文件并提交,开启新的工单。",
- "edit-sql-statement": "编辑 SQL 语句"
+ "edit-sql-statement": "编辑 SQL 语句",
+ "upload-file": "上传文件",
+ "override-current-statement": "覆盖当前的 SQL 语句",
+ "upload-sql-file-max-size-exceeded": "上传文件大小不能超过 {size}。"
},
"alter-schema": {
"vcs-enabled": "该项目开启了基于 VCS 的版本管理,选择下面的数据库会将您导航到相应的 Git 仓库以发起变更流程。",