Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: preview issue in UI editor (#3771)
boojack54a3aa257b1e
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vueindex da84ccd493..c0b945c9af 100644--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue@@ -43,9 +43,13 @@<div>{{ $t("sql-editor.self") }}</div><div><button- class="text-sm border px-3 leading-8 rounded cursor-pointer hover:bg-gray-100"+ class="text-sm border px-3 leading-8 flex items-center rounded cursor-pointer hover:opacity-80 disabled:cursor-not-allowed disabled:opacity-60"+ :disabled="!allowSyncSQLFromUIEditor"@click="handleSyncSQLFromUIEditor">+ <heroicons-outline:exclamation-circle+ class="w-5 h-auto mr-1 text-gray-500"+ />{{ $t("ui-editor.sync-sql-from-ui-editor") }}</button></div>@@ -65,7 +69,11 @@<button type="button" class="btn-normal" @click="dismissModal">{{ $t("common.cancel") }}</button>- <button class="btn-primary" @click="handlePreviewIssue">+ <button+ class="btn-primary"+ :disabled="!allowPreviewIssue"+ @click="handlePreviewIssue"+ >{{ $t("ui-editor.preview-issue") }}</button></div>@@ -78,7 +86,7 @@<script lang="ts" setup>import dayjs from "dayjs";import { head } from "lodash-es";-import { onMounted, PropType, reactive, ref } from "vue";+import { computed, onMounted, PropType, reactive, ref, watch } from "vue";import { useRouter } from "vue-router";import {Database,@@ -88,8 +96,13 @@ import {UNKNOWN_ID,} from "@/types";import { allowGhostMigration } from "@/utils";-import { useDatabaseStore, useUIEditorStore } from "@/store";+import {+ useDatabaseStore,+ useNotificationStore,+ useUIEditorStore,+} from "@/store";import { diffTableList } from "@/utils/UIEditor/diffTable";+import { validateDatabaseEdit } from "@/utils/UIEditor/validate";import UIEditor from "@/components/UIEditor/UIEditor.vue";import GhostDialog from "./GhostDialog.vue";@@ -122,8 +135,26 @@ const state = reactive<LocalState>({});const editorStore = useUIEditorStore();const databaseStore = useDatabaseStore();+const notificationStore = useNotificationStore();+const statementFromUIEditor = ref<string>();const ghostDialog = ref<InstanceType<typeof GhostDialog>>();+const allowPreviewIssue = computed(() => {+ if (state.selectedTab === "ui-editor") {+ const databaseEditList = getDatabaseEditListWithUIEditor();+ return databaseEditList.length !== 0;+ } else {+ return state.editStatement !== "";+ }+});++const allowSyncSQLFromUIEditor = computed(() => {+ if (state.selectedTab === "raw-sql") {+ return statementFromUIEditor.value !== state.editStatement;+ }+ return false;+});+const databaseList = props.databaseIdList.map((databaseId) => {return databaseStore.getDatabaseById(databaseId);});@@ -182,18 +213,23 @@ const isUsingGhostMigration = async (databaseList: Database[]) => {};const handleSyncSQLFromUIEditor = async () => {+ if (!allowSyncSQLFromUIEditor.value) {+ return;+ }+const databaseEditMap = await fetchDatabaseEditMapWithUIEditor();state.editStatement = Array.from(databaseEditMap.values()).join("\n");+ statementFromUIEditor.value = state.editStatement;};-const fetchDatabaseEditMapWithUIEditor = async () => {+const getDatabaseEditListWithUIEditor = () => {const databaseEditList: DatabaseEdit[] = [];for (const database of editorStore.databaseList) {const originTableList = editorStore.originTableList.filter((table) => table.databaseId === database.id);- const tableList = await editorStore.getOrFetchTableListByDatabaseId(- database.id+ const tableList = editorStore.tableList.filter(+ (table) => table.databaseId === database.id);const diffTableListResult = diffTableList(originTableList, tableList);if (@@ -208,7 +244,11 @@ const fetchDatabaseEditMapWithUIEditor = async () => {});}}+ return databaseEditList;+};+const fetchDatabaseEditMapWithUIEditor = async () => {+ const databaseEditList = getDatabaseEditListWithUIEditor();const databaseEditMap: Map<DatabaseId, string> = new Map();if (databaseEditList.length > 0) {for (const databaseEdit of databaseEditList) {@@ -256,14 +296,41 @@ const handlePreviewIssue = async () => {if (state.selectedTab === "raw-sql") {query.sql = state.editStatement;} else {+ const databaseEditList = getDatabaseEditListWithUIEditor();+ const validateResultList = databaseEditList.map((databaseEdit) =>+ validateDatabaseEdit(databaseEdit)+ );+ const messageList = validateResultList.reduce(+ (result, { messageList }) => {+ result.push(...messageList);+ return result;+ },+ [] as {+ message: string;+ level: "warning" | "error";+ }[]+ );+ if (messageList.length > 0) {+ notificationStore.pushNotification({+ module: "bytebase",+ style: "CRITICAL",+ title: "Invalid request",+ description: JSON.stringify(+ messageList.map((message) => message.message)+ ),+ });+ return;+ }+const databaseEditMap = await fetchDatabaseEditMapWithUIEditor();const databaseIdList = Array.from(databaseEditMap.keys());if (databaseIdList.length > 0) {const statmentList = Array.from(databaseEditMap.values());- query.sql = statmentList.join("\n");-- if (!props.tenantMode) {+ if (props.tenantMode) {+ query.sql = statmentList.join("\n");+ } else {query.databaseList = databaseIdList.join(",");+ query.sqlList = JSON.stringify(statmentList);query.name = generateIssueName(databaseList.filter((database) => databaseIdList.includes(database.id))@@ -304,6 +371,16 @@ const generateIssueName = (return issueNameParts.join(" ");};++watch(+ () => getDatabaseEditListWithUIEditor(),+ () => {+ statementFromUIEditor.value = undefined;+ },+ {+ deep: true,+ }+);</script><style>diff --git a/frontend/src/components/Issue/IssueDetailLayout.vue b/frontend/src/components/Issue/IssueDetailLayout.vueindex a72d4b9187..42d67fd542 100644--- a/frontend/src/components/Issue/IssueDetailLayout.vue+++ b/frontend/src/components/Issue/IssueDetailLayout.vue@@ -304,13 +304,24 @@ onMounted(() => {const hasSQLReviewPolicyFeature = featureToRef("bb.feature.sql-review");watch(- [create, issue, () => route.query.sql as string, issueLogic],- ([create, issue, sql, provider]) => {- // If 'sql' in URL query, update the issueCreate's statement- // Only works for the first time.- // E.g. redirected from SQL editor when user wants to execute DML.- if (create && issue && sql && provider) {- provider.updateStatement(sql);+ [+ create,+ issue,+ () => route.query.sqlList as string,+ () => route.query.sql as string,+ issueLogic,+ ],+ ([create, issue, sqlList, sql, provider]) => {+ if (create && issue && provider) {+ if (sqlList) {+ // If 'sqlList' in URL query, update the tasks's statement in issueCreate.+ provider.initialTaskListStatement();+ } else if (sql) {+ // If 'sql' in URL query, update the issueCreate's statement+ // Only works for the first time.+ // E.g. redirected from SQL editor when user wants to execute DML.+ provider.updateStatement(sql);+ }}});diff --git a/frontend/src/components/Issue/logic/IssueLogic.ts b/frontend/src/components/Issue/logic/IssueLogic.tsindex b376a84925..674de007e3 100644--- a/frontend/src/components/Issue/logic/IssueLogic.ts+++ b/frontend/src/components/Issue/logic/IssueLogic.ts@@ -51,6 +51,9 @@ type IssueLogic = {) => any;createIssue: (issueCreate: IssueCreate) => any;+ // state logic+ initialTaskListStatement: () => any;+// edit logicallowEditStatement: Ref<boolean>;selectedStatement: Ref<string>;diff --git a/frontend/src/components/Issue/logic/common.ts b/frontend/src/components/Issue/logic/common.tsindex c993a5002b..eb46da1389 100644--- a/frontend/src/components/Issue/logic/common.ts+++ b/frontend/src/components/Issue/logic/common.ts@@ -1,5 +1,6 @@import { computed } from "vue";import { cloneDeep } from "lodash-es";+import { useRoute } from "vue-router";import formatSQL from "@/components/MonacoEditor/sqlFormatter";import {useCurrentUser,@@ -28,6 +29,7 @@ import {MigrationDetail,MigrationType,TaskDatabaseSchemaBaselinePayload,+ DatabaseId,} from "@/types";import { useIssueLogic } from "./index";import { isDev, taskCheckRunSummary } from "@/utils";@@ -35,6 +37,7 @@ import { isDev, taskCheckRunSummary } from "@/utils";export const useCommonLogic = () => {const { create, issue, selectedTask, createIssue, onStatusChanged } =useIssueLogic();+ const route = useRoute();const currentUser = useCurrentUser();const databaseStore = useDatabaseStore();const issueStore = useIssueStore();@@ -81,6 +84,36 @@ export const useCommonLogic = () => {});};+ const initialTaskListStatement = () => {+ if (create.value) {+ const taskList = flattenTaskList<TaskCreate>(issue.value);+ const databaseStatementMap = new Map<DatabaseId, string>();+ // route.query.databaseList is comma-splitted databaseId list+ // e.g. databaseList=7002,7006,7014+ const idListString = route.query.databaseList as string;+ // route.query.sqlList is JSON string of a string array.+ const sqlListString = route.query.sqlList as string;+ if (idListString && sqlListString) {+ const databaseIdList = idListString.split(",");+ const statementList = JSON.parse(sqlListString) as string[];+ for (+ let i = 0;+ i < Math.min(databaseIdList.length, statementList.length);+ i+++ ) {+ databaseStatementMap.set(Number(databaseIdList[i]), statementList[i]);+ }+ }++ for (const [databaseId, statement] of databaseStatementMap) {+ const task = taskList.find((task) => task.databaseId === databaseId);+ if (task) {+ task.statement = statement;+ }+ }+ }+ };+const allowEditStatement = computed(() => {// if creating an issue, it's editableif (create.value) {@@ -205,6 +238,7 @@ export const useCommonLogic = () => {patchTask,allowEditStatement,selectedStatement,+ initialTaskListStatement,updateStatement,allowApplyStatementToOtherTasks,applyStatementToOtherTasks,diff --git a/frontend/src/utils/UIEditor/validate.ts b/frontend/src/utils/UIEditor/validate.tsnew file mode 100644index 0000000000..84ba1aba1a--- /dev/null+++ b/frontend/src/utils/UIEditor/validate.ts@@ -0,0 +1,30 @@+import { DatabaseEdit } from "@/types";++interface ValidateResult {+ isValid: boolean;+ messageList: {+ message: string;+ level: "warning" | "error";+ }[];+}++export const validateDatabaseEdit = (+ databaseEdit: DatabaseEdit+): ValidateResult => {+ const validateResult: ValidateResult = {+ isValid: true,+ messageList: [],+ };++ for (const createTableContext of databaseEdit.createTableList) {+ if (createTableContext.addColumnList.length === 0) {+ validateResult.isValid = false;+ validateResult.messageList.push({+ message: `Table ${createTableContext.name} should has at least one column.`,+ level: "error",+ });+ }+ }++ return validateResult;+};