Skip to content

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

Instance Detail Route

Tianzhou Chenabaff249871b
diff --git a/frontend/src/components/QuickActionPanel.vue b/frontend/src/components/QuickActionPanel.vue
index bced63cddf..376eff79f4 100644
--- a/frontend/src/components/QuickActionPanel.vue
+++ b/frontend/src/components/QuickActionPanel.vue
@@ -1,7 +1,7 @@
<template>
<div class="overflow-hidden grid grid-cols-3 gap-2 sm:inline-flex">
<div class="flex flex-col items-center w-28">
- <button class="btn-icon">
+ <router-link to="/instance/new" class="btn-icon">
<span
class="inline-flex items-center p-3 bg-blue-600 hover:bg-blue-700 rounded-full"
>
@@ -20,7 +20,7 @@
></path>
</svg>
</span>
- </button>
+ </router-link>
<h3 class="mt-1 text-base font-normal text-accent tracking-tight">
Add Instance
</h3>
diff --git a/frontend/src/miragejs/factories/index.js b/frontend/src/miragejs/factories/index.js
index debf05b8b5..5b6d65f1a6 100644
--- a/frontend/src/miragejs/factories/index.js
+++ b/frontend/src/miragejs/factories/index.js
@@ -7,6 +7,7 @@ import user from "./user";
import bookmark from "./bookmark";
import project from "./project";
import environment from "./environment";
+import instance from "./instance";
import repository from "./repository";
import job from "./job";
import step from "./step";
@@ -29,6 +30,7 @@ export default {
...bookmark,
...project,
...environment,
+ ...instance,
...repository,
...job,
...step,
diff --git a/frontend/src/miragejs/factories/instance.js b/frontend/src/miragejs/factories/instance.js
new file mode 100644
index 0000000000..fe5fa64048
--- /dev/null
+++ b/frontend/src/miragejs/factories/instance.js
@@ -0,0 +1,48 @@
+import { Factory } from "miragejs";
+
+export default {
+ instance: Factory.extend({
+ name(i) {
+ if (i == 0) {
+ return "dev env";
+ } else if (i == 1) {
+ return "test env";
+ } else if (i == 2) {
+ return "staging env";
+ } else {
+ return "prod env " + i;
+ }
+ },
+ 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";
+ }
+ },
+ username() {
+ return "root";
+ },
+ password() {
+ return "root";
+ },
+ database() {
+ return "";
+ },
+ }),
+};
diff --git a/frontend/src/miragejs/factories/workspace.js b/frontend/src/miragejs/factories/workspace.js
index d4f67c3e82..b80739d923 100644
--- a/frontend/src/miragejs/factories/workspace.js
+++ b/frontend/src/miragejs/factories/workspace.js
@@ -10,6 +10,13 @@ export default {
return "ws" + (i + 1);
},
afterCreate(workspace, server) {
+ for (let i = 0; i < 4; i++) {
+ server.create("instance", {
+ workspace,
+ name: workspace.name + " instance" + i + faker.fake("{{lorem.word}}"),
+ });
+ }
+
server.create("activity", {
workspace,
name: workspace.name + " activity1 " + faker.fake("{{lorem.word}}"),
diff --git a/frontend/src/miragejs/models/index.js b/frontend/src/miragejs/models/index.js
index 1863f482de..e33b294c02 100644
--- a/frontend/src/miragejs/models/index.js
+++ b/frontend/src/miragejs/models/index.js
@@ -22,6 +22,7 @@ export default {
workspaceRole: hasMany(),
pipeline: hasMany(),
environment: hasMany(),
+ instance: hasMany(),
}),
workspaceRole: Model.extend({
@@ -56,6 +57,10 @@ export default {
workspace: belongsTo(),
}),
+ instance: Model.extend({
+ workspace: belongsTo(),
+ }),
+
project: Model.extend({
workspace: belongsTo(),
group: belongsTo(),
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index cc81c20922..017ed74099 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -172,6 +172,39 @@ export default function routes() {
return schema.environments.find(request.params.environmentId).destroy();
});
+ this.get("/environment", function (schema, request) {
+ return schema.environments
+ .where((environment) => {
+ return environment.workspaceId == WORKSPACE_ID;
+ })
+ .sort((a, b) => a.order - b.order);
+ });
+
+ this.post("/instance", function (schema, request) {
+ let order = 0;
+ const list = schema.instances.where((instance) => {
+ return instance.workspaceId == WORKSPACE_ID;
+ });
+ if (list.length > 0) {
+ order = list.sort((a, b) => b.order - a.order).models[0].order + 1;
+ }
+ const newInstance = {
+ ...this.normalizedRequestAttrs("instance"),
+ workspaceId: WORKSPACE_ID,
+ order,
+ };
+ return schema.instances.create(newInstance);
+ });
+
+ this.patch("/instance/:instanceId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("instance");
+ return schema.instances.find(request.params.instanceId).update(attrs);
+ });
+
+ this.delete("/instance/:instanceId", function (schema, request) {
+ return schema.instances.find(request.params.instanceId).destroy();
+ });
+
this.get("/bookmark", function (schema, request) {
return schema.bookmarks.where({
workspaceId: WORKSPACE_ID,
diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts
index 437c99df54..bcff2d04ef 100644
--- a/frontend/src/router/index.ts
+++ b/frontend/src/router/index.ts
@@ -206,6 +206,18 @@ const routes: Array<RouteRecordRaw> = [
},
props: { content: true, leftSidebar: true },
},
+ {
+ path: "instance/:instanceId",
+ name: "workspace.instance.detail",
+ meta: { displayName: "Instance" },
+ components: {
+ content: defineAsyncComponent(
+ () => import("../views/InstanceDetail.vue")
+ ),
+ leftSidebar: MainSidebar,
+ },
+ props: { content: true },
+ },
{
path: "pipeline/:pipelineId",
name: "workspace.pipeline.detail",
@@ -269,6 +281,7 @@ router.beforeEach((to, from, next) => {
to.name === "workspace.home" ||
to.name === "workspace.inbox" ||
to.name === "workspace.environment" ||
+ to.name === "workspace.instance" ||
to.name?.toString().startsWith("setting")
) {
next();
diff --git a/frontend/src/store/modules/instance.ts b/frontend/src/store/modules/instance.ts
new file mode 100644
index 0000000000..d954977bc4
--- /dev/null
+++ b/frontend/src/store/modules/instance.ts
@@ -0,0 +1,130 @@
+import axios from "axios";
+import { InstanceId, Instance, InstanceState } from "../../types";
+
+const state: () => InstanceState = () => ({
+ instanceList: [],
+});
+
+const getters = {
+ instanceList: (state: InstanceState) => () => {
+ return state.instanceList;
+ },
+};
+
+const actions = {
+ async fetchInstanceList({ commit }: any) {
+ const instanceList = (await axios.get(`/api/instance`)).data.data;
+
+ commit("setInstanceList", {
+ instanceList,
+ });
+
+ return instanceList;
+ },
+
+ async createInstance(
+ { commit }: any,
+ { newInstance }: { newInstance: Instance }
+ ) {
+ const createdInstance = (
+ await axios.post(`/api/instance`, {
+ data: newInstance,
+ })
+ ).data.data;
+
+ commit("appendInstance", {
+ newInstance: createdInstance,
+ });
+
+ return createdInstance;
+ },
+
+ async patchInstanceById(
+ { commit }: any,
+ {
+ instanceId,
+ instance,
+ }: {
+ instanceId: InstanceId;
+ instance: Instance;
+ }
+ ) {
+ const updatedInstance = (
+ await axios.patch(`/api/instance/${instanceId}`, {
+ data: instance,
+ })
+ ).data.data;
+
+ commit("replaceInstanceInList", {
+ updatedInstance,
+ });
+
+ return updatedInstance;
+ },
+
+ async deleteInstanceById(
+ { state, commit }: { state: InstanceState; commit: any },
+ {
+ id,
+ }: {
+ id: InstanceId;
+ }
+ ) {
+ await axios.delete(`/api/instance/${id}`);
+
+ const newList = state.instanceList.filter((item: Instance) => {
+ return item.id != id;
+ });
+
+ commit("setInstanceList", {
+ instanceList: newList,
+ });
+ },
+};
+
+const mutations = {
+ setInstanceList(
+ state: InstanceState,
+ {
+ instanceList,
+ }: {
+ instanceList: Instance[];
+ }
+ ) {
+ state.instanceList = instanceList;
+ },
+ appendInstance(
+ state: InstanceState,
+ {
+ newInstance,
+ }: {
+ newInstance: Instance;
+ }
+ ) {
+ state.instanceList.push(newInstance);
+ },
+
+ replaceInstanceInList(
+ state: InstanceState,
+ {
+ updatedInstance,
+ }: {
+ updatedInstance: Instance;
+ }
+ ) {
+ const i = state.instanceList.findIndex(
+ (item: Instance) => item.id == updatedInstance.id
+ );
+ if (i != -1) {
+ state.instanceList[i] = updatedInstance;
+ }
+ },
+};
+
+export default {
+ namespaced: true,
+ state,
+ getters,
+ actions,
+ mutations,
+};
diff --git a/frontend/src/store/modules/router.ts b/frontend/src/store/modules/router.ts
index 9295efef87..8dc9073530 100644
--- a/frontend/src/store/modules/router.ts
+++ b/frontend/src/store/modules/router.ts
@@ -20,6 +20,17 @@ const getters = {
pipelineId: pipelineComponents[1],
};
}
+
+ // /instance/<<instance-id>>
+ // Total 2 elements, 2nd element is the pipeline id
+ const instanceComponents = currentRoute.path.match(
+ "/instance/([0-9a-zA-Z_]+)"
+ ) || ["/", undefined];
+ if (instanceComponents[1]) {
+ return {
+ instanceId: instanceComponents[1],
+ };
+ }
return {};
},
};
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index b3c503098d..24f8dadf04 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -12,6 +12,8 @@ export type ProjectId = string;
export type EnvironmentId = string;
+export type InstanceId = string;
+
// Models
export type User = ResourceObject & {};
export type NewUser = Omit<ResourceObject, "id">;
@@ -68,6 +70,18 @@ export type Environment = ResourceObject & {
};
export type NewEnvironment = Omit<ResourceObject, "id">;
+export type Instance = ResourceObject & {
+ attributes: {
+ name: string;
+ host: string;
+ port?: string;
+ username?: string;
+ password?: string;
+ database?: string;
+ };
+};
+export type NewInstance = Omit<ResourceObject, "id">;
+
export type Group = ResourceObject & {};
export type NewGroup = Omit<ResourceObject, "id">;
@@ -128,6 +142,10 @@ export interface EnvironmentState {
environmentList: Environment[];
}
+export interface InstanceState {
+ instanceList: Instance[];
+}
+
export interface RepositoryState {
repositoryByProject: Map<ProjectId, Repository>;
}
@@ -135,4 +153,5 @@ export interface RepositoryState {
// UI
export interface RouterSlug {
pipelineId?: PipelineId;
+ instanceId?: InstanceId;
}
diff --git a/frontend/src/views/InstanceDetail.vue b/frontend/src/views/InstanceDetail.vue
new file mode 100644
index 0000000000..2c494a1fd6
--- /dev/null
+++ b/frontend/src/views/InstanceDetail.vue
@@ -0,0 +1,290 @@
+<style scoped>
+/* Removed the ticker in the number field */
+input::-webkit-outer-spin-button,
+input::-webkit-inner-spin-button {
+ -webkit-appearance: none;
+ margin: 0;
+}
+
+/* Firefox */
+input[type="number"] {
+ -moz-appearance: textfield;
+}
+</style>
+
+<template>
+ <form
+ class="px-4 space-y-6 divide-y divide-gray-200"
+ @submit.prevent="doUpdate(state.instance)"
+ >
+ <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
+ <div class="sm:col-span-2">
+ <label for="name" class="text-lg leading-6 font-medium text-gray-900">
+ Instance Name <span style="color: red">*</span>
+ </label>
+ <input
+ required
+ id="name"
+ name="name"
+ type="text"
+ class="shadow-sm mt-4 focus:ring-accent block w-full sm:text-sm border-control-border rounded-md"
+ :value="state.instance.attributes.name"
+ @input="updateInstance('name', $event.target.value)"
+ />
+ </div>
+ </div>
+ <div class="pt-6">
+ <h3 class="text-lg leading-6 font-medium text-gray-900">
+ Connection Info
+ </h3>
+ <p class="mt-1 text-sm text-gray-500">
+ Provide the info to connect to the database.
+ </p>
+ <button type="button" class="btn-normal mt-4">Test Connection</button>
+ </div>
+ <div class="pt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
+ <div class="sm:col-span-3">
+ <label for="host" class="block text-sm font-medium text-gray-700">
+ Host or Socket <span style="color: red">*</span>
+ </label>
+ <div class="mt-1">
+ <input
+ required
+ type="text"
+ id="host"
+ name="host"
+ placeholder="e.g. 127.0.0.1 | localhost | /tmp/mysql.sock"
+ class="shadow-sm focus:ring-accent block w-full sm:text-sm border-control-border rounded-md"
+ :value="state.instance.attributes.host"
+ @input="updateInstance('host', $event.target.value)"
+ />
+ </div>
+ </div>
+
+ <div class="sm:col-span-3">
+ <label for="port" class="block text-sm font-medium text-gray-700">
+ Port (Only applicable when connecting via Host)
+ </label>
+ <div class="mt-1">
+ <input
+ type="number"
+ id="port"
+ name="port"
+ placeholder="3306"
+ class="shadow-sm focus:ring-accent block w-full sm:text-sm border-control-border rounded-md"
+ :value="state.instance.attributes.port"
+ @input="updateInstance('port', $event.target.value)"
+ />
+ </div>
+ </div>
+
+ <div class="sm:col-span-2">
+ <label for="username" class="block text-sm font-medium text-gray-700">
+ Username <span style="color: red">*</span>
+ </label>
+ <div class="mt-1">
+ <input
+ required
+ id="username"
+ name="username"
+ type="text"
+ class="shadow-sm focus:ring-accent block w-full sm:text-sm border-control-border rounded-md"
+ :value="state.instance.attributes.username"
+ @input="updateInstance('username', $event.target.value)"
+ />
+ </div>
+ </div>
+
+ <div class="sm:col-span-2 sm:col-start-1">
+ <label for="password" class="block text-sm font-medium text-gray-700">
+ Password
+ </label>
+ <div class="mt-1">
+ <input
+ id="password"
+ name="password"
+ type="password"
+ class="shadow-sm focus:ring-accent block w-full sm:text-sm border-control-border rounded-md"
+ :value="state.instance.attributes.password"
+ @input="updateInstance('password', $event.target.value)"
+ />
+ </div>
+ </div>
+
+ <div class="sm:col-span-2 sm:col-start-1">
+ <label for="database" class="block text-sm font-medium text-gray-700">
+ Database
+ </label>
+ <div class="mt-1">
+ <input
+ id="database"
+ name="database"
+ type="text"
+ class="shadow-sm focus:ring-accent block w-full sm:text-sm border-control-border rounded-md"
+ :value="state.instance.attributes.database"
+ @input="updateInstance('database', $event.target.value)"
+ />
+ </div>
+ </div>
+ </div>
+ <!-- Create button group -->
+ <div v-if="state.new" class="flex justify-end pt-5">
+ <button
+ type="button"
+ class="btn-normal py-2 px-4"
+ @click.prevent="goBack"
+ >
+ Cancel
+ </button>
+ <button
+ type="submit"
+ class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
+ >
+ Create
+ </button>
+ </div>
+ <!-- Update button group -->
+ <div v-else class="flex justify-between pt-5">
+ <button
+ type="button"
+ class="btn-danger py-2 px-4"
+ @click.prevent="state.showDeleteModal = true"
+ >
+ Delete
+ </button>
+ <div>
+ <button
+ type="button"
+ class="btn-normal py-2 px-4"
+ :disabled="!valueChanged"
+ @click.prevent="revertInstance"
+ >
+ Revert
+ </button>
+ <button
+ type="submit"
+ class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
+ :disabled="!valueChanged"
+ >
+ Update
+ </button>
+ </div>
+ </div>
+ </form>
+ <BBAlert
+ :showing="state.showDeleteModal"
+ :style="'critical'"
+ :okText="'Delete'"
+ :title="'Delete instance \'' + state.instance.attributes.name + '\' ?'"
+ @ok="
+ () => {
+ state.showDeleteModal = false;
+ doDelete();
+ }
+ "
+ @cancel="state.showDeleteModal = false"
+ >
+ </BBAlert>
+</template>
+
+<script lang="ts">
+import { computed, onMounted, reactive } from "vue";
+import { useStore } from "vuex";
+import { useRouter } from "vue-router";
+import isEqual from "lodash-es/isEqual";
+import InstanceForm from "../components/InstanceForm.vue";
+import { Instance, NewInstance } from "../types";
+
+interface LocalState {
+ new: boolean;
+ originalInstance?: Instance;
+ instance?: Instance | NewInstance;
+ showDeleteModal: boolean;
+}
+
+export default {
+ name: "InstanceDetail",
+ emits: ["delete"],
+ components: {
+ InstanceForm,
+ },
+ props: {
+ instanceId: {
+ required: true,
+ type: String,
+ },
+ },
+ setup(props, { emit }) {
+ const store = useStore();
+ const router = useRouter();
+
+ const state = reactive<LocalState>({
+ new: props.instanceId.toLowerCase() == "new",
+ showDeleteModal: false,
+ });
+
+ onMounted(() => {
+ document.addEventListener("keydown", (e) => {
+ if (e.code == "Escape") {
+ goBack();
+ }
+ });
+ });
+
+ const assignInstance = (instance: Instance) => {
+ state.originalInstance = instance;
+ // Make hard copy since we are going to make equal comparsion to determine the update button enable state.
+ state.instance = JSON.parse(JSON.stringify(state.originalInstance));
+ };
+
+ // [NOTE] Ternary operator doesn't trigger VS type checking, so we use a separate
+ // IF block.
+ if (state.new) {
+ state.instance = {
+ type: "instance",
+ attributes: {
+ name: "New Instance",
+ host: "127.0.0.1",
+ username: "root",
+ },
+ };
+ } else {
+ assignInstance(store.getters["instance/instanceById"]);
+ }
+
+ const valueChanged = computed(() => {
+ return state.new || !isEqual(state.originalInstance, state.instance);
+ });
+
+ const goBack = () => {
+ router.push(store.getters["router/backPath"]());
+ };
+
+ const doUpdate = (newInstance: Instance) => {
+ store
+ .dispatch("instance/patchInstanceById", {
+ instanceId: props.instanceId,
+ instance: newInstance,
+ })
+ .then((instance) => {
+ assignInstance(instance);
+ })
+ .catch((error) => {
+ console.log(error);
+ });
+ };
+
+ const doDelete = () => {
+ emit("delete", state.instance);
+ };
+
+ return {
+ state,
+ valueChanged,
+ goBack,
+ doUpdate,
+ doDelete,
+ };
+ },
+};
+</script>