Skip to content

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

Add database model

天舟6d25d6757bee
diff --git a/frontend/src/components/DataSourceTable.vue b/frontend/src/components/DataSourceTable.vue
index ff626e75bc..1a4d967ca7 100644
--- a/frontend/src/components/DataSourceTable.vue
+++ b/frontend/src/components/DataSourceTable.vue
@@ -43,11 +43,18 @@
class="w-4 table-cell"
:title="columnList[5].title"
/>
+ <BBTableHeaderCell
+ class="w-4 table-cell"
+ :title="columnList[6].title"
+ />
</template>
<template v-slot:body="{ rowData: dataSource }">
<BBTableCell :leftPadding="4" class="w-4 table-cell text-gray-500">
<span class="">{{ dataSource.name }}</span>
</BBTableCell>
+ <BBTableCell class="w-24 table-cell">
+ {{ dataSource.database }}
+ </BBTableCell>
<BBTableCell class="w-24 table-cell">
{{ dataSource.type }}
</BBTableCell>
@@ -81,6 +88,9 @@ const columnList: BBTableColumn[] = [
{
title: "Name",
},
+ {
+ title: "Database",
+ },
{
title: "Type",
},
@@ -145,9 +155,9 @@ export default {
) {
if (item.type === "ADMIN") {
adminList.push(item);
- } else if (item.type === "READWRITE") {
+ } else if (item.type === "RW") {
readWriteList.push(item);
- } else if (item.type === "READONLY") {
+ } else if (item.type === "RO") {
readOnlyList.push(item);
}
}
diff --git a/frontend/src/miragejs/factories/dataSource.js b/frontend/src/miragejs/factories/dataSource.js
index c25d564b5c..59f97a1ee1 100644
--- a/frontend/src/miragejs/factories/dataSource.js
+++ b/frontend/src/miragejs/factories/dataSource.js
@@ -16,10 +16,10 @@ export default {
return "ADMIN";
}
if (i % 3 == 1) {
- return "READWRITE";
+ return "RW";
}
if (i % 3 == 2) {
- return "READONLY";
+ return "RO";
}
},
username() {
diff --git a/frontend/src/miragejs/factories/database.js b/frontend/src/miragejs/factories/database.js
new file mode 100644
index 0000000000..7091221647
--- /dev/null
+++ b/frontend/src/miragejs/factories/database.js
@@ -0,0 +1,30 @@
+import { Factory } from "miragejs";
+import faker from "faker";
+
+export default {
+ database: Factory.extend({
+ name(i) {
+ return "db" + i;
+ },
+ createdTs(i) {
+ return Date.now() - (i + 1) * 1800 * 1000;
+ },
+ lastUpdatedTs(i) {
+ return Date.now() - i * 3600 * 1000;
+ },
+ syncStatus(i) {
+ if (i % 3 == 0) {
+ return "OK";
+ }
+ if (i % 3 == 1) {
+ return "MISMATCH";
+ }
+ if (i % 3 == 2) {
+ return "NOT_FOUND";
+ }
+ },
+ fingerprint(i) {
+ return faker.fake("{{random.alpha}}");
+ },
+ }),
+};
diff --git a/frontend/src/miragejs/factories/index.js b/frontend/src/miragejs/factories/index.js
index 4f38727c3c..206567175e 100644
--- a/frontend/src/miragejs/factories/index.js
+++ b/frontend/src/miragejs/factories/index.js
@@ -3,6 +3,7 @@ import bookmark from "./bookmark";
import environment from "./environment";
import instance from "./instance";
import dataSource from "./dataSource";
+import database from "./database";
import task from "./task";
import taskPatch from "./taskPatch";
import activity from "./activity";
@@ -26,6 +27,7 @@ export default {
...environment,
...instance,
...dataSource,
+ ...database,
...task,
...taskPatch,
...activity,
diff --git a/frontend/src/miragejs/factories/instance.js b/frontend/src/miragejs/factories/instance.js
index d3ddd725e5..1eb739ee8c 100644
--- a/frontend/src/miragejs/factories/instance.js
+++ b/frontend/src/miragejs/factories/instance.js
@@ -41,9 +41,40 @@ export default {
afterCreate(instance, server) {
server.create("dataSource", {
instance,
- name: instance.name + " ds1",
+ name: instance.name + " admin ds1",
type: "ADMIN",
});
+
+ for (let i = 0; i < 3; i++) {
+ const db = server.create("database", {
+ instance,
+ name: instance.name + " db1",
+ });
+
+ server.create("dataSource", {
+ instance,
+ name: instance.name + " " + db.name + " admin",
+ type: "ADMIN",
+ username: "root",
+ password: "pwdRoot",
+ });
+
+ server.create("dataSource", {
+ instance,
+ name: instance.name + " " + db.name + " rw",
+ type: "RW",
+ username: "rootRW",
+ password: "pwdRW",
+ });
+
+ server.create("dataSource", {
+ instance,
+ name: instance.name + " " + db.name + " ro",
+ type: "RO",
+ username: "rootRO",
+ password: "pwdRO",
+ });
+ }
},
}),
};
diff --git a/frontend/src/miragejs/models/index.js b/frontend/src/miragejs/models/index.js
index bd73da35c5..32250ea7c6 100644
--- a/frontend/src/miragejs/models/index.js
+++ b/frontend/src/miragejs/models/index.js
@@ -49,10 +49,16 @@ export default {
instance: Model.extend({
workspace: belongsTo(),
dataSource: hasMany(),
+ database: hasMany(),
}),
dataSource: Model.extend({
instance: belongsTo(),
+ database: hasMany(),
+ }),
+
+ database: Model.extend({
+ instance: belongsTo(),
}),
batchUpdate: Model,
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index e694f02a3c..3b5fb4fdba 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -448,6 +448,21 @@ export default function routes() {
}
);
+ // Database
+ this.get("/instance/:instanceId/database", function (schema, request) {
+ const instance = schema.databases.find(request.params.instanceId);
+ if (instance) {
+ return schema.databases.where((database) => {
+ return database.instanceId == instance.id;
+ });
+ }
+ return new Response(
+ 404,
+ {},
+ { errors: "Instance " + request.params.instanceId + " not found" }
+ );
+ });
+
// Bookmark
this.get("/bookmark", function (schema, request) {
const {
diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.ts
index 3a27a21b89..0781567cf8 100644
--- a/frontend/src/store/index.ts
+++ b/frontend/src/store/index.ts
@@ -7,6 +7,7 @@ import roleMapping from "./modules/roleMapping";
import environment from "./modules/environment";
import instance from "./modules/instance";
import dataSource from "./modules/dataSource";
+import database from "./modules/database";
import principal from "./modules/principal";
import auth from "./modules/auth";
import task from "./modules/task";
@@ -29,6 +30,7 @@ export const store: Store<any> = createStore({
environment,
instance,
dataSource,
+ database,
principal,
auth,
task,
diff --git a/frontend/src/store/modules/database.ts b/frontend/src/store/modules/database.ts
new file mode 100644
index 0000000000..500c301b0f
--- /dev/null
+++ b/frontend/src/store/modules/database.ts
@@ -0,0 +1,71 @@
+import axios from "axios";
+import {
+ Database,
+ InstanceId,
+ DatabaseState,
+ ResourceObject,
+} from "../../types";
+import { isDevOrDemo, randomString } from "../../utils";
+
+function convert(database: ResourceObject, rootGetters: any): Database {
+ const instance = rootGetters["instance/instanceById"](
+ database.attributes.instanceId
+ );
+ return {
+ id: database.id,
+ instance,
+ ...(database.attributes as Omit<Database, "id" | "instance">),
+ };
+}
+
+const state: () => DatabaseState = () => ({
+ databaseListByInstanceId: new Map(),
+});
+
+const getters = {
+ databaseListByInstanceId: (state: DatabaseState) => (
+ instanceId: InstanceId
+ ): Database[] => {
+ return state.databaseListByInstanceId.get(instanceId) || [];
+ },
+};
+
+const actions = {
+ async fetchDatabaseListByInstanceId(
+ { commit, rootGetters }: any,
+ instanceId: InstanceId
+ ) {
+ const databaseList = (
+ await axios.get(`/api/instance/${instanceId}/database`)
+ ).data.data.map((database: ResourceObject) => {
+ return convert(database, rootGetters);
+ });
+
+ commit("setDatabaseListByInstanceId", { instanceId, databaseList });
+
+ return databaseList;
+ },
+};
+
+const mutations = {
+ setDatabaseListByInstanceId(
+ state: DatabaseState,
+ {
+ instanceId,
+ databaseList,
+ }: {
+ instanceId: InstanceId;
+ databaseList: Database[];
+ }
+ ) {
+ state.databaseListByInstanceId.set(instanceId, databaseList);
+ },
+};
+
+export default {
+ namespaced: true,
+ state,
+ getters,
+ actions,
+ mutations,
+};
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index c8d58e308a..ae976d569e 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -229,7 +229,7 @@ export type Instance = {
};
export type InstanceNew = Omit<Instance, "id">;
-export type DataSourceType = "ADMIN" | "READWRITE" | "READONLY";
+export type DataSourceType = "ADMIN" | "RW" | "RO";
// Data Source
export type DataSource = {
id: string;
@@ -240,6 +240,8 @@ export type DataSource = {
// In mysql, username can be empty which means anonymous user
username?: string;
password?: string;
+ // If empty, it means it can access all databases from an instance.
+ database?: string;
};
export type DataSourceNew = {
name: string;
@@ -248,6 +250,26 @@ export type DataSourceNew = {
password?: string;
};
+// We periodically sync the underlying db schema and stores those info
+// in the "database" object.
+
+// "OK" means find the exact match
+// "MISMATCH" means we find the database with the same name, but the fingerprint is different,
+// this usually indicates the underlying database has been recreated (might for a entirely different purpose)
+// "NOT_FOUND" means no matching database name found, this ususally means someone changes
+// the underlying db name.
+export type DatabaseSyncStatus = "OK" | "MISMATCH" | "NOT_FOUND";
+// Database
+export type Database = {
+ id: string;
+ name: string;
+ createdTs: number;
+ lastUpdatedTs: number;
+ syncStatus: DatabaseSyncStatus;
+ fingerprint: string;
+ instance: Instance;
+};
+
// Auth
export type LoginInfo = {
email: string;
@@ -341,6 +363,10 @@ export interface DataSourceState {
dataSourceById: Map<DataSourceId, DataSource>;
}
+export interface DatabaseState {
+ databaseListByInstanceId: Map<InstanceId, Database[]>;
+}
+
export interface NotificationState {
notificationByModule: Map<string, Notification[]>;
}
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
index 1b915e8105..c87e9f0764 100644
--- a/frontend/src/views/InstanceDetail.vue
+++ b/frontend/src/views/InstanceDetail.vue
@@ -298,7 +298,7 @@ input[type="number"] {
</template>
<script lang="ts">
-import { computed, onMounted, onUnmounted, reactive } from "vue";
+import { computed, onMounted, onUnmounted, reactive, watchEffect } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import cloneDeep from "lodash-es/cloneDeep";
@@ -367,6 +367,19 @@ export default {
document.removeEventListener("keydown", escHandler);
});
+ const prepareDatabaseList = () => {
+ store
+ .dispatch(
+ "database/fetchDatabaseListByInstanceId",
+ idFromSlug(props.instanceSlug)
+ )
+ .catch((error) => {
+ console.log(error);
+ });
+ };
+
+ watchEffect(prepareDatabaseList);
+
const assignInstance = (instance: Instance | InstanceNew) => {
if (!state.new) {
state.originalInstance = instance as Instance;