Skip to content

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

feat(schema-editor): support rename/drop schema for pg (#4158)

boojackd23bc3fbc406
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index 0204c4aad8..4628fecd07 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -293,11 +293,13 @@ const getDatabaseEditListWithSchemaEditor = () => {
for (const schema of databaseSchema.schemaList) {
const originSchema = databaseSchema.originSchemaList.find(
- (originSchema) => originSchema.name === schema.name
+ (originSchema) => originSchema.id === schema.id
);
const diffSchemaResult = diffSchema(database.id, originSchema, schema);
if (
diffSchemaResult.createSchemaList.length > 0 ||
+ diffSchemaResult.renameSchemaList.length > 0 ||
+ diffSchemaResult.dropSchemaList.length > 0 ||
diffSchemaResult.createTableList.length > 0 ||
diffSchemaResult.alterTableList.length > 0 ||
diffSchemaResult.renameTableList.length > 0 ||
diff --git a/frontend/src/components/SchemaEditor/DatabaseTree.vue b/frontend/src/components/SchemaEditor/DatabaseTree.vue
index 38762308e3..016196cb5b 100644
--- a/frontend/src/components/SchemaEditor/DatabaseTree.vue
+++ b/frontend/src/components/SchemaEditor/DatabaseTree.vue
@@ -38,19 +38,20 @@
</div>
</div>
+ <SchemaNameModal
+ v-if="state.schemaNameModalContext !== undefined"
+ :database-id="state.schemaNameModalContext.databaseId"
+ :schema-id="state.schemaNameModalContext.schemaId"
+ @close="state.schemaNameModalContext = undefined"
+ />
+
<TableNameModal
v-if="state.tableNameModalContext !== undefined"
:database-id="state.tableNameModalContext.databaseId"
- :schema-name="state.tableNameModalContext.schemaName"
+ :schema-id="state.tableNameModalContext.schemaId"
:table-id="state.tableNameModalContext.tableId"
@close="state.tableNameModalContext = undefined"
/>
-
- <SchemaNameModal
- v-if="state.schemaNameModalContext !== undefined"
- :database-id="state.schemaNameModalContext.databaseId"
- @close="state.schemaNameModalContext = undefined"
- />
</template>
<script lang="ts" setup>
@@ -67,13 +68,15 @@ import {
useInstanceStore,
} from "@/store";
import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";
+import { isSchemaChanged } from "./utils/schema";
+import { isTableChanged } from "./utils/table";
+import SchemaNameModal from "./Modals/SchemaNameModal.vue";
import InstanceEngineIcon from "@/components/InstanceEngineIcon.vue";
import TableNameModal from "./Modals/TableNameModal.vue";
import DatabaseIcon from "~icons/heroicons-outline/circle-stack";
import SchemaIcon from "~icons/heroicons-outline/view-columns";
import TableIcon from "~icons/heroicons-outline/table-cells";
import EllipsisIcon from "~icons/heroicons-solid/ellipsis-horizontal";
-import { isTableChanged } from "./utils/table";
interface BaseTreeNode extends TreeOption {
key: string;
@@ -97,14 +100,14 @@ interface TreeNodeForSchema extends BaseTreeNode {
type: "schema";
instanceId: InstanceId;
databaseId: DatabaseId;
- schemaName: string;
+ schemaId: string;
}
interface TreeNodeForTable extends BaseTreeNode {
type: "table";
instanceId: InstanceId;
databaseId: DatabaseId;
- schemaName: string;
+ schemaId: string;
tableId: string;
}
@@ -125,10 +128,11 @@ interface LocalState {
shouldRelocateTreeNode: boolean;
schemaNameModalContext?: {
databaseId: DatabaseId;
+ schemaId: string | undefined;
};
tableNameModalContext?: {
databaseId: DatabaseId;
- schemaName: string;
+ schemaId: string;
tableId: string | undefined;
};
}
@@ -187,18 +191,32 @@ const contextMenuOptions = computed(() => {
} else if (treeNode.type === "schema") {
const options = [];
if (instanceEngine === "POSTGRES") {
- options.push({
- key: "create-table",
- label: t("schema-editor.actions.create-table"),
- });
+ const schema = editorStore.getSchema(
+ treeNode.databaseId,
+ treeNode.schemaId
+ );
+ if (!schema) {
+ return [];
+ }
- const schema = editorStore.databaseSchemaById
- .get(treeNode.databaseId)
- ?.schemaList.find((schema) => schema.name === treeNode.schemaName);
- if (schema?.status === "created") {
+ const isDropped = schema.status === "dropped";
+ if (isDropped) {
+ options.push({
+ key: "restore",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ options.push({
+ key: "create-table",
+ label: t("schema-editor.actions.create-table"),
+ });
+ options.push({
+ key: "rename",
+ label: t("schema-editor.actions.rename"),
+ });
options.push({
- key: "delete-schema",
- label: t("schema-editor.actions.delete-schema"),
+ key: "drop-schema",
+ label: t("schema-editor.actions.drop-schema"),
});
}
}
@@ -206,7 +224,7 @@ const contextMenuOptions = computed(() => {
} else if (treeNode.type === "table") {
const table = editorStore.databaseSchemaById
.get(treeNode.databaseId)
- ?.schemaList.find((schema) => schema.name === treeNode.schemaName)
+ ?.schemaList.find((schema) => schema.id === treeNode.schemaId)
?.tableList.find((table) => table.id === treeNode.tableId);
if (!table) {
return [];
@@ -284,7 +302,7 @@ onMounted(async () => {
expandedKeysRef.value.push(node.key);
const schema = head(schemaList);
if (schemaList.length === 1 && schema) {
- expandedKeysRef.value.push(`s-${node.databaseId}-${schema.name}`);
+ expandedKeysRef.value.push(`s-${node.databaseId}-${schema.id}`);
editorStore.addTab({
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForDatabase,
@@ -328,9 +346,11 @@ watch(
if (instanceEngine === "MYSQL") {
const schemaList: Schema[] =
editorStore.databaseSchemaById.get(database.id)?.schemaList || [];
- const tableList: Table[] = schemaList
- .map((schema) => schema.tableList)
- .flat();
+ const schema = head(schemaList);
+ if (!schema) {
+ return;
+ }
+ const tableList: Table[] = schema.tableList;
if (tableList.length === 0) {
databaseTreeNode.isLeaf = true;
databaseTreeNode.children = [];
@@ -345,7 +365,7 @@ watch(
isLeaf: true,
instanceId: database.instance.id,
databaseId: database.id,
- schemaName: "",
+ schemaId: schema.id,
tableId: table.id,
};
});
@@ -357,11 +377,11 @@ watch(
for (const schema of schemaList) {
const schemaTreeNode: TreeNodeForSchema = {
type: "schema",
- key: `s-${database.id}-${schema.name}`,
+ key: `s-${database.id}-${schema.id}`,
label: schema.name,
instanceId: database.instance.id,
databaseId: database.id,
- schemaName: schema.name,
+ schemaId: schema.id,
isLeaf: true,
};
@@ -379,7 +399,7 @@ watch(
isLeaf: true,
instanceId: database.instance.id,
databaseId: database.id,
- schemaName: schema.name,
+ schemaId: schema.id,
tableId: table.id,
};
});
@@ -412,8 +432,8 @@ watch(
}
if (currentTab.type === SchemaEditorTabType.TabForDatabase) {
- if (currentTab.selectedSchemaName) {
- const key = `s-${currentTab.databaseId}-${currentTab.selectedSchemaName}`;
+ if (currentTab.selectedSchemaId) {
+ const key = `s-${currentTab.databaseId}-${currentTab.selectedSchemaId}`;
selectedKeysRef.value = [key];
} else {
const key = `d-${currentTab.databaseId}`;
@@ -424,7 +444,7 @@ watch(
if (!expandedKeysRef.value.includes(databaseTreeNodeKey)) {
expandedKeysRef.value.push(databaseTreeNodeKey);
}
- const schemaTreeNodeKey = `s-${currentTab.databaseId}-${currentTab.schemaName}`;
+ const schemaTreeNodeKey = `s-${currentTab.databaseId}-${currentTab.schemaId}`;
if (!expandedKeysRef.value.includes(schemaTreeNodeKey)) {
expandedKeysRef.value.push(schemaTreeNodeKey);
}
@@ -498,15 +518,23 @@ const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
if (treeNode.type === "schema") {
const schema = editorStore.databaseSchemaById
.get(treeNode.databaseId)
- ?.schemaList.find((schema) => schema.name === treeNode.schemaName);
+ ?.schemaList.find((schema) => schema.id === treeNode.schemaId);
- if (schema && schema.status === "created") {
- additionalClassList.push("text-green-700");
+ if (schema) {
+ if (schema.status === "created") {
+ additionalClassList.push("text-green-700");
+ } else if (schema.status === "dropped") {
+ additionalClassList.push("text-red-700 line-through");
+ } else {
+ if (isSchemaChanged(treeNode.databaseId, treeNode.schemaId)) {
+ additionalClassList.push("text-yellow-700");
+ }
+ }
}
} else if (treeNode.type === "table") {
const table = editorStore.databaseSchemaById
.get(treeNode.databaseId)
- ?.schemaList.find((schema) => schema.name === treeNode.schemaName)
+ ?.schemaList.find((schema) => schema.id === treeNode.schemaId)
?.tableList.find((table) => table.id === treeNode.tableId);
if (table) {
@@ -518,7 +546,7 @@ const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
if (
isTableChanged(
treeNode.databaseId,
- treeNode.schemaName,
+ treeNode.schemaId,
treeNode.tableId
)
) {
@@ -633,14 +661,14 @@ const nodeProps = ({ option: treeNode }: { option: TreeNode }) => {
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForDatabase,
databaseId: treeNode.databaseId,
- selectedSchemaName: treeNode.schemaName,
+ selectedSchemaId: treeNode.schemaId,
});
} else if (treeNode.type === "table") {
editorStore.addTab({
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForTable,
databaseId: treeNode.databaseId,
- schemaName: treeNode.schemaName,
+ schemaId: treeNode.schemaId,
tableId: treeNode.tableId,
});
}
@@ -650,7 +678,7 @@ const nodeProps = ({ option: treeNode }: { option: TreeNode }) => {
selectedKeysRef.value = [`d-${treeNode.databaseId}`];
} else if (treeNode.type === "schema") {
selectedKeysRef.value = [
- `s-${treeNode.databaseId}-${treeNode.schemaName}`,
+ `s-${treeNode.databaseId}-${treeNode.schemaId}`,
];
} else if (treeNode.type === "table") {
selectedKeysRef.value = [
@@ -689,15 +717,27 @@ const handleContextMenuDropdownSelect = async (key: string) => {
if (treeNode?.type === "database") {
if (key === "create-table") {
await loadSubTree(treeNode);
- // Only for MySQL.
- state.tableNameModalContext = {
- databaseId: treeNode.databaseId,
- schemaName: "",
- tableId: undefined,
- };
+ const instanceEngine = instanceStore.getInstanceById(
+ treeNode.instanceId
+ ).engine;
+ if (instanceEngine === "MYSQL") {
+ const schemaList: Schema[] =
+ editorStore.databaseSchemaById.get(treeNode.databaseId)?.schemaList ||
+ [];
+ const schema = head(schemaList);
+ if (!schema) {
+ return;
+ }
+ state.tableNameModalContext = {
+ databaseId: treeNode.databaseId,
+ schemaId: schema.id,
+ tableId: undefined,
+ };
+ }
} else if (key === "create-schema") {
state.schemaNameModalContext = {
databaseId: treeNode.databaseId,
+ schemaId: undefined,
};
}
} else if (treeNode?.type === "schema") {
@@ -705,28 +745,38 @@ const handleContextMenuDropdownSelect = async (key: string) => {
await loadSubTree(treeNode);
state.tableNameModalContext = {
databaseId: treeNode.databaseId,
- schemaName: treeNode.schemaName,
+ schemaId: treeNode.schemaId,
tableId: undefined,
};
- } else if (key === "delete-schema") {
- editorStore.deleteCreatedSchema(treeNode.databaseId, treeNode.schemaName);
+ } else if (key === "rename") {
+ state.schemaNameModalContext = {
+ databaseId: treeNode.databaseId,
+ schemaId: treeNode.schemaId,
+ };
+ } else if (key === "drop-schema") {
+ editorStore.dropSchema(treeNode.databaseId, treeNode.schemaId);
+ } else if (key === "restore") {
+ editorStore.restoreSchema(treeNode.databaseId, treeNode.schemaId);
}
} else if (treeNode?.type === "table") {
- const table = editorStore.databaseSchemaById
- .get(treeNode.databaseId)
- ?.schemaList.find((schema) => schema.name === treeNode.schemaName)
- ?.tableList.find((table) => table.id === treeNode.tableId) as Table;
-
if (key === "rename") {
state.tableNameModalContext = {
databaseId: treeNode.databaseId,
- schemaName: treeNode.schemaName,
- tableId: table.id,
+ schemaId: treeNode.schemaId,
+ tableId: treeNode.tableId,
};
} else if (key === "drop") {
- editorStore.dropTable(treeNode.databaseId, treeNode.schemaName, table);
+ editorStore.dropTable(
+ treeNode.databaseId,
+ treeNode.schemaId,
+ treeNode.tableId
+ );
} else if (key === "restore") {
- editorStore.restoreTable(table);
+ editorStore.restoreTable(
+ treeNode.databaseId,
+ treeNode.schemaId,
+ treeNode.tableId
+ );
}
}
contextMenu.showDropdown = false;
diff --git a/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue b/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue
index ee7cd75a64..d9c410d37e 100644
--- a/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue
+++ b/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue
@@ -11,7 +11,7 @@
:item-list="schemaList"
:placeholder="$t('schema-editor.schema.select')"
:show-prefix-item="true"
- @select-item="(schema: Schema) => state.referencedSchema = schema.name"
+ @select-item="(schema: Schema) => state.referencedSchemaId = schema.id"
>
<template #menuItem="{ item }">
{{ item.name }}
@@ -86,7 +86,7 @@ import {
import { BBModal, BBSelect } from "@/bbkit";
interface LocalState {
- referencedSchema?: string;
+ referencedSchemaId?: string;
referencedTableId?: string;
referencedColumnId?: string;
}
@@ -96,7 +96,7 @@ const props = defineProps({
type: Number as PropType<DatabaseId>,
default: UNKNOWN_ID,
},
- schemaName: {
+ schemaId: {
type: String as PropType<string>,
default: "",
},
@@ -122,19 +122,15 @@ const database = databaseSchema.database;
const databaseEngine = database.instance.engine;
const schemaList = databaseSchema.schemaList;
const state = reactive<LocalState>({
- referencedSchema: props.schemaName,
+ referencedSchemaId: props.schemaId,
});
const schema = computed(() => {
- return editorStore.getSchema(props.databaseId, props.schemaName) as Schema;
+ return editorStore.getSchema(props.databaseId, props.schemaId) as Schema;
});
const table = computed(() => {
- return editorStore.getTable(
- props.databaseId,
- props.schemaName,
- props.tableId
- );
+ return editorStore.getTable(props.databaseId, props.schemaId, props.tableId);
});
const propsColumn = computed(() => {
@@ -152,7 +148,7 @@ const shouldShowSchemaSelector = computed(() => {
});
const selectedSchema = computed(() => {
- return schemaList.find((schema) => schema.name === state.referencedSchema);
+ return schemaList.find((schema) => schema.id === state.referencedSchemaId);
});
const tableList = computed(() => {
@@ -201,7 +197,7 @@ onMounted(() => {
(columnId) => columnId === props.columnId
);
if (foundIndex > -1) {
- state.referencedSchema = foreignKey.value.referencedSchema;
+ state.referencedSchemaId = foreignKey.value.referencedSchemaId;
state.referencedTableId = foreignKey.value.referencedTableId;
state.referencedColumnId =
foreignKey.value.referencedColumnIdList[foundIndex];
@@ -238,7 +234,7 @@ const handleRemoveFKButtonClick = async () => {
const handleConfirmButtonClick = async () => {
if (
- isUndefined(state.referencedSchema) ||
+ isUndefined(state.referencedSchemaId) ||
isUndefined(state.referencedTableId) ||
isUndefined(state.referencedColumnId)
) {
@@ -251,7 +247,7 @@ const handleConfirmButtonClick = async () => {
name: uuidv1(),
tableId: props.tableId,
columnIdList: [column.id],
- referencedSchema: state.referencedSchema,
+ referencedSchemaId: state.referencedSchemaId,
referencedTableId: state.referencedTableId,
referencedColumnIdList: [state.referencedColumnId],
};
diff --git a/frontend/src/components/SchemaEditor/Modals/SchemaNameModal.vue b/frontend/src/components/SchemaEditor/Modals/SchemaNameModal.vue
index f18dc8d28f..c73014f842 100644
--- a/frontend/src/components/SchemaEditor/Modals/SchemaNameModal.vue
+++ b/frontend/src/components/SchemaEditor/Modals/SchemaNameModal.vue
@@ -1,6 +1,10 @@
<template>
<BBModal
- :title="$t('schema-editor.actions.create-schema')"
+ :title="
+ isCreatingSchema
+ ? $t('schema-editor.actions.create-schema')
+ : $t('schema-editor.actions.rename')
+ "
class="shadow-inner outline outline-gray-200"
@close="dismissModal"
>
@@ -19,14 +23,14 @@
{{ $t("common.cancel") }}
</button>
<button class="btn-primary" @click="handleConfirmButtonClick">
- {{ $t("common.create") }}
+ {{ isCreatingSchema ? $t("common.create") : $t("common.save") }}
</button>
</div>
</BBModal>
</template>
<script lang="ts" setup>
-import { PropType, reactive } from "vue";
+import { computed, onMounted, PropType, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { DatabaseId, SchemaEditorTabType, UNKNOWN_ID } from "@/types";
import {
@@ -48,6 +52,10 @@ const props = defineProps({
type: Number as PropType<DatabaseId>,
default: UNKNOWN_ID,
},
+ schemaId: {
+ type: String as PropType<string | undefined>,
+ default: undefined,
+ },
});
const emit = defineEmits<{
@@ -61,6 +69,21 @@ const state = reactive<LocalState>({
schemaName: "",
});
+const isCreatingSchema = computed(() => {
+ return props.schemaId === undefined;
+});
+
+onMounted(() => {
+ if (props.schemaId === undefined) {
+ return;
+ }
+
+ const schema = editorStore.getSchema(props.databaseId, props.schemaId);
+ if (schema) {
+ state.schemaName = schema.name;
+ }
+});
+
const handleSchemaNameChange = (event: Event) => {
state.schemaName = (event.target as HTMLInputElement).value;
};
@@ -91,16 +114,28 @@ const handleConfirmButtonClick = async () => {
return;
}
- const schema = convertSchemaMetadataToSchema(SchemaMetadata.fromPartial({}));
- schema.name = state.schemaName;
- schema.status = "created";
- databaseSchema.schemaList.push(schema);
- editorStore.addTab({
- id: generateUniqueTabId(),
- type: SchemaEditorTabType.TabForDatabase,
- databaseId: databaseId,
- selectedSchemaName: schema.name,
- });
+ if (isCreatingSchema.value) {
+ const schema = convertSchemaMetadataToSchema(
+ SchemaMetadata.fromPartial({})
+ );
+ schema.name = state.schemaName;
+ schema.status = "created";
+ databaseSchema.schemaList.push(schema);
+ editorStore.addTab({
+ id: generateUniqueTabId(),
+ type: SchemaEditorTabType.TabForDatabase,
+ databaseId: databaseId,
+ selectedSchemaId: schema.id,
+ });
+ } else {
+ const schema = editorStore.getSchema(
+ props.databaseId,
+ props.schemaId ?? ""
+ );
+ if (schema) {
+ schema.name = state.schemaName;
+ }
+ }
dismissModal();
};
diff --git a/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue b/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue
index 6859d0f78f..a207169d5e 100644
--- a/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue
+++ b/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue
@@ -56,7 +56,7 @@ const props = defineProps({
type: Number as PropType<DatabaseId>,
default: UNKNOWN_ID,
},
- schemaName: {
+ schemaId: {
type: String as PropType<string>,
default: "",
},
@@ -88,7 +88,7 @@ onMounted(() => {
const table = editorStore.getTable(
props.databaseId,
- props.schemaName,
+ props.schemaId,
props.tableId
);
if (table) {
@@ -111,9 +111,7 @@ const handleConfirmButtonClick = async () => {
}
const databaseId = props.databaseId;
- const schema = editorStore.databaseSchemaById
- .get(databaseId)
- ?.schemaList.find((schema) => schema.name === props.schemaName) as Schema;
+ const schema = editorStore.getSchema(databaseId, props.schemaId) as Schema;
const tableNameList = schema.tableList.map((table) => table.name);
if (tableNameList.includes(state.tableName)) {
notificationStore.pushNotification({
@@ -144,21 +142,20 @@ const handleConfirmButtonClick = async () => {
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForTable,
databaseId: props.databaseId,
- schemaName: props.schemaName,
+ schemaId: props.schemaId,
tableId: table.id,
});
- dismissModal();
} else {
const table = editorStore.getTable(
props.databaseId,
- props.schemaName,
+ props.schemaId,
props.tableId ?? ""
);
if (table) {
table.name = state.tableName;
}
- dismissModal();
}
+ dismissModal();
};
const dismissModal = () => {
diff --git a/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue b/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue
index 4547813ce3..f2e7ab2aa7 100644
--- a/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue
+++ b/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue
@@ -43,7 +43,7 @@
>
<span class="mr-1">Schema:</span>
<n-select
- v-model:value="state.selectedSchema"
+ v-model:value="state.selectedSchemaId"
class="min-w-[8rem]"
:options="schemaSelectorOptionList"
/>
@@ -171,7 +171,7 @@
<TableNameModal
v-if="state.tableNameModalContext !== undefined"
:database-id="state.tableNameModalContext.databaseId"
- :schema-name="state.tableNameModalContext.schemaName"
+ :schema-id="state.tableNameModalContext.schemaId"
:table-name="state.tableNameModalContext.tableName"
@close="state.tableNameModalContext = undefined"
/>
@@ -207,12 +207,12 @@ type TabType = "table-list" | "schema-diagram" | "raw-sql";
interface LocalState {
selectedTab: TabType;
- selectedSchema: string;
+ selectedSchemaId: string;
isFetchingDDL: boolean;
statement: string;
tableNameModalContext?: {
databaseId: DatabaseId;
- schemaName: string;
+ schemaId: string;
tableName: string | undefined;
};
}
@@ -222,7 +222,7 @@ const editorStore = useSchemaEditorStore();
const notificationStore = useNotificationStore();
const state = reactive<LocalState>({
selectedTab: "table-list",
- selectedSchema: "",
+ selectedSchemaId: "",
isFetchingDDL: false,
statement: "",
});
@@ -234,13 +234,17 @@ const databaseSchema = computed(() => {
});
const database = databaseSchema.value.database;
const databaseEngine = database.instance.engine;
-const schemaList = databaseSchema.value.schemaList;
-const tableList = computed(() => {
- return (
- schemaList.find((schema) => schema.name === state.selectedSchema)
- ?.tableList ?? []
+const schemaList = computed(() => {
+ return databaseSchema.value.schemaList;
+});
+const selectedSchema = computed(() => {
+ return schemaList.value.find(
+ (schema) => schema.id === state.selectedSchemaId
);
});
+const tableList = computed(() => {
+ return selectedSchema.value?.tableList ?? [];
+});
const shouldShowSchemaSelector = computed(() => {
return databaseEngine === "POSTGRES";
@@ -248,17 +252,21 @@ const shouldShowSchemaSelector = computed(() => {
const allowCreateTable = computed(() => {
if (databaseEngine === "POSTGRES") {
- return schemaList.length > 0 && state.selectedSchema !== "";
+ return (
+ schemaList.value.length > 0 &&
+ selectedSchema.value &&
+ selectedSchema.value.status !== "dropped"
+ );
}
return true;
});
const schemaSelectorOptionList = computed(() => {
const optionList = [];
- for (const schema of schemaList) {
+ for (const schema of schemaList.value) {
optionList.push({
label: schema.name,
- value: schema.name,
+ value: schema.id,
});
}
return optionList;
@@ -296,15 +304,15 @@ const tableHeaderList = computed(() => {
watch(
[() => currentTab.value, () => schemaList],
() => {
- const schemaNameList = schemaList.map((schema) => schema.name);
+ const schemaIdList = schemaList.value.map((schema) => schema.id);
if (
currentTab.value &&
- currentTab.value.selectedSchemaName &&
- schemaNameList.includes(currentTab.value.selectedSchemaName)
+ currentTab.value.selectedSchemaId &&
+ schemaIdList.includes(currentTab.value.selectedSchemaId)
) {
- state.selectedSchema = currentTab.value.selectedSchemaName;
+ state.selectedSchemaId = currentTab.value.selectedSchemaId;
} else {
- state.selectedSchema = head(schemaNameList) || "";
+ state.selectedSchemaId = head(schemaIdList) || "";
}
},
{
@@ -321,11 +329,13 @@ watch(
const databaseEditList: DatabaseEdit[] = [];
for (const schema of databaseSchema.value.schemaList) {
const originSchema = databaseSchema.value.originSchemaList.find(
- (originSchema) => originSchema.name === schema.name
+ (originSchema) => originSchema.id === schema.id
);
const diffSchemaResult = diffSchema(database.id, originSchema, schema);
if (
diffSchemaResult.createSchemaList.length > 0 ||
+ diffSchemaResult.renameSchemaList.length > 0 ||
+ diffSchemaResult.dropSchemaList.length > 0 ||
diffSchemaResult.createTableList.length > 0 ||
diffSchemaResult.alterTableList.length > 0 ||
diffSchemaResult.renameTableList.length > 0 ||
@@ -376,11 +386,16 @@ const handleChangeTab = (tab: TabType) => {
};
const handleCreateNewTable = () => {
- state.tableNameModalContext = {
- databaseId: database.id,
- schemaName: state.selectedSchema,
- tableName: undefined,
- };
+ const selectedSchema = schemaList.value.find(
+ (schema) => schema.id === state.selectedSchemaId
+ );
+ if (selectedSchema) {
+ state.tableNameModalContext = {
+ databaseId: database.id,
+ schemaId: selectedSchema.id,
+ tableName: undefined,
+ };
+ }
};
const handleTableItemClick = (table: Table) => {
@@ -388,17 +403,17 @@ const handleTableItemClick = (table: Table) => {
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForTable,
databaseId: database.id,
- schemaName: state.selectedSchema,
+ schemaId: state.selectedSchemaId,
tableId: table.id,
});
};
const handleDropTable = (table: Table) => {
- editorStore.dropTable(database.id, state.selectedSchema, table);
+ editorStore.dropTable(database.id, state.selectedSchemaId, table.id);
};
const handleRestoreTable = (table: Table) => {
- editorStore.restoreTable(table);
+ editorStore.restoreTable(database.id, state.selectedSchemaId, table.id);
};
const {
diff --git a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
index 3e3a9077b2..4f01af9b58 100644
--- a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
@@ -27,8 +27,8 @@
<div class="w-full py-2 flex flex-row justify-between items-center">
<div>
<button
- class="flex flex-row justify-center items-center border px-3 py-1 leading-6 text-sm text-gray-700 rounded cursor-pointer hover:opacity-80"
- :disabled="isDroppedTable"
+ class="flex flex-row justify-center items-center border px-3 py-1 leading-6 text-sm text-gray-700 rounded cursor-pointer hover:opacity-80 disabled:cursor-not-allowed disabled:opacity-60"
+ :disabled="disableChangeTable"
@click="handleAddColumn"
>
<heroicons-outline:plus class="w-4 h-auto mr-1 text-gray-400" />
@@ -181,19 +181,20 @@
class="column-field-text italic text-gray-400 !w-auto"
>EMPTY</span
>
- <span
- class="foreign-key-edit-button hidden cursor-pointer hover:opacity-80"
+ <button
+ class="foreign-key-edit-button hidden cursor-pointer hover:opacity-80 disabled:cursor-not-allowed disabled:opacity-60"
+ :disabled="disableAlterColumn(column)"
@click="handleEditColumnForeignKey(column)"
>
<heroicons:pencil-square class="w-4 h-auto text-gray-400" />
- </span>
+ </button>
</div>
<div class="w-full flex justify-start items-center">
<n-tooltip v-if="!isDroppedColumn(column)" trigger="hover">
<template #trigger>
<button
- :disabled="isDroppedTable"
- class="text-gray-500 cursor-pointer hover:opacity-80"
+ :disabled="disableChangeTable"
+ class="text-gray-500 cursor-pointer hover:opacity-80 disabled:cursor-not-allowed disabled:opacity-60"
@click="handleDropColumn(column)"
>
<heroicons:trash class="w-4 h-auto" />
@@ -204,8 +205,8 @@
<n-tooltip v-else trigger="hover">
<template #trigger>
<button
- class="text-gray-500 cursor-pointer hover:opacity-80"
- :disabled="isDroppedTable"
+ class="text-gray-500 cursor-pointer hover:opacity-80 disabled:cursor-not-allowed disabled:opacity-60"
+ :disabled="disableChangeTable"
@click="handleRestoreColumn(column)"
>
<heroicons:arrow-uturn-left class="w-4 h-auto" />
@@ -245,7 +246,7 @@
<EditColumnForeignKeyModal
v-if="state.showEditColumnForeignKeyModal && editForeignKeyColumn"
:database-id="currentTab.databaseId"
- :schema-name="schema.name"
+ :schema-id="schema.id"
:table-id="table.id"
:column-id="editForeignKeyColumn.id"
@close="state.showEditColumnForeignKeyModal = false"
@@ -300,7 +301,7 @@ const state = reactive<LocalState>({
const table = ref(editorStore.getTableWithTableTab(currentTab) as Table);
const schema = computed(() => {
return databaseSchema.schemaList.find(
- (schema) => schema.name === currentTab.schemaName
+ (schema) => schema.id === currentTab.schemaId
) as Schema;
});
const foreignKeyList = computed(() => {
@@ -312,6 +313,10 @@ const foreignKeyList = computed(() => {
const tableEditorContainerRef = ref<HTMLDivElement>();
const editForeignKeyColumn = ref<Column>();
+const isDroppedSchema = computed(() => {
+ return schema.value.status === "dropped";
+});
+
const isDroppedTable = computed(() => {
return table.value.status === "dropped";
});
@@ -324,7 +329,7 @@ const allowResetTable = computed(() => {
return (
isTableChanged(
currentTab.databaseId,
- currentTab.schemaName,
+ currentTab.schemaId,
currentTab.tableId
) || isDroppedTable.value
);
@@ -393,7 +398,7 @@ watch(
if (state.selectedTab === "raw-sql") {
const originSchema = editorStore.getOriginSchema(
currentTab.databaseId,
- currentTab.schemaName
+ currentTab.schemaId
) as Schema;
const diffSchemaResult = diffSchema(
currentTab.databaseId,
@@ -403,6 +408,8 @@ watch(
const databaseEdit: DatabaseEdit = {
databaseId: currentTab.databaseId,
createSchemaList: [],
+ renameSchemaList: [],
+ dropSchemaList: [],
createTableList: diffSchemaResult.createTableList.filter(
(context) => context.name === table.value.name
),
@@ -464,9 +471,13 @@ const getReferencedForeignKeyName = (column: Column) => {
if (isUndefined(fk) || isUndefined(index) || index < 0) {
return;
}
+ const referencedSchema = editorStore.getSchema(
+ currentTab.databaseId,
+ fk.referencedSchemaId
+ );
const referencedTable = editorStore.getTable(
currentTab.databaseId,
- fk.referencedSchema,
+ fk.referencedSchemaId,
fk.referencedTableId
);
if (!referencedTable) {
@@ -478,7 +489,7 @@ const getReferencedForeignKeyName = (column: Column) => {
if (databaseEngine.value === "MYSQL") {
return `${referencedTable.name}(${referColumn?.name})`;
} else {
- return `${fk.referencedSchema}.${referencedTable.name}(${referColumn?.name})`;
+ return `${referencedSchema?.name}.${referencedTable.name}(${referColumn?.name})`;
}
};
@@ -486,8 +497,14 @@ const isDroppedColumn = (column: Column): boolean => {
return column.status === "dropped";
};
+const disableChangeTable = (): boolean => {
+ return isDroppedSchema.value || isDroppedTable.value;
+};
+
const disableAlterColumn = (column: Column): boolean => {
- return isDroppedTable.value || isDroppedColumn(column);
+ return (
+ isDroppedSchema.value || isDroppedTable.value || isDroppedColumn(column)
+ );
};
const setColumnPrimaryKey = (column: Column, isPrimaryKey: boolean) => {
@@ -577,11 +594,11 @@ const handleDiscardChanges = () => {
const originSchema = editorStore.getOriginSchema(
currentTab.databaseId,
- currentTab.schemaName
+ currentTab.schemaId
);
const originTable = editorStore.getOriginTable(
currentTab.databaseId,
- currentTab.schemaName,
+ currentTab.schemaId,
table.value.id
);
diff --git a/frontend/src/components/SchemaEditor/TabsContainer.vue b/frontend/src/components/SchemaEditor/TabsContainer.vue
index 5f42d2d8db..f52ffb248c 100644
--- a/frontend/src/components/SchemaEditor/TabsContainer.vue
+++ b/frontend/src/components/SchemaEditor/TabsContainer.vue
@@ -89,7 +89,7 @@ const getTabComputedClassList = (tab: TabContext) => {
return ["text-green-700"];
}
- if (isTableChanged(tab.databaseId, tab.schemaName, tab.tableId)) {
+ if (isTableChanged(tab.databaseId, tab.schemaId, tab.tableId)) {
return ["text-yellow-700"];
}
}
@@ -106,7 +106,7 @@ const getTabName = (tab: TabContext) => {
} else if (tab.type === SchemaEditorTabType.TabForTable) {
const table = editorStore.getTable(
tab.databaseId,
- tab.schemaName,
+ tab.schemaId,
tab.tableId
);
return `${table?.name || ""}`;
diff --git a/frontend/src/components/SchemaEditor/utils/schema.ts b/frontend/src/components/SchemaEditor/utils/schema.ts
new file mode 100644
index 0000000000..ea3b065635
--- /dev/null
+++ b/frontend/src/components/SchemaEditor/utils/schema.ts
@@ -0,0 +1,14 @@
+import { isEqual } from "lodash-es";
+import { useSchemaEditorStore } from "@/store";
+import { DatabaseId } from "@/types";
+
+export const isSchemaChanged = (
+ databaseId: DatabaseId,
+ schemaId: string
+): boolean => {
+ const editorStore = useSchemaEditorStore();
+ const originSchema = editorStore.getOriginSchema(databaseId, schemaId);
+ const schema = editorStore.getSchema(databaseId, schemaId);
+
+ return !isEqual(originSchema, schema);
+};
diff --git a/frontend/src/components/SchemaEditor/utils/table.ts b/frontend/src/components/SchemaEditor/utils/table.ts
index 346794bc36..e79cc08995 100644
--- a/frontend/src/components/SchemaEditor/utils/table.ts
+++ b/frontend/src/components/SchemaEditor/utils/table.ts
@@ -4,12 +4,12 @@ import { DatabaseId } from "@/types";
export const isTableChanged = (
databaseId: DatabaseId,
- schemaName: string,
+ schemaId: string,
tableId: string
): boolean => {
const editorStore = useSchemaEditorStore();
- const originSchema = editorStore.getOriginSchema(databaseId, schemaName);
- const schema = editorStore.getSchema(databaseId, schemaName);
+ const originSchema = editorStore.getOriginSchema(databaseId, schemaId);
+ const schema = editorStore.getSchema(databaseId, schemaId);
const table = schema?.tableList.find((table) => table.id === tableId);
const originTable = originSchema?.tableList.find(
(table) => table.id === tableId
diff --git a/frontend/src/components/SchemaEditor/utils/useMetadataForDiagram.ts b/frontend/src/components/SchemaEditor/utils/useMetadataForDiagram.ts
index 197b05fc4e..5dc5588b3f 100644
--- a/frontend/src/components/SchemaEditor/utils/useMetadataForDiagram.ts
+++ b/frontend/src/components/SchemaEditor/utils/useMetadataForDiagram.ts
@@ -101,7 +101,7 @@ export const useMetadataForDiagram = (
// In PostgreSQL, foreign keys can cross different schemas.
// So we need to search the schemaList here.
const refSchema = schemaList.find(
- (schema) => schema.name === fk.referencedSchema
+ (schema) => schema.id === fk.referencedSchemaId
)!;
const refTable = refSchema.tableList.find(
(table) => table.id === fk.referencedTableId
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 88142a68b6..e812db4d46 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1440,8 +1440,8 @@
"sync-sql-from-schema-editor": "Sync SQL from Schema Editor",
"raw-sql": "Raw SQL",
"actions": {
- "create-schema": "Create Schema",
- "delete-schema": "Delete Schema",
+ "create-schema": "Create schema",
+ "drop-schema": "Drop schema",
"create-table": "New table",
"rename": "Rename",
"drop-table": "Drop table",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 50bd2b9562..d27ff1ce5b 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1441,7 +1441,7 @@
"raw-sql": "SQL 语句",
"actions": {
"create-schema": "新建 Schema",
- "delete-schema": "删除 Schema",
+ "drop-schema": "删除 Schema",
"create-table": "新建表",
"rename": "重命名",
"drop-table": "删除表",
diff --git a/frontend/src/store/modules/schemaEditor.ts b/frontend/src/store/modules/schemaEditor.ts
index 103f18e8c2..f1e5c94302 100644
--- a/frontend/src/store/modules/schemaEditor.ts
+++ b/frontend/src/store/modules/schemaEditor.ts
@@ -187,68 +187,83 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {
return this.databaseSchemaById.get(databaseId)!.schemaList;
},
- // We only allow to delete the new created schema.
- deleteCreatedSchema(databaseId: DatabaseId, schemaName: string) {
- const databaseSchema = this.databaseSchemaById.get(databaseId);
- if (databaseSchema) {
- databaseSchema.schemaList =
- this.databaseSchemaById
- .get(databaseId)
- ?.schemaList.filter((schema) => schema.name !== schemaName) || [];
+ getSchema(databaseId: DatabaseId, schemaId: string) {
+ return this.databaseSchemaById
+ .get(databaseId)
+ ?.schemaList.find((schema) => schema.id === schemaId);
+ },
+ getOriginSchema(databaseId: DatabaseId, schemaId: string) {
+ return this.databaseSchemaById
+ .get(databaseId)
+ ?.originSchemaList.find((schema) => schema.id === schemaId);
+ },
+ dropSchema(databaseId: DatabaseId, schemaId: string) {
+ const schema = this.getSchema(databaseId, schemaId);
+ if (!schema) {
+ return;
+ }
- // Close related tabs.
- for (const tab of this.tabList) {
- if (tab.databaseId !== databaseId) {
- continue;
- }
+ if (schema.status === "created") {
+ const databaseSchema = this.databaseSchemaById.get(databaseId);
+ if (databaseSchema) {
+ databaseSchema.schemaList =
+ this.databaseSchemaById
+ .get(databaseId)
+ ?.schemaList.filter((schema) => schema.id !== schemaId) || [];
+
+ // Close related tabs.
+ for (const tab of this.tabList) {
+ if (tab.databaseId !== databaseId) {
+ continue;
+ }
- if (
- tab.type === SchemaEditorTabType.TabForTable &&
- tab.schemaName === schemaName
- ) {
- this.closeTab(tab.id);
+ if (
+ tab.type === SchemaEditorTabType.TabForTable &&
+ tab.schemaId === schemaId
+ ) {
+ this.closeTab(tab.id);
+ }
}
}
+ } else {
+ schema.status = "dropped";
}
},
- getSchema(databaseId: DatabaseId, schemaName: string) {
- return this.databaseSchemaById
- .get(databaseId)
- ?.schemaList.find((schema) => schema.name === schemaName);
- },
- getOriginSchema(databaseId: DatabaseId, schemaName: string) {
- return this.databaseSchemaById
- .get(databaseId)
- ?.originSchemaList.find((schema) => schema.name === schemaName);
+ restoreSchema(databaseId: DatabaseId, schemaId: string) {
+ const schema = this.getSchema(databaseId, schemaId);
+ if (!schema) {
+ return;
+ }
+
+ schema.status = "normal";
},
- getTable(databaseId: DatabaseId, schemaName: string, tableId: string) {
- return this.databaseSchemaById
- .get(databaseId)
- ?.schemaList.find((schema) => schema.name === schemaName)
- ?.tableList.find((table) => table.id === tableId);
+ getTable(databaseId: DatabaseId, schemaId: string, tableId: string) {
+ return this.getSchema(databaseId, schemaId)?.tableList.find(
+ (table) => table.id === tableId
+ );
},
- getOriginTable(
- databaseId: DatabaseId,
- schemaName: string,
- tableId: string
- ) {
- return this.databaseSchemaById
- .get(databaseId)
- ?.originSchemaList.find((schema) => schema.name === schemaName)
- ?.tableList.find((table) => table.id === tableId);
+ getOriginTable(databaseId: DatabaseId, schemaId: string, tableId: string) {
+ return this.getOriginSchema(databaseId, schemaId)?.tableList.find(
+ (table) => table.id === tableId
+ );
},
getTableWithTableTab(tab: TableTabContext) {
return this.databaseSchemaById
.get(tab.databaseId)
- ?.schemaList.find((schema) => schema.name === tab.schemaName)
+ ?.schemaList.find((schema) => schema.id === tab.schemaId)
?.tableList?.find((table) => table.id === tab.tableId);
},
- dropTable(databaseId: DatabaseId, schemaName: string, table: Table) {
+ dropTable(databaseId: DatabaseId, schemaId: string, tableId: string) {
+ const table = this.getTable(databaseId, schemaId, tableId);
+ if (!table) {
+ return;
+ }
+
// Remove table record and close tab for created table.
if (table.status === "created") {
const tableList = this.databaseSchemaById
.get(databaseId)
- ?.schemaList.find((schema) => schema.name === schemaName)
+ ?.schemaList.find((schema) => schema.id === schemaId)
?.tableList as Table[];
const index = tableList.findIndex((item) => item.id === table.id);
tableList.splice(index, 1);
@@ -260,7 +275,12 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {
table.status = "dropped";
}
},
- restoreTable(table: Table) {
+ restoreTable(databaseId: DatabaseId, schemaId: string, tableId: string) {
+ const table = this.getTable(databaseId, schemaId, tableId);
+ if (!table) {
+ return;
+ }
+
table.status = "normal";
},
async postDatabaseEdit(databaseEdit: DatabaseEdit) {
diff --git a/frontend/src/types/schemaEditor/atomType.ts b/frontend/src/types/schemaEditor/atomType.ts
index 7fb0bff8bf..0c2f8ade42 100644
--- a/frontend/src/types/schemaEditor/atomType.ts
+++ b/frontend/src/types/schemaEditor/atomType.ts
@@ -42,12 +42,13 @@ export interface ForeignKey {
name: string;
tableId: string;
columnIdList: string[];
- referencedSchema: string;
+ referencedSchemaId: string;
referencedTableId: string;
referencedColumnIdList: string[];
}
export interface Schema {
+ id: string;
// It should be an empty string for MySQL/TiDB.
name: string;
tableList: Table[];
@@ -124,6 +125,7 @@ export const convertSchemaMetadataToSchema = (
}
return {
+ id: uuidv1(),
name: schemaMetadata.name,
tableList: tableList,
foreignKeyList: [],
@@ -173,7 +175,7 @@ export const convertSchemaMetadataList = (
name: foreignKeyMetadata.name,
tableId: table.id,
columnIdList: [],
- referencedSchema: foreignKeyMetadata.referencedSchema,
+ referencedSchemaId: referencedSchema.id,
referencedTableId: referencedTable.id,
referencedColumnIdList: [],
};
diff --git a/frontend/src/types/schemaEditor/index.ts b/frontend/src/types/schemaEditor/index.ts
index a5d3115e7b..604e87f2f9 100644
--- a/frontend/src/types/schemaEditor/index.ts
+++ b/frontend/src/types/schemaEditor/index.ts
@@ -11,7 +11,7 @@ export interface DatabaseTabContext {
id: string;
type: SchemaEditorTabType.TabForDatabase;
databaseId: DatabaseId;
- selectedSchemaName?: string;
+ selectedSchemaId?: string;
}
// Tab context for editing table.
@@ -19,7 +19,7 @@ export interface TableTabContext {
id: string;
type: SchemaEditorTabType.TabForTable;
databaseId: DatabaseId;
- schemaName: string;
+ schemaId: string;
tableId: string;
}
@@ -48,6 +48,8 @@ export interface DatabaseEdit {
databaseId: DatabaseId;
createSchemaList: CreateSchemaContext[];
+ renameSchemaList: RenameSchemaContext[];
+ dropSchemaList: DropSchemaContext[];
createTableList: CreateTableContext[];
alterTableList: AlterTableContext[];
renameTableList: RenameTableContext[];
@@ -58,6 +60,15 @@ export interface CreateSchemaContext {
schema: string;
}
+export interface RenameSchemaContext {
+ oldName: string;
+ newName: string;
+}
+
+export interface DropSchemaContext {
+ schema: string;
+}
+
export interface CreateTableContext {
schema: string;
name: string;
diff --git a/frontend/src/utils/schemaEditor/diffSchema.ts b/frontend/src/utils/schemaEditor/diffSchema.ts
index f5adc5dec3..1e7049d778 100644
--- a/frontend/src/utils/schemaEditor/diffSchema.ts
+++ b/frontend/src/utils/schemaEditor/diffSchema.ts
@@ -4,7 +4,9 @@ import {
CreateSchemaContext,
CreateTableContext,
DatabaseId,
+ DropSchemaContext,
DropTableContext,
+ RenameSchemaContext,
RenameTableContext,
} from "@/types";
import { Schema } from "@/types/schemaEditor/atomType";
@@ -19,10 +21,22 @@ export const diffSchema = (
) => {
const editorStore = useSchemaEditorStore();
const createSchemaContextList: CreateSchemaContext[] = [];
+ const renameSchemaContextList: RenameSchemaContext[] = [];
+ const dropSchemaContextList: DropSchemaContext[] = [];
+ if (originSchema && originSchema.name !== schema.name) {
+ renameSchemaContextList.push({
+ oldName: originSchema.name,
+ newName: schema.name,
+ });
+ }
if (schema.status === "created") {
createSchemaContextList.push({
schema: schema.name,
});
+ } else if (schema.status === "dropped") {
+ dropSchemaContextList.push({
+ schema: schema.name,
+ });
}
const createTableContextList: CreateTableContext[] = [];
@@ -46,12 +60,16 @@ export const diffSchema = (
(fk) => fk.tableId === table.id
);
for (const foreignKey of foreignKeyList) {
+ const referencedSchema = editorStore.getSchema(
+ databaseId,
+ foreignKey.referencedSchemaId
+ );
const referencedTable = editorStore.getTable(
databaseId,
- foreignKey.referencedSchema,
+ foreignKey.referencedSchemaId,
foreignKey.referencedTableId
);
- if (referencedTable) {
+ if (referencedSchema && referencedTable) {
const columnNameList: string[] = [];
const referencedColumnNameList: string[] = [];
for (const columnId of foreignKey.columnIdList) {
@@ -73,7 +91,7 @@ export const diffSchema = (
createTableContext.addForeignKeyList.push({
columnList: columnNameList,
- referencedSchema: foreignKey.referencedSchema,
+ referencedSchema: referencedSchema.name,
referencedTable: referencedTable.name,
referencedColumnList: referencedColumnNameList,
});
@@ -196,7 +214,7 @@ export const diffSchema = (
) {
const referencedSchema = editorStore.getSchema(
databaseId,
- foreignKey.referencedSchema
+ foreignKey.referencedSchemaId
);
const referencedTable = referencedSchema?.tableList.find(
(table) => table.id === foreignKey.referencedTableId
@@ -258,6 +276,8 @@ export const diffSchema = (
return {
createSchemaList: createSchemaContextList,
+ renameSchemaList: renameSchemaContextList,
+ dropSchemaList: dropSchemaContextList,
createTableList: createTableContextList,
alterTableList: alterTableContextList,
renameTableList: renameTableContextList,
diff --git a/plugin/parser/edit/pg/deparse.go b/plugin/parser/edit/pg/deparse.go
index 2c36ef3aec..bf431df0fe 100644
--- a/plugin/parser/edit/pg/deparse.go
+++ b/plugin/parser/edit/pg/deparse.go
@@ -83,9 +83,11 @@ func transformCreateSchemaContext(ctx *DeparseContext, createSchemaContext *api.
}
func transformRenameSchemaContext(ctx *DeparseContext, renameSchemaContext *api.RenameSchemaContext) error {
- // TODO(steven): support rename schema with our pg parser.
- alterSchemaStmt := fmt.Sprintf(`ALTER SCHEMA "%s" RENAME TO "%s";`, renameSchemaContext.OldName, renameSchemaContext.NewName)
- ctx.StmtList = append(ctx.StmtList, alterSchemaStmt)
+ dropSchemaStmt := &ast.RenameSchemaStmt{
+ Schema: renameSchemaContext.OldName,
+ NewName: renameSchemaContext.NewName,
+ }
+ ctx.NodeList = append(ctx.NodeList, dropSchemaStmt)
return nil
}
diff --git a/plugin/parser/edit/pg/deparse_test.go b/plugin/parser/edit/pg/deparse_test.go
index bc86ac87a7..843624b790 100644
--- a/plugin/parser/edit/pg/deparse_test.go
+++ b/plugin/parser/edit/pg/deparse_test.go
@@ -24,11 +24,11 @@ func TestDeparseRenameSchema(t *testing.T) {
RenameSchemaList: []*api.RenameSchemaContext{
{
OldName: "public",
- NewName: "private",
+ NewName: "protected",
},
},
},
- want: "ALTER SCHEMA \"public\" RENAME TO \"private\";",
+ want: "ALTER SCHEMA \"public\" RENAME TO \"protected\";",
},
}