Skip to content

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

Add tabletable to database detail page and /database/<<id>>/table api

天舟52ee2786017b
diff --git a/backend/server/acl_casbin_policy_dba.csv b/backend/server/acl_casbin_policy_dba.csv
index 66a3f1e26d..5737ee9828 100644
--- a/backend/server/acl_casbin_policy_dba.csv
+++ b/backend/server/acl_casbin_policy_dba.csv
@@ -20,6 +20,7 @@ p, DBA, /database, POST
p, DBA, /database, GET
p, DBA, /database/{id}, GET
p, DBA, /database/{id}, PATCH
+p, DBA, /database/{id}/table, GET
p, DBA, /issue, POST
p, DBA, /issue, GET
p, DBA, /issue/{id}, GET
diff --git a/backend/server/acl_casbin_policy_developer.csv b/backend/server/acl_casbin_policy_developer.csv
index 0f9a7f4e6b..334316cc41 100644
--- a/backend/server/acl_casbin_policy_developer.csv
+++ b/backend/server/acl_casbin_policy_developer.csv
@@ -16,6 +16,7 @@ p, DEVELOPER, /database, POST
p, DEVELOPER, /database, GET
p, DEVELOPER, /database/{id}, GET
p, DEVELOPER, /database/{id}, PATCH
+p, DEVELOPER, /database/{id}/table, GET
p, DEVELOPER, /issue, POST
p, DEVELOPER, /issue, GET
p, DEVELOPER, /issue/{id}, GET
diff --git a/backend/server/acl_casbin_policy_owner.csv b/backend/server/acl_casbin_policy_owner.csv
index 48d37196cd..366447beb3 100644
--- a/backend/server/acl_casbin_policy_owner.csv
+++ b/backend/server/acl_casbin_policy_owner.csv
@@ -25,6 +25,7 @@ p, OWNER, /database, POST
p, OWNER, /database, GET
p, OWNER, /database/{id}, GET
p, OWNER, /database/{id}, PATCH
+p, OWNER, /database/{id}/table, GET
p, OWNER, /issue, POST
p, OWNER, /issue, GET
p, OWNER, /issue/{id}, GET
diff --git a/backend/server/database.go b/backend/server/database.go
index 8a5dc98ecb..7ae7cd474c 100644
--- a/backend/server/database.go
+++ b/backend/server/database.go
@@ -128,6 +128,42 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
}
return nil
})
+
+ g.GET("/database/:id/table", func(c echo.Context) error {
+ id, err := strconv.Atoi(c.Param("id"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)
+ }
+
+ databaseFind := &api.DatabaseFind{
+ ID: &id,
+ }
+ database, err := s.DatabaseService.FindDatabase(context.Background(), databaseFind)
+ if err != nil {
+ if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))
+ }
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)
+ }
+
+ tableFind := &api.TableFind{
+ DatabaseId: &id,
+ }
+ tableList, err := s.TableService.FindTableList(context.Background(), tableFind)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch table list for database id: %d", id)).SetInternal(err)
+ }
+
+ for _, table := range tableList {
+ table.Database = database
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, tableList); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal fetch table response: %v", id)).SetInternal(err)
+ }
+ return nil
+ })
}
func (s *Server) ComposeDatabaseById(ctx context.Context, id int, includeList []string) (*api.Database, error) {
diff --git a/frontend/src/components/TableTable.vue b/frontend/src/components/TableTable.vue
new file mode 100644
index 0000000000..34944e1c1c
--- /dev/null
+++ b/frontend/src/components/TableTable.vue
@@ -0,0 +1,89 @@
+<template>
+ <BBTable
+ :columnList="columnList"
+ :dataSource="tableList"
+ :showHeader="true"
+ :leftBordered="true"
+ :rightBordered="true"
+ :rowClickable="false"
+ >
+ <template v-slot:body="{ rowData: table }">
+ <BBTableCell :leftPadding="4" class="w-16">
+ {{ table.name }}
+ </BBTableCell>
+ <BBTableCell class="w-8">
+ {{ table.engine }}
+ </BBTableCell>
+ <BBTableCell class="w-8">
+ {{ table.collation }}
+ </BBTableCell>
+ <BBTableCell class="w-16">
+ {{ table.rowCount }}
+ </BBTableCell>
+ <BBTableCell class="w-8">
+ {{ bytesToString(table.dataSize) }}
+ </BBTableCell>
+ <BBTableCell class="w-8">
+ {{ bytesToString(table.indexSize) }}
+ </BBTableCell>
+ <BBTableCell class="w-8">
+ {{ table.syncStatus }}
+ </BBTableCell>
+ <BBTableCell class="w-16">
+ {{ humanizeTs(table.lastSuccessfulSyncTs) }}
+ </BBTableCell>
+ </template>
+ </BBTable>
+</template>
+
+<script lang="ts">
+import { PropType } from "vue";
+import { useStore } from "vuex";
+import { BBTableColumn } from "../bbkit/types";
+import { Table } from "../types";
+import { bytesToString } from "../utils";
+
+const columnList: BBTableColumn[] = [
+ {
+ title: "Name",
+ },
+ {
+ title: "Engine",
+ },
+ {
+ title: "Collation",
+ },
+ {
+ title: "Row count est.",
+ },
+ {
+ title: "Data size",
+ },
+ {
+ title: "Index size",
+ },
+ {
+ title: "Sync status",
+ },
+ {
+ title: "Last successful sync",
+ },
+];
+
+export default {
+ name: "TableTable",
+ components: {},
+ props: {
+ tableList: {
+ required: true,
+ type: Object as PropType<Table[]>,
+ },
+ },
+ setup(props, ctx) {
+ return {
+ columnList,
+ bytesToString,
+ };
+ },
+};
+</script>
diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.ts
index f407b381ee..8cae35b721 100644
--- a/frontend/src/store/index.ts
+++ b/frontend/src/store/index.ts
@@ -10,6 +10,7 @@ import project from "./modules/project";
import instance from "./modules/instance";
import dataSource from "./modules/dataSource";
import database from "./modules/database";
+import table from "./modules/table";
import sql from "./modules/sql";
import principal from "./modules/principal";
import plan from "./modules/plan";
@@ -43,6 +44,7 @@ export const store: Store<any> = createStore({
instance,
dataSource,
database,
+ table,
sql,
principal,
plan,
diff --git a/frontend/src/store/modules/table.ts b/frontend/src/store/modules/table.ts
new file mode 100644
index 0000000000..262d732071
--- /dev/null
+++ b/frontend/src/store/modules/table.ts
@@ -0,0 +1,71 @@
+import axios from "axios";
+import {
+ ResourceObject,
+ Principal,
+ TableState,
+ DatabaseId,
+ Table,
+} from "../../types";
+
+function convert(table: ResourceObject, rootGetters: any): Table {
+ const creator = table.attributes.creator as Principal;
+ const updater = table.attributes.updater as Principal;
+
+ return {
+ ...(table.attributes as Omit<Table, "id" | "creator" | "updater">),
+ id: parseInt(table.id),
+ creator,
+ updater,
+ };
+}
+
+const state: () => TableState = () => ({
+ tableListByDatabaseId: new Map(),
+});
+
+const getters = {
+ tableListByDatabaseId:
+ (state: TableState) =>
+ (databaseId: DatabaseId): Table[] => {
+ return state.tableListByDatabaseId.get(databaseId) || [];
+ },
+};
+
+const actions = {
+ async fetchTableListByDatabaseId(
+ { commit, rootGetters }: any,
+ databaseId: DatabaseId
+ ) {
+ const tableList = (
+ await axios.get(`/api/database/${databaseId}/table`)
+ ).data.data.map((table: ResourceObject) => {
+ return convert(table, rootGetters);
+ });
+
+ commit("setTableListByDatabaseId", { databaseId, tableList });
+ return tableList;
+ },
+};
+
+const mutations = {
+ setTableListByDatabaseId(
+ state: TableState,
+ {
+ databaseId,
+ tableList,
+ }: {
+ databaseId: DatabaseId;
+ tableList: Table[];
+ }
+ ) {
+ state.tableListByDatabaseId.set(databaseId, tableList);
+ },
+};
+
+export default {
+ namespaced: true,
+ state,
+ getters,
+ actions,
+ mutations,
+};
diff --git a/frontend/src/types/id.ts b/frontend/src/types/id.ts
index fa152a624a..55fcb3412f 100644
--- a/frontend/src/types/id.ts
+++ b/frontend/src/types/id.ts
@@ -35,6 +35,8 @@ export type DataSourceId = IdType;
export type DatabaseId = IdType;
+export type TableId = IdType;
+
export type CommandId = IdType;
export type CommandRegisterId = IdType;
diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts
index 59ade5a837..3ca61fb7c6 100644
--- a/frontend/src/types/index.ts
+++ b/frontend/src/types/index.ts
@@ -8,6 +8,7 @@ export * from "./issue";
export * from "./pipeline";
export * from "./instance";
export * from "./database";
+export * from "./table";
export * from "./dataSource";
export * from "./sql";
export * from "./bookmark";
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index e30ecf7ddb..e42d6184d4 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -11,6 +11,7 @@ import {
IssueId,
ProjectId,
PrincipalId,
+ DatabaseId,
} from "./id";
import { Instance } from "./instance";
import { Issue } from "./issue";
@@ -20,6 +21,7 @@ import { PlanType } from "./plan";
import { Principal } from "./principal";
import { Project } from "./project";
import { Notification } from "./notification";
+import { Table } from "./table";
export interface AuthState {
currentUser: Principal;
@@ -90,6 +92,10 @@ export interface DatabaseState {
databaseListByInstanceId: Map<InstanceId, Database[]>;
}
+export interface TableState {
+ tableListByDatabaseId: Map<DatabaseId, Table[]>;
+}
+
export interface NotificationState {
notificationByModule: Map<string, Notification[]>;
}
diff --git a/frontend/src/types/table.ts b/frontend/src/types/table.ts
new file mode 100644
index 0000000000..b88e4496d4
--- /dev/null
+++ b/frontend/src/types/table.ts
@@ -0,0 +1,34 @@
+// "OK" means find the exact match
+// "DRIFTED" means we find the table with the same name, but the fingerprint is different,
+// this usually indicates the underlying table has been recreated (might for a entirely different purpose)
+// "NOT_FOUND" means no matching table name found, this ususally means someone changes
+
+import { Database } from "./database";
+import { TableId } from "./id";
+import { Principal } from "./principal";
+
+// the underlying table name.
+export type TableSyncStatus = "OK" | "DRIFTED" | "NOT_FOUND";
+// Table
+export type Table = {
+ id: TableId;
+
+ // Related fields
+ database: Database;
+
+ // Standard fields
+ creator: Principal;
+ createdTs: number;
+ updater: Principal;
+ updatedTs: number;
+
+ // Domain specific fields
+ name: string;
+ engine: string;
+ collation: string;
+ rowCount: number;
+ dataSize: number;
+ indexSize: number;
+ syncStatus: TableSyncStatus;
+ lastSuccessfulSyncTs: number;
+};
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index 2cbc0b5506..8bce53d6d7 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -30,6 +30,18 @@ export function humanizeTs(ts: number) {
return time.local().format("MMM D YYYY");
}
+export function bytesToString(size: number) {
+ const unitList = ["B", "KB", "MB", "GB", "TB"];
+ let i = 0;
+ for (i = 0; i < unitList.length; i++) {
+ if (size < 1024) {
+ break;
+ }
+ size = size / 1024;
+ }
+ return size.toString() + " " + unitList[i];
+}
+
export function urlfy(str: string) {
let result = str.trim();
if (result.search(/^http[s]?\:\/\//) == -1) {
diff --git a/frontend/src/views/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail.vue
index 648d77cf3f..3ed36682dd 100644
--- a/frontend/src/views/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail.vue
@@ -3,7 +3,14 @@
<main class="flex-1 relative pb-8 overflow-y-auto">
<!-- Highlight Panel -->
<div
- class="px-4 pb-4 border-b border-block-border md:flex md:items-center md:justify-between"
+ class="
+ px-4
+ pb-4
+ border-b border-block-border
+ md:flex
+ md:items-center
+ md:justify-between
+ "
>
<div class="flex-1 min-w-0">
<!-- Summary -->
@@ -11,7 +18,15 @@
<div>
<div class="flex items-center">
<h1
- class="pt-2 pb-2.5 text-xl font-bold leading-6 text-main truncate"
+ class="
+ pt-2
+ pb-2.5
+ text-xl
+ font-bold
+ leading-6
+ text-main
+ truncate
+ "
>
{{ database.name }}
</h1>
@@ -19,7 +34,12 @@
</div>
</div>
<dl
- class="flex flex-col space-y-1 md:space-y-0 md:flex-row md:flex-wrap"
+ class="
+ flex flex-col
+ space-y-1
+ md:space-y-0
+ md:flex-row md:flex-wrap
+ "
>
<dt class="sr-only">Environment</dt>
<dd class="flex items-center text-sm md:mr-4">
@@ -121,6 +141,13 @@
</div>
</dl>
+ <div class="pt-6">
+ <div class="text-lg leading-6 font-medium text-main mb-4">
+ Tables
+ </div>
+ <TableTable :tableList="tableList" />
+ </div>
+
<!-- Hide data source list for now, as we don't allow adding new data source after creating the database. -->
<div v-if="false" class="pt-6">
<DataSourceTable
@@ -280,6 +307,7 @@ import { useStore } from "vuex";
import { useRouter } from "vue-router";
import DataSourceTable from "../components/DataSourceTable.vue";
import DataSourceConnectionPanel from "../components/DataSourceConnectionPanel.vue";
+import TableTable from "../components/TableTable.vue";
import PrincipalSelect from "../components/PrincipalSelect.vue";
import ProjectSelect from "../components/ProjectSelect.vue";
import { idFromSlug, isDBAOrOwner } from "../utils";
@@ -303,6 +331,7 @@ export default {
components: {
DataSourceConnectionPanel,
DataSourceTable,
+ TableTable,
PrincipalSelect,
ProjectSelect,
},
@@ -317,6 +346,15 @@ export default {
const currentUser = computed(() => store.getters["auth/currentUser"]());
+ const prepareTableList = () => {
+ store.dispatch(
+ "table/fetchTableListByDatabaseId",
+ idFromSlug(props.databaseSlug)
+ );
+ };
+
+ watchEffect(prepareTableList);
+
const hasDataSourceFeature = computed(() =>
store.getters["plan/feature"]("bb.data-source")
);
@@ -327,6 +365,12 @@ export default {
);
});
+ const tableList = computed(() => {
+ return store.getters["table/tableListByDatabaseId"](
+ idFromSlug(props.databaseSlug)
+ );
+ });
+
const isCurrentUserDBAOrOwner = computed((): boolean => {
return isDBAOrOwner(currentUser.value.role);
});
@@ -440,6 +484,7 @@ export default {
return {
state,
database,
+ tableList,
hasDataSourceFeature,
isCurrentUserDBAOrOwner,
allowChangeProject,