Skip to content

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

Create a seperate model for InstanceNew and post a message to all DBA and owners when creating a new instance.

天舟7bd507d23edd
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index 474c23f31d..32c75be4f6 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -314,6 +314,7 @@ export default {
instance: props.instance
? cloneDeep(props.instance)
: {
+ creatorId: currentUser.value.id,
environmentId: UNKNOWN_ID,
name: "New Instance",
host: "127.0.0.1",
diff --git a/frontend/src/miragejs/factories/index.js b/frontend/src/miragejs/factories/index.js
index d7e92ab50a..e505ec7ade 100644
--- a/frontend/src/miragejs/factories/index.js
+++ b/frontend/src/miragejs/factories/index.js
@@ -2,6 +2,7 @@ import user from "./user";
import bookmark from "./bookmark";
import environment from "./environment";
import instance from "./instance";
+import instanceNew from "./instanceNew";
import dataSource from "./dataSource";
import dataSourceMember from "./dataSourceMember";
import database from "./database";
@@ -30,6 +31,7 @@ export default {
...bookmark,
...environment,
...instance,
+ ...instanceNew,
...dataSource,
...dataSourceMember,
...database,
diff --git a/frontend/src/miragejs/factories/instance.js b/frontend/src/miragejs/factories/instance.js
index 9b73f99aa5..fe09ebc843 100644
--- a/frontend/src/miragejs/factories/instance.js
+++ b/frontend/src/miragejs/factories/instance.js
@@ -1,11 +1,18 @@
import { Factory } from "miragejs";
import faker from "faker";
+import { UNKNOWN_ID } from "../../types";
export default {
instance: Factory.extend({
name(i) {
return "instance " + (i + i);
},
+ creatorId() {
+ return UNKNOWN_ID;
+ },
+ updaterId() {
+ return UNKNOWN_ID;
+ },
createdTs(i) {
return Date.now() - (i + 1) * 1800 * 1000;
},
diff --git a/frontend/src/miragejs/factories/instanceNew.js b/frontend/src/miragejs/factories/instanceNew.js
new file mode 100644
index 0000000000..41b149b8a5
--- /dev/null
+++ b/frontend/src/miragejs/factories/instanceNew.js
@@ -0,0 +1,42 @@
+import { Factory } from "miragejs";
+import faker from "faker";
+import { UNKNOWN_ID } from "../../types";
+
+export default {
+ instanceNew: Factory.extend({
+ creatorId() {
+ return UNKNOWN_ID;
+ },
+ environmentId() {
+ return UNKNOWN_ID;
+ },
+ name(i) {
+ return "instance new" + (i + i);
+ },
+ externalLink() {
+ return "google.com";
+ },
+ host(i) {
+ if (i == 0) {
+ return "localhost";
+ } else if (i == 1) {
+ return "127.0.0.1";
+ } else if (i == 2) {
+ return "13.24.32.122";
+ } else {
+ return "mydb.com";
+ }
+ },
+ port(i) {
+ if (i == 0) {
+ return "3306";
+ } else if (i == 1) {
+ return "";
+ } else if (i == 2) {
+ return "15202";
+ } else {
+ return "5432";
+ }
+ },
+ }),
+};
diff --git a/frontend/src/miragejs/models/index.js b/frontend/src/miragejs/models/index.js
index 23eb1f8340..4ae2066b2e 100644
--- a/frontend/src/miragejs/models/index.js
+++ b/frontend/src/miragejs/models/index.js
@@ -61,6 +61,8 @@ export default {
database: hasMany(),
}),
+ instanceNew: Model,
+
database: Model.extend({
workspace: belongsTo(),
instance: belongsTo(),
diff --git a/frontend/src/miragejs/routes/instance.js b/frontend/src/miragejs/routes/instance.js
index 2282234044..855865dab0 100644
--- a/frontend/src/miragejs/routes/instance.js
+++ b/frontend/src/miragejs/routes/instance.js
@@ -21,10 +21,58 @@ export default function configurInstance(route) {
route.post("/instance", function (schema, request) {
const newInstance = {
- ...this.normalizedRequestAttrs("instance"),
+ ...this.normalizedRequestAttrs("instance-new"),
workspaceId: WORKSPACE_ID,
};
- return schema.instances.create(newInstance);
+ const ts = Date.now();
+ const createdInstance = schema.instances.create({
+ environmentId: newInstance.environmentId,
+ creatorId: newInstance.creatorId,
+ updaterId: newInstance.creatorId,
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ name: newInstance.name,
+ externalLink: newInstance.externalLink,
+ host: newInstance.host,
+ port: newInstance.port,
+ });
+
+ const messageList = [];
+ const messageTemplate = {
+ containerId: createdInstance.id,
+ createdTs: ts,
+ lastUpdatedTs: ts,
+ type: "bb.msg.instance.create",
+ status: "DELIVERED",
+ creatorId: newInstance.creatorId,
+ workspaceId: WORKSPACE_ID,
+ payload: {
+ instanceName: createdInstance.name,
+ },
+ };
+
+ const allOwnerAndDBAs = schema.roleMappings.where((roleMapping) => {
+ return (
+ roleMapping.workspaceId == WORKSPACE_ID &&
+ (roleMapping.role == "OWNER" || roleMapping.role == "DBA")
+ );
+ }).models;
+
+ allOwnerAndDBAs.forEach((roleMapping) => {
+ messageList.push({
+ ...messageTemplate,
+ receiverId: roleMapping.principalId,
+ });
+ });
+
+ for (const message of messageList) {
+ // We only send out message if it's NOT destined to self.
+ if (newInstance.creatorId != message.receiverId) {
+ schema.messages.create(message);
+ }
+ }
+
+ return createdInstance;
});
route.patch("/instance/:instanceId", function (schema, request) {
diff --git a/frontend/src/miragejs/seeds/index.ts b/frontend/src/miragejs/seeds/index.ts
index 315f788ec5..a724d21583 100644
--- a/frontend/src/miragejs/seeds/index.ts
+++ b/frontend/src/miragejs/seeds/index.ts
@@ -13,6 +13,17 @@ const workspacesSeeder = (server: any) => {
// resources from different workspaces.
const workspace2 = server.schema.workspaces.find(101);
+ // User
+ const ws1Owner = server.schema.users.find(1);
+ const ws1DBA = server.schema.users.find(2);
+ const ws1Dev1 = server.schema.users.find(3);
+ const ws1Dev2 = server.schema.users.find(5);
+
+ const ws1UserList = [ws1Owner, ws1DBA, ws1Dev1, ws1Dev2];
+
+ const ws2DBA = server.schema.users.find(4);
+ const ws2Dev = server.schema.users.find(1);
+
// Environment
const environmentList1 = [];
for (let i = 0; i < 4; i++) {
@@ -55,6 +66,8 @@ const workspacesSeeder = (server: any) => {
(i + 1),
// Create an extra instance for prod.
environmentId: i == 4 ? environmentList1[3].id : environmentList1[i].id,
+ creatorId: ws1DBA.id,
+ updaterId: ws1DBA.id,
})
);
}
@@ -66,6 +79,8 @@ const workspacesSeeder = (server: any) => {
instanceNamelist[Math.floor(Math.random() * instanceNamelist.length)] +
(i + 1),
environmentId: environmentList2[i].id,
+ creatorId: ws2DBA.id,
+ updaterId: ws2DBA.id,
});
}
@@ -97,16 +112,6 @@ const workspacesSeeder = (server: any) => {
);
// Task
- const ws1Owner = server.schema.users.find(1);
- const ws1DBA = server.schema.users.find(2);
- const ws1Dev1 = server.schema.users.find(3);
- const ws1Dev2 = server.schema.users.find(5);
-
- const ws1UserList = [ws1Owner, ws1DBA, ws1Dev1, ws1Dev2];
-
- const ws2DBA = server.schema.users.find(4);
- const ws2Dev = server.schema.users.find(1);
-
let task = server.create("task", {
type: "bytebase.general",
name: "Hello, World!",
diff --git a/frontend/src/store/modules/instance.ts b/frontend/src/store/modules/instance.ts
index 7d2da42302..45fa643869 100644
--- a/frontend/src/store/modules/instance.ts
+++ b/frontend/src/store/modules/instance.ts
@@ -20,9 +20,21 @@ function convert(instance: ResourceObject, rootGetters: any): Instance {
);
}
);
+ const creator = rootGetters["principal/principalById"](
+ instance.attributes.creatorId
+ );
+ const updater = rootGetters["principal/principalById"](
+ instance.attributes.updaterId
+ );
+
return {
id: instance.id,
- ...(instance.attributes as Omit<Instance, "id" | "environment">),
+ creator,
+ updater,
+ ...(instance.attributes as Omit<
+ Instance,
+ "id" | "environment" | "creator" | "updater"
+ >),
environment,
};
}
@@ -103,7 +115,7 @@ const actions = {
(
await axios.post(`/api/instance`, {
data: {
- type: "instance",
+ type: "instancenew",
attributes: newInstance,
},
})
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index 46270f2f60..51ad0956c9 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -71,6 +71,8 @@ export const unknown = (
const UNKNOWN_INSTANCE: Instance = {
id: UNKNOWN_ID,
environment: UNKNOWN_ENVIRONMENT,
+ creator: UNKNOWN_PRINCIPAL,
+ updater: UNKNOWN_PRINCIPAL,
createdTs: 0,
lastUpdatedTs: 0,
name: "<<Unknown instance>>",
@@ -515,7 +517,11 @@ export type TaskMessageType =
| "bb.msg.task.updatestatus"
| "bb.msg.task.comment";
-export type MessageType = TaskMessageType;
+export type InstanceMessageType =
+ | "bb.msg.instance.create"
+ | "bb.msg.instance.delete";
+
+export type MessageType = TaskMessageType | InstanceMessageType;
export type TaskAssignMessagePayload = {
taskName: string;
@@ -529,14 +535,19 @@ export type TaskUpdateStatusMessagePayload = {
newStatus: TaskStatus;
};
-export type TaskCommentPayload = {
+export type TaskCommentMessagePayload = {
taskName: string;
};
+export type InstanceMessagePaylaod = {
+ instanceName: string;
+};
+
export type MessagePayload =
| TaskAssignMessagePayload
| TaskUpdateStatusMessagePayload
- | TaskCommentPayload;
+ | TaskCommentMessagePayload
+ | InstanceMessagePaylaod;
export type MessageStatus = "DELIVERED" | "CONSUMED";
@@ -571,6 +582,8 @@ export type EnvironmentNew = Omit<Environment, "id">;
export type Instance = {
id: InstanceId;
environment: Environment;
+ creator: Principal;
+ updater: Principal;
createdTs: number;
lastUpdatedTs: number;
name: string;
@@ -580,6 +593,7 @@ export type Instance = {
};
export type InstanceNew = {
+ creatorId: PrincipalId;
environmentId: EnvironmentId;
name: string;
externalLink?: string;
diff --git a/frontend/src/views/Inbox.vue b/frontend/src/views/Inbox.vue
index 343d8d87de..fb7a6052d8 100644
--- a/frontend/src/views/Inbox.vue
+++ b/frontend/src/views/Inbox.vue
@@ -124,6 +124,15 @@
{{ message.payload.taskName }}
</router-link>
</template>
+ <template v-else-if="message.type == 'bb.msg.instance.create'">
+ <span class="ml-1"> created instance</span>
+ <router-link
+ :to="`/instance/${message.containerId}`"
+ class="normal-link ml-1"
+ >
+ {{ message.payload.instanceName }}
+ </router-link>
+ </template>
<span
v-if="message.status == 'DELIVERED'"
class="ml-2 mt-1 h-3 w-3 rounded-full bg-accent"