Skip to content

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

feat: check unsaved tabs when preview issue (#3619)

boojackc812230de5d3
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index 685e34ae7d..dd0b72dba1 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -80,7 +80,8 @@
<script lang="ts" setup>
import dayjs from "dayjs";
-import { head } from "lodash-es";
+import { head, isEqual } from "lodash-es";
+import { useDialog } from "naive-ui";
import { onMounted, PropType, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import {
@@ -88,6 +89,8 @@ import {
DatabaseEdit,
DatabaseId,
SQLDialect,
+ TabContext,
+ UIEditorTabType,
UNKNOWN_ID,
} from "@/types";
import { allowGhostMigration } from "@/utils";
@@ -122,6 +125,7 @@ const state = reactive<LocalState>({
const editorStore = useUIEditorStore();
const tableStore = useTableStore();
const databaseStore = useDatabaseStore();
+const dialog = useDialog();
const ghostDialog = ref<InstanceType<typeof GhostDialog>>();
const databaseList = props.databaseIdList.map((databaseId) => {
@@ -182,11 +186,11 @@ const isUsingGhostMigration = async (databaseList: Database[]) => {
};
const handleSyncSQLFromUIEditor = async () => {
- const statement = await fetchDDLStatementWithUIEditor();
- state.editStatement = statement;
+ const databaseEditMap = await fetchDatabaseEditMapWithUIEditor();
+ state.editStatement = Array.from(databaseEditMap.values()).join("\n");
};
-const fetchDDLStatementWithUIEditor = async () => {
+const fetchDatabaseEditMapWithUIEditor = async () => {
const databaseEditList: DatabaseEdit[] = [];
for (const database of editorStore.databaseList) {
const originTableList = await tableStore.getOrFetchTableListByDatabaseId(
@@ -212,16 +216,36 @@ const fetchDDLStatementWithUIEditor = async () => {
}
}
- const statmentList = [];
+ const databaseEditMap: Map<DatabaseId, string> = new Map();
if (databaseEditList.length > 0) {
- const databaseIdList: DatabaseId[] = [];
for (const databaseEdit of databaseEditList) {
- databaseIdList.push(databaseEdit.databaseId);
const statement = await editorStore.postDatabaseEdit(databaseEdit);
- statmentList.push(statement);
+ databaseEditMap.set(databaseEdit.databaseId, statement);
}
}
- return statmentList.join("\n");
+ return databaseEditMap;
+};
+
+const unsavedDialogWarning = (): Promise<
+ "Close" | "NegativeClick" | "PositiveClick"
+> => {
+ return new Promise((resolve) => {
+ dialog.warning({
+ title: "Confirm to continue",
+ content: "There are unsaved changes. Are you sure confirm to continue?",
+ negativeText: "Discard",
+ positiveText: "Save",
+ onClose: () => {
+ resolve("Close");
+ },
+ onNegativeClick: () => {
+ resolve("NegativeClick");
+ },
+ onPositiveClick: () => {
+ resolve("PositiveClick");
+ },
+ });
+ });
};
const handlePreviewIssue = async () => {
@@ -252,39 +276,34 @@ const handlePreviewIssue = async () => {
if (state.selectedTab === "raw-sql") {
query.sql = state.editStatement;
} else {
- const databaseEditList: DatabaseEdit[] = [];
- for (const database of editorStore.databaseList) {
- const originTableList = await tableStore.getOrFetchTableListByDatabaseId(
- database.id
- );
- const updatedTableList = (
- await editorStore.getOrFetchTableListByDatabaseId(database.id)
- ).filter((table) => !editorStore.droppedTableList.includes(table));
- const diffTableListResult = diffTableList(
- originTableList,
- updatedTableList
- );
- if (
- diffTableListResult.createTableList.length > 0 ||
- diffTableListResult.alterTableList.length > 0 ||
- diffTableListResult.renameTableList.length > 0 ||
- diffTableListResult.dropTableList.length > 0
- ) {
- databaseEditList.push({
- databaseId: database.id,
- ...diffTableListResult,
- });
+ // Check whether tabs saved.
+ const unsavedTabList: TabContext[] = [];
+ for (const tab of editorStore.tabList) {
+ if (tab.type === UIEditorTabType.TabForTable) {
+ if (!isEqual(tab.tableCache, tab.table)) {
+ unsavedTabList.push(tab);
+ }
}
}
-
- if (databaseEditList.length > 0) {
- const databaseIdList: DatabaseId[] = [];
- const statmentList = [];
- for (const databaseEdit of databaseEditList) {
- databaseIdList.push(databaseEdit.databaseId);
- const statement = await editorStore.postDatabaseEdit(databaseEdit);
- statmentList.push(statement);
+ if (unsavedTabList.length > 0) {
+ const action = await unsavedDialogWarning();
+ if (action === "NegativeClick") {
+ for (const unsavedTab of unsavedTabList) {
+ editorStore.discardTabChanges(unsavedTab);
+ }
+ } else if (action === "PositiveClick") {
+ for (const unsavedTab of unsavedTabList) {
+ editorStore.saveTab(unsavedTab);
+ }
+ } else {
+ return;
}
+ }
+
+ const databaseEditMap = await fetchDatabaseEditMapWithUIEditor();
+ const databaseIdList = Array.from(databaseEditMap.keys());
+ if (databaseIdList.length > 0) {
+ const statmentList = Array.from(databaseEditMap.values());
query.databaseList = databaseIdList.join(",");
query.name = generateIssueName(
databaseList
diff --git a/frontend/src/components/UIEditor/DatabaseTree.vue b/frontend/src/components/UIEditor/DatabaseTree.vue
index f2a2e446aa..78cc95cc27 100644
--- a/frontend/src/components/UIEditor/DatabaseTree.vue
+++ b/frontend/src/components/UIEditor/DatabaseTree.vue
@@ -264,7 +264,11 @@ watch(
if (state.shouldRelocateTreeNode) {
nextTick(() => {
const element = treeRef.value?.querySelector(".n-tree-node--selected");
- scrollIntoView(element);
+ if (element) {
+ scrollIntoView(element, {
+ scrollMode: "if-needed",
+ });
+ }
});
}
}
@@ -326,7 +330,7 @@ const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
{
class: additionalClassList.join(" "),
},
- [
+ () => [
h("span", {
innerHTML: getHighlightHTMLByKeyWords(
escape(treeNode.label),
diff --git a/frontend/src/components/UIEditor/Panels/DatabaseEditor.vue b/frontend/src/components/UIEditor/Panels/DatabaseEditor.vue
index ed875819b4..466c222527 100644
--- a/frontend/src/components/UIEditor/Panels/DatabaseEditor.vue
+++ b/frontend/src/components/UIEditor/Panels/DatabaseEditor.vue
@@ -231,8 +231,12 @@ watch(
databaseId: database.id,
...diffTableListResult,
};
- const statement = await editorStore.postDatabaseEdit(databaseEdit);
- state.statement = statement;
+ try {
+ const statement = await editorStore.postDatabaseEdit(databaseEdit);
+ state.statement = statement;
+ } catch (error) {
+ state.statement = "";
+ }
}
state.isFetchingDDL = false;
}
@@ -276,6 +280,6 @@ const handleDropTable = (table: Table) => {
@apply py-2 px-3;
}
.table-body-item-container {
- @apply w-full box-border p-px pl-3 pr-4 relative leading-9;
+ @apply w-full h-10 box-border p-px pl-3 pr-4 relative leading-9;
}
</style>
diff --git a/frontend/src/components/UIEditor/Panels/TableEditor.vue b/frontend/src/components/UIEditor/Panels/TableEditor.vue
index daec5d8b6e..4cc488bfed 100644
--- a/frontend/src/components/UIEditor/Panels/TableEditor.vue
+++ b/frontend/src/components/UIEditor/Panels/TableEditor.vue
@@ -17,11 +17,14 @@
</div>
</div>
<div class="flex flex-row items-center space-x-2">
- <NPopover trigger="click" placement="bottom-end">
+ <NPopover
+ trigger="click"
+ placement="bottom-end"
+ @update:show="handlePreviewDDLStatement"
+ >
<template #trigger>
<button
class="flex flex-row justify-center items-center border px-2 py-1 text-sm text-gray-700 rounded cursor-pointer hover:bg-gray-100"
- @click="handlePreviewDDLStatement"
>
SQL Preview
</button>
@@ -35,10 +38,10 @@
</div>
<template v-else>
<HighlightCodeBlock
- v-if="state.ddlStatement !== ''"
+ v-if="state.statement !== ''"
class="text-sm whitespace-pre-wrap break-all"
language="sql"
- :code="state.ddlStatement"
+ :code="state.statement"
></HighlightCodeBlock>
<span v-else class="py-2 italic">Nothing changed</span>
</template>
@@ -85,7 +88,7 @@
<input
v-model="column.type"
placeholder="column type"
- class="pr-8 column-field-input"
+ class="column-field-input !pr-8"
type="text"
/>
<NDropdown
@@ -195,12 +198,12 @@ const columnTypeFieldRegexp = /\S+/;
interface LocalState {
isFetchingDDL: boolean;
- ddlStatement: string;
+ statement: string;
}
const state = reactive<LocalState>({
isFetchingDDL: false,
- ddlStatement: "",
+ statement: "",
});
const editorStore = useUIEditorStore();
const tableStore = useTableStore();
@@ -292,8 +295,7 @@ const handleSaveChanges = async () => {
}
}
- table.name = tableCache.name;
- table.columnList = cloneDeep(tableCache.columnList);
+ editorStore.saveTab(currentTab);
};
const handleAddColumn = () => {
@@ -317,7 +319,11 @@ const handleDiscardChanges = () => {
tableCache.columnList = cloneDeep(table.columnList);
};
-const handlePreviewDDLStatement = async () => {
+const handlePreviewDDLStatement = async (show: boolean) => {
+ if (!show) {
+ return;
+ }
+
const databaseEdit: DatabaseEdit = {
databaseId: table.database.id,
createTableList: [],
@@ -344,8 +350,12 @@ const handlePreviewDDLStatement = async () => {
}
}
state.isFetchingDDL = true;
- const statement = await editorStore.postDatabaseEdit(databaseEdit);
- state.ddlStatement = statement;
+ try {
+ const statement = await editorStore.postDatabaseEdit(databaseEdit);
+ state.statement = statement;
+ } catch (error) {
+ state.statement = "";
+ }
state.isFetchingDDL = false;
};
</script>
@@ -355,9 +365,9 @@ const handlePreviewDDLStatement = async () => {
@apply py-2 px-3;
}
.table-body-item-container {
- @apply w-full box-border p-px pr-2 relative;
+ @apply w-full h-10 box-border p-px pr-2 relative;
}
.column-field-input {
- @apply w-full box-border rounded border-none bg-transparent text-sm focus:bg-white focus:text-black placeholder:italic placeholder:text-gray-400;
+ @apply w-full pr-1 box-border border-transparent text-ellipsis rounded bg-transparent text-sm placeholder:italic placeholder:text-gray-400 focus:bg-white focus:text-black;
}
</style>
diff --git a/frontend/src/components/UIEditor/TabsContainer.vue b/frontend/src/components/UIEditor/TabsContainer.vue
index e93ac6e413..c34fc93d05 100644
--- a/frontend/src/components/UIEditor/TabsContainer.vue
+++ b/frontend/src/components/UIEditor/TabsContainer.vue
@@ -51,6 +51,7 @@ import { useUIEditorStore } from "@/store";
import { TabContext, UIEditorTabType } from "@/types";
const editorStore = useUIEditorStore();
+const dialog = useDialog();
const tabsContainerRef = ref();
const tabList = computed(() => {
return Array.from(editorStore.tabState.tabMap.values());
@@ -58,7 +59,6 @@ const tabList = computed(() => {
const currentTab = computed(() => {
return editorStore.currentTab;
});
-const dialog = useDialog();
watch(
() => currentTab.value,
@@ -67,26 +67,23 @@ watch(
const element = tabsContainerRef.value?.querySelector(
`.tab-${currentTab.value?.id}`
);
- scrollIntoView(element);
+ if (element) {
+ scrollIntoView(element, {
+ scrollMode: "if-needed",
+ });
+ }
});
}
);
const checkTabSaved = (tab: TabContext) => {
- if (tab.type === UIEditorTabType.TabForTable) {
- const table = editorStore.tableList.find((table) => {
- return (
- table.id === tab.tableCache.id &&
- table.database.id === tab.tableCache.database.id &&
- table.name === tab.tableCache.name
- );
- });
- if (!isEqual(tab.tableCache, table)) {
- return false;
- }
- } else if (tab.type === UIEditorTabType.TabForDatabase) {
+ if (tab.type === UIEditorTabType.TabForDatabase) {
// Edit database metadata is not allowed.
return true;
+ } else if (tab.type === UIEditorTabType.TabForTable) {
+ if (!isEqual(tab.tableCache, tab.table)) {
+ return false;
+ }
}
return true;
@@ -114,17 +111,7 @@ const handleCloseTab = (tab: TabContext) => {
if (tab.type === UIEditorTabType.TabForDatabase) {
editorStore.closeTab(tab.id);
} else if (tab.type === UIEditorTabType.TabForTable) {
- const table = editorStore.tableList.find((table) => {
- return (
- table.id === tab.tableCache.id &&
- table.database.id === tab.tableCache.database.id &&
- table.name === tab.tableCache.name
- );
- });
- if (!table) {
- return;
- }
- if (isEqual(tab.tableCache, table)) {
+ if (isEqual(tab.tableCache, tab.table)) {
editorStore.closeTab(tab.id);
} else {
dialog.warning({
diff --git a/frontend/src/store/modules/UIEditor.ts b/frontend/src/store/modules/UIEditor.ts
index 70deb4e5fe..cd5470e28b 100644
--- a/frontend/src/store/modules/UIEditor.ts
+++ b/frontend/src/store/modules/UIEditor.ts
@@ -72,6 +72,13 @@ export const useUIEditorStore = defineStore("UIEditor", {
if (tabCache !== undefined) {
tab = tabCache;
} else {
+ // Don't show tab for dropped table.
+ if (tab.type === UIEditorTabType.TabForTable) {
+ if (this.droppedTableList.includes(tab.table)) {
+ return;
+ }
+ }
+
this.tabState.tabMap.set(tab.id, tab);
}
@@ -79,6 +86,22 @@ export const useUIEditorStore = defineStore("UIEditor", {
this.setCurrentTab(tab.id);
}
},
+ saveTab(tab: TabContext) {
+ if (tab.type === UIEditorTabType.TabForDatabase) {
+ // Edit database metadata is not allowed.
+ } else if (tab.type === UIEditorTabType.TabForTable) {
+ tab.table.name = tab.tableCache.name;
+ tab.table.columnList = cloneDeep(tab.tableCache.columnList);
+ }
+ },
+ discardTabChanges(tab: TabContext) {
+ if (tab.type === UIEditorTabType.TabForDatabase) {
+ // Edit database metadata is not allowed.
+ } else if (tab.type === UIEditorTabType.TabForTable) {
+ tab.tableCache.name = tab.table.name;
+ tab.tableCache.columnList = cloneDeep(tab.table.columnList);
+ }
+ },
setCurrentTab(tabId: string) {
if (isUndefined(this.tabState.tabMap.get(tabId))) {
this.tabState.currentTabId = undefined;