Skip to content

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

fix: show schema name for postgres tables (#3937)

boojack9d3a37c4088b
diff --git a/frontend/src/components/DatabaseOverviewPanel.vue b/frontend/src/components/DatabaseOverviewPanel.vue
index ef7a4537b5..0e7b332cce 100644
--- a/frontend/src/components/DatabaseOverviewPanel.vue
+++ b/frontend/src/components/DatabaseOverviewPanel.vue
@@ -72,18 +72,46 @@
</dl>
<div class="pt-6">
+ <div
+ v-if="hasSchemaProperty"
+ class="flex flex-row justify-start items-center mb-4"
+ >
+ <span class="text-lg leading-6 font-medium text-main mr-2">Schema</span>
+ <BBSelect
+ class="!w-auto min-w-[12rem]"
+ :selected-item="state.selectedSchemaName"
+ :item-list="schemaNameList"
+ :placeholder="$t('database.schema.select')"
+ :show-prefix-item="true"
+ @select-item="(schema: string) => state.selectedSchemaName = schema"
+ >
+ <template #menuItem="{ item: schema }">
+ {{ schema }}
+ </template>
+ </BBSelect>
+ </div>
+
<div class="text-lg leading-6 font-medium text-main mb-4">
<span v-if="databaseEngine === 'MONGODB'">{{
$t("db.collections")
}}</span>
<span v-else>{{ $t("db.tables") }}</span>
</div>
- <TableTable :database="database" :table-list="tableList" />
+
+ <TableTable
+ :database="database"
+ :schema-name="state.selectedSchemaName"
+ :table-list="tableList"
+ />
<div class="mt-6 text-lg leading-6 font-medium text-main mb-4">
{{ $t("db.views") }}
</div>
- <ViewTable :view-list="viewList" />
+ <ViewTable
+ :database="database"
+ :schema-name="state.selectedSchemaName"
+ :view-list="viewList"
+ />
<template v-if="database.instance.engine == 'POSTGRES'">
<div class="mt-6 text-lg leading-6 font-medium text-main mb-4">
@@ -185,21 +213,14 @@
</div>
</template>
-<script lang="ts">
-import {
- computed,
- reactive,
- watchEffect,
- PropType,
- defineComponent,
-} from "vue";
-import { useRouter } from "vue-router";
+<script lang="ts" setup>
+import { computed, reactive, watchEffect, PropType } from "vue";
import AnomalyTable from "../components/AnomalyTable.vue";
import DataSourceTable from "../components/DataSourceTable.vue";
import DataSourceConnectionPanel from "../components/DataSourceConnectionPanel.vue";
import TableTable from "../components/TableTable.vue";
import ViewTable from "../components/ViewTable.vue";
-import { hasWorkspacePermission, timezoneString, instanceSlug } from "../utils";
+import { hasWorkspacePermission } from "../utils";
import {
Anomaly,
Database,
@@ -207,7 +228,7 @@ import {
DataSourcePatch,
EngineType,
} from "../types";
-import { cloneDeep, isEqual } from "lodash-es";
+import { cloneDeep, head, isEqual } from "lodash-es";
import { BBTableSectionDataSource } from "../bbkit/types";
import {
featureToRef,
@@ -218,172 +239,161 @@ import {
} from "@/store";
interface LocalState {
+ selectedSchemaName: string;
editingDataSource?: DataSource;
}
-export default defineComponent({
- name: "DatabaseOverviewPanel",
- components: {
- AnomalyTable,
- DataSourceConnectionPanel,
- DataSourceTable,
- TableTable,
- ViewTable,
+const props = defineProps({
+ database: {
+ required: true,
+ type: Object as PropType<Database>,
},
- props: {
- database: {
- required: true,
- type: Object as PropType<Database>,
- },
- },
- setup(props) {
- const router = useRouter();
- const dataSourceStore = useDataSourceStore();
-
- const state = reactive<LocalState>({});
+});
- const currentUser = useCurrentUser();
- const dbSchemaStore = useDBSchemaStore();
+const dataSourceStore = useDataSourceStore();
- const databaseEngine = props.database.instance.engine as EngineType;
+const state = reactive<LocalState>({
+ selectedSchemaName: "",
+});
- const prepareDatabaseMetadata = () => {
- dbSchemaStore.getOrFetchDatabaseMetadataById(props.database.id);
- };
+const currentUser = useCurrentUser();
+const dbSchemaStore = useDBSchemaStore();
- watchEffect(prepareDatabaseMetadata);
+const databaseEngine = props.database.instance.engine as EngineType;
+const hasSchemaProperty =
+ databaseEngine === "POSTGRES" || databaseEngine === "SNOWFLAKE";
- const anomalyList = useAnomalyList(
- computed(() => ({ databaseId: props.database.id }))
- );
+const prepareDatabaseMetadata = async () => {
+ await dbSchemaStore.getOrFetchDatabaseMetadataById(props.database.id);
+ if (hasSchemaProperty && schemaList.value.length > 0) {
+ state.selectedSchemaName = head(schemaList.value)?.name || "";
+ }
+};
- const anomalySectionList = computed(
- (): BBTableSectionDataSource<Anomaly>[] => {
- const list: BBTableSectionDataSource<Anomaly>[] = [];
- if (anomalyList.value.length > 0) {
- list.push({
- title: props.database.name,
- list: anomalyList.value,
- });
- }
- return list;
- }
- );
+watchEffect(prepareDatabaseMetadata);
- const hasDataSourceFeature = featureToRef("bb.feature.data-source");
+const anomalyList = useAnomalyList(
+ computed(() => ({ databaseId: props.database.id }))
+);
- const tableList = computed(() => {
- return dbSchemaStore.getTableListByDatabaseId(props.database.id);
+const anomalySectionList = computed((): BBTableSectionDataSource<Anomaly>[] => {
+ const list: BBTableSectionDataSource<Anomaly>[] = [];
+ if (anomalyList.value.length > 0) {
+ list.push({
+ title: props.database.name,
+ list: anomalyList.value,
});
+ }
+ return list;
+});
- const viewList = computed(() => {
- return dbSchemaStore.getViewListByDatabaseId(props.database.id);
- });
+const hasDataSourceFeature = featureToRef("bb.feature.data-source");
- const dbExtensionList = computed(() => {
- return dbSchemaStore.getExtensionListByDatabaseId(props.database.id);
- });
+const schemaList = computed(() => {
+ return dbSchemaStore.getSchemaListByDatabaseId(props.database.id);
+});
- const allowConfigInstance = computed(() => {
- return hasWorkspacePermission(
- "bb.permission.workspace.manage-instance",
- currentUser.value.role
- );
- });
+const schemaNameList = computed(() => {
+ return schemaList.value.map((schema) => schema.name);
+});
- const allowViewDataSource = computed(() => {
- if (allowConfigInstance.value) {
- return true;
- }
+const tableList = computed(() => {
+ if (hasSchemaProperty) {
+ return (
+ schemaList.value.find(
+ (schema) => schema.name === state.selectedSchemaName
+ )?.tables || []
+ );
+ }
+ return dbSchemaStore.getTableListByDatabaseId(props.database.id);
+});
- for (const member of props.database.project.memberList) {
- if (member.principal.id == currentUser.value.id) {
- return true;
- }
- }
+const viewList = computed(() => {
+ if (hasSchemaProperty) {
+ return (
+ schemaList.value.find(
+ (schema) => schema.name === state.selectedSchemaName
+ )?.views || []
+ );
+ }
+ return dbSchemaStore.getViewListByDatabaseId(props.database.id);
+});
- return false;
- });
+const dbExtensionList = computed(() => {
+ return dbSchemaStore.getExtensionListByDatabaseId(props.database.id);
+});
- const dataSourceList = computed(() => {
- return props.database.dataSourceList;
- });
+const allowConfigInstance = computed(() => {
+ return hasWorkspacePermission(
+ "bb.permission.workspace.manage-instance",
+ currentUser.value.role
+ );
+});
- const readWriteDataSourceList = computed(() => {
- return dataSourceList.value.filter((dataSource: DataSource) => {
- return dataSource.type == "RW";
- });
- });
+const allowViewDataSource = computed(() => {
+ if (allowConfigInstance.value) {
+ return true;
+ }
- const readonlyDataSourceList = computed(() => {
- return dataSourceList.value.filter((dataSource: DataSource) => {
- return dataSource.type == "RO";
- });
- });
+ for (const member of props.database.project.memberList) {
+ if (member.principal.id == currentUser.value.id) {
+ return true;
+ }
+ }
- const isEditingDataSource = (dataSource: DataSource) => {
- return (
- state.editingDataSource && state.editingDataSource.id == dataSource.id
- );
- };
-
- const allowSaveDataSource = computed(() => {
- for (const dataSource of dataSourceList.value) {
- if (dataSource.id == state.editingDataSource!.id) {
- return !isEqual(dataSource, state.editingDataSource);
- }
- }
- return false;
- });
+ return false;
+});
- const editDataSource = (dataSource: DataSource) => {
- state.editingDataSource = cloneDeep(dataSource);
- };
+const dataSourceList = computed(() => {
+ return props.database.dataSourceList;
+});
- const cancelEditDataSource = () => {
- state.editingDataSource = undefined;
- };
-
- const saveEditDataSource = () => {
- const dataSourcePatch = {
- username: state.editingDataSource?.username,
- password: state.editingDataSource?.password,
- } as DataSourcePatch;
- dataSourceStore
- .patchDataSource({
- databaseId: state.editingDataSource?.databaseId as number,
- dataSourceId: state.editingDataSource?.id as number,
- dataSource: dataSourcePatch,
- })
- .then(() => {
- state.editingDataSource = undefined;
- });
- };
-
- const configInstance = () => {
- router.push(`/instance/${instanceSlug(props.database.instance)}`);
- };
-
- return {
- timezoneString,
- state,
- anomalySectionList,
- tableList,
- viewList,
- dbExtensionList,
- hasDataSourceFeature,
- allowConfigInstance,
- allowViewDataSource,
- readWriteDataSourceList,
- readonlyDataSourceList,
- isEditingDataSource,
- allowSaveDataSource,
- editDataSource,
- cancelEditDataSource,
- saveEditDataSource,
- configInstance,
- databaseEngine,
- };
- },
+const readWriteDataSourceList = computed(() => {
+ return dataSourceList.value.filter((dataSource: DataSource) => {
+ return dataSource.type == "RW";
+ });
});
+
+const readonlyDataSourceList = computed(() => {
+ return dataSourceList.value.filter((dataSource: DataSource) => {
+ return dataSource.type == "RO";
+ });
+});
+
+const isEditingDataSource = (dataSource: DataSource) => {
+ return state.editingDataSource && state.editingDataSource.id == dataSource.id;
+};
+
+const allowSaveDataSource = computed(() => {
+ for (const dataSource of dataSourceList.value) {
+ if (dataSource.id == state.editingDataSource!.id) {
+ return !isEqual(dataSource, state.editingDataSource);
+ }
+ }
+ return false;
+});
+
+const editDataSource = (dataSource: DataSource) => {
+ state.editingDataSource = cloneDeep(dataSource);
+};
+
+const cancelEditDataSource = () => {
+ state.editingDataSource = undefined;
+};
+
+const saveEditDataSource = () => {
+ const dataSourcePatch = {
+ username: state.editingDataSource?.username,
+ password: state.editingDataSource?.password,
+ } as DataSourcePatch;
+ dataSourceStore
+ .patchDataSource({
+ databaseId: state.editingDataSource?.databaseId as number,
+ dataSourceId: state.editingDataSource?.id as number,
+ dataSource: dataSourcePatch,
+ })
+ .then(() => {
+ state.editingDataSource = undefined;
+ });
+};
</script>
diff --git a/frontend/src/components/TableTable.vue b/frontend/src/components/TableTable.vue
index f73dab72ae..523293ee46 100644
--- a/frontend/src/components/TableTable.vue
+++ b/frontend/src/components/TableTable.vue
@@ -12,7 +12,7 @@
<template #body="{ rowData: table }">
<BBTableCell :left-padding="4">
<div class="flex items-center space-x-2">
- <EllipsisText>{{ table.name }}</EllipsisText>
+ <EllipsisText>{{ getTableName(table.name) }}</EllipsisText>
<BBBadge
v-if="isGhostTable(table)"
text="gh-ost"
@@ -73,6 +73,10 @@ export default defineComponent({
required: true,
type: Object as PropType<Database>,
},
+ schemaName: {
+ type: String,
+ default: "",
+ },
tableList: {
required: true,
type: Object as PropType<TableMetadata[]>,
@@ -86,12 +90,20 @@ export default defineComponent({
showReservedTableList: false,
});
- const isPostgres = computed(
- () => props.database.instance.engine === "POSTGRES"
- );
+ const isPostgres = props.database.instance.engine === "POSTGRES";
+
+ const hasSchemaProperty =
+ isPostgres || props.database.instance.engine === "SNOWFLAKE";
+
+ const getTableName = (tableName: string) => {
+ if (hasSchemaProperty) {
+ return `"${props.schemaName}"."${tableName}"`;
+ }
+ return tableName;
+ };
const columnList = computed(() => {
- if (isPostgres.value) {
+ if (isPostgres) {
return [
{
title: t("common.name"),
@@ -146,9 +158,12 @@ export default defineComponent({
return tableList;
});
- const clickTable = (section: number, row: number, e: MouseEvent) => {
+ const clickTable = (_: number, row: number, e: MouseEvent) => {
const table = mixedTableList.value[row];
- const url = `/db/${databaseSlug(props.database)}/table/${table.name}`;
+ let url = `/db/${databaseSlug(props.database)}/table/${table.name}`;
+ if (props.schemaName !== "") {
+ url = url + `?schema=${props.schemaName}`;
+ }
if (e.ctrlKey || e.metaKey) {
window.open(url, "_blank");
} else {
@@ -165,6 +180,7 @@ export default defineComponent({
hasReservedTables,
mixedTableList,
isGhostTable,
+ getTableName,
};
},
});
diff --git a/frontend/src/components/ViewTable.vue b/frontend/src/components/ViewTable.vue
index c1dc61e1d3..5191762806 100644
--- a/frontend/src/components/ViewTable.vue
+++ b/frontend/src/components/ViewTable.vue
@@ -9,7 +9,7 @@
>
<template #body="{ rowData: view }">
<BBTableCell :left-padding="4" class="w-16">
- {{ view.name }}
+ {{ getViewName(view.name) }}
</BBTableCell>
<BBTableCell class="w-64">
{{ view.definition }}
@@ -25,18 +25,32 @@
import { computed, PropType } from "vue";
import { useI18n } from "vue-i18n";
import { ViewMetadata } from "@/types/proto/database";
+import { Database } from "@/types";
export default {
name: "ViewTable",
components: {},
props: {
+ database: {
+ required: true,
+ type: Object as PropType<Database>,
+ },
+ schemaName: {
+ type: String,
+ default: "",
+ },
viewList: {
required: true,
type: Object as PropType<ViewMetadata[]>,
},
},
- setup() {
+ setup(props) {
const { t } = useI18n();
+
+ const hasSchemaProperty =
+ props.database.instance.engine === "POSTGRES" ||
+ props.database.instance.engine === "SNOWFLAKE";
+
const columnList = computed(() => [
{
title: t("common.name"),
@@ -49,8 +63,17 @@ export default {
},
]);
+ const getViewName = (viewName: string) => {
+ if (hasSchemaProperty) {
+ return `"${props.schemaName}"."${viewName}"`;
+ }
+ return viewName;
+ };
+
return {
columnList,
+ hasSchemaProperty,
+ getViewName,
};
},
};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index c06c0070ed..8cef0e0462 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -986,7 +986,10 @@
"preview-issue": "Preview issue"
},
"sensitive": "Sensitive",
- "access-denied": "You don't have the permission to access this database."
+ "access-denied": "You don't have the permission to access this database.",
+ "schema": {
+ "select": "Select schema"
+ }
},
"repository": {
"branch-observe-file-change": "The branch where Bytebase observes the file change.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 9d78a48e84..6117839de0 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -986,7 +986,10 @@
"preview-issue": "预览工单"
},
"sensitive": "敏感",
- "access-denied": "你没有访问该数据库的权限"
+ "access-denied": "你没有访问该数据库的权限",
+ "schema": {
+ "select": "选择 schema"
+ }
},
"repository": {
"branch-observe-file-change": "Bytebase 跟踪文件变更的分支。",
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 808738284b..75baca7ecb 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -779,7 +779,12 @@ const routes: Array<RouteRecordRaw> = [
name: "workspace.database.table.detail",
meta: {
title: (route: RouteLocationNormalized) => {
- return `${t("db.tables")} - ${route.params.tableName}`;
+ const schemaName = route.query.schema?.toString() || "";
+ let tableName = route.params.tableName;
+ if (schemaName) {
+ tableName = `"${schemaName}"."${tableName}"`;
+ }
+ return `${t("db.tables")} - ${tableName}`;
},
allowBookmark: true,
},
diff --git a/frontend/src/store/modules/dbSchema.ts b/frontend/src/store/modules/dbSchema.ts
index f4ff95aca6..5449857572 100644
--- a/frontend/src/store/modules/dbSchema.ts
+++ b/frontend/src/store/modules/dbSchema.ts
@@ -4,6 +4,7 @@ import { DatabaseId, DBSchemaState } from "@/types";
import {
DatabaseMetadata,
ExtensionMetadata,
+ SchemaMetadata,
TableMetadata,
ViewMetadata,
} from "@/types/proto/database";
@@ -27,6 +28,22 @@ export const useDBSchemaStore = defineStore("dbSchema", {
this.databaseMetadataById.set(databaseId, databaseMetadata);
return databaseMetadata;
},
+ async getOrFetchSchemaListByDatabaseId(
+ databaseId: DatabaseId
+ ): Promise<SchemaMetadata[]> {
+ if (!this.databaseMetadataById.has(databaseId)) {
+ await this.getOrFetchDatabaseMetadataById(databaseId);
+ }
+ return this.getSchemaListByDatabaseId(databaseId);
+ },
+ getSchemaListByDatabaseId(databaseId: DatabaseId): SchemaMetadata[] {
+ const databaseMetadata = this.databaseMetadataById.get(databaseId);
+ if (!databaseMetadata) {
+ return [];
+ }
+
+ return databaseMetadata.schemas;
+ },
async getOrFetchTableListByDatabaseId(
databaseId: DatabaseId
): Promise<TableMetadata[]> {
diff --git a/frontend/src/views/TableDetail.vue b/frontend/src/views/TableDetail.vue
index 49ef413cc0..907a9ea1cf 100644
--- a/frontend/src/views/TableDetail.vue
+++ b/frontend/src/views/TableDetail.vue
@@ -1,5 +1,9 @@
<template>
- <div class="flex-1 overflow-auto focus:outline-none" tabindex="0">
+ <div
+ v-if="table"
+ class="flex-1 overflow-auto focus:outline-none"
+ tabindex="0"
+ >
<main class="flex-1 relative pb-8 overflow-y-auto">
<!-- Highlight Panel -->
<div
@@ -13,8 +17,7 @@
<h1
class="pt-2 pb-2.5 text-xl font-bold leading-6 text-main truncate flex items-center gap-x-3"
>
- {{ table.name }}
-
+ {{ getTableName(table.name) }}
<BBBadge
v-if="isGhostTable(table)"
text="gh-ost"
@@ -195,7 +198,8 @@
</template>
<script lang="ts">
-import { computed, defineComponent } from "vue";
+import { computed, defineComponent, onMounted, ref } from "vue";
+import { useRoute, useRouter } from "vue-router";
import {
bytesToString,
connectionSlug,
@@ -227,18 +231,40 @@ export default defineComponent({
},
},
setup(props) {
+ const route = useRoute();
+ const router = useRouter();
const databaseStore = useDatabaseStore();
- const databaseId = idFromSlug(props.databaseSlug);
const dbSchemaStore = useDBSchemaStore();
+ const table = ref<TableMetadata>();
+ const databaseId = idFromSlug(props.databaseSlug);
+ const schemaName = (route.query.schema as string) || "";
const database = computed(() => {
return databaseStore.getDatabaseById(databaseId);
});
- const table = computed(() => {
- return dbSchemaStore.getTableByDatabaseIdAndTableName(
- databaseId,
- props.tableName
- ) as TableMetadata;
+ const hasSchemaProperty = computed(
+ () => database.value.instance.engine === "POSTGRES"
+ );
+ const getTableName = (tableName: string) => {
+ if (hasSchemaProperty.value) {
+ return `"${schemaName}"."${tableName}"`;
+ }
+ return tableName;
+ };
+
+ onMounted(() => {
+ const schemaList = dbSchemaStore.getSchemaListByDatabaseId(databaseId);
+ const schema = schemaList.find((schema) => schema.name === schemaName);
+ if (schema) {
+ table.value = schema.tables.find(
+ (table) => table.name === props.tableName
+ );
+ }
+ if (!table.value) {
+ router.replace({
+ name: "error.404",
+ });
+ }
});
const gotoSQLEditor = () => {
@@ -268,6 +294,7 @@ export default defineComponent({
return {
table,
database,
+ getTableName,
gotoSQLEditor,
bytesToString,
isGhostTable,