Skip to content

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

refactor: move issue pipeline creation from client-side to server-side (#644)

Liu Ji6a1b90cdf7df
diff --git a/frontend/src/types/instance.ts b/frontend/src/types/instance.ts
index 27dde6befe..cb84a7730d 100644
--- a/frontend/src/types/instance.ts
+++ b/frontend/src/types/instance.ts
@@ -105,7 +105,7 @@ export type InstanceMigration = {
export type MigrationEngine = "UI" | "VCS";
-export type MigrationType = "BASELINE" | "MIGRATE" | "BRANCH";
+export type MigrationType = "BASELINE" | "MIGRATE" | "BRANCH" | "DATA";
export type MigrationStatus = "PENDING" | "DONE" | "FAILED";
diff --git a/frontend/src/types/pipeline/task.ts b/frontend/src/types/pipeline/task.ts
index c01aea26a4..f0a9101d0e 100644
--- a/frontend/src/types/pipeline/task.ts
+++ b/frontend/src/types/pipeline/task.ts
@@ -105,7 +105,7 @@ export type TaskCreate = {
instanceId: InstanceId;
databaseId?: DatabaseId;
statement: string;
- rollbackStatement: string;
+ rollbackStatement?: string;
databaseName?: string;
characterSet?: string;
collation?: string;
diff --git a/frontend/src/views/IssueDetail.vue b/frontend/src/views/IssueDetail.vue
index e864ef25dc..0a7048d327 100644
--- a/frontend/src/views/IssueDetail.vue
+++ b/frontend/src/views/IssueDetail.vue
@@ -25,9 +25,10 @@ import {
reactive,
ref,
defineComponent,
+ Ref,
} from "vue";
-import { useStore } from "vuex";
-import { useRoute, useRouter } from "vue-router";
+import { Store, useStore } from "vuex";
+import { useRoute, _RouteLocationBase } from "vue-router";
import { idFromSlug } from "../utils";
import { IssueDetailLayout } from "../components/Issue";
import {
@@ -36,22 +37,20 @@ import {
IssueCreate,
IssueType,
Database,
- Environment,
- TaskDatabaseSchemaUpdatePayload,
- TaskDatabaseDataUpdatePayload,
NORMAL_POLL_INTERVAL,
POLL_JITTER,
POST_CHANGE_POLL_INTERVAL,
Project,
- Policy,
unknown,
+ MigrationType,
+ TaskDatabaseSchemaUpdatePayload,
+ Principal,
} from "../types";
import {
defaulTemplate as defaultTemplate,
templateForType,
IssueTemplate,
} from "../plugins";
-import { isEmpty } from "lodash-es";
import { NSpin } from "naive-ui";
interface LocalState {
@@ -80,7 +79,6 @@ export default defineComponent({
setup(props) {
const store = useStore();
- const router = useRouter();
const route = useRoute();
const currentUser = computed(() => store.getters["auth/currentUser"]());
@@ -88,7 +86,7 @@ export default defineComponent({
let newIssueTemplate = ref<IssueTemplate>(defaultTemplate());
const refreshTemplate = () => {
- const issueType = router.currentRoute.value.query.template as IssueType;
+ const issueType = route.query.template as IssueType;
if (issueType) {
const template = templateForType(issueType);
if (template) {
@@ -111,7 +109,7 @@ export default defineComponent({
// Vue doesn't natively react to query parameter change
// so we need to manually watch here.
watch(
- () => router.currentRoute.value.query.template,
+ () => route.query.template,
() => {
refreshTemplate();
}
@@ -131,64 +129,6 @@ export default defineComponent({
: store.getters["issue/issueById"](idFromSlug(props.issueSlug));
});
- const buildNewTenantSchemaUpdateIssue = async (
- project: Project
- ): Promise<IssueCreate> => {
- const baseTemplate = newIssueTemplate.value.buildIssue({
- environmentList: [],
- approvalPolicyList: [],
- databaseList: [],
- currentUser: currentUser.value,
- });
- const issueCreate: IssueCreate = {
- projectId: project.id,
- name: (route.query.name as string) || baseTemplate.name,
- type: "bb.issue.database.schema.update",
- description: baseTemplate.description,
- assigneeId: currentUser.value.id,
- createContext: {
- migrationType: "MIGRATE",
- updateSchemaDetailList: [
- {
- databaseName: route.query.databaseName,
- statement: "/* YOUR_SQL_HERE */",
- rollbackStatement: "",
- },
- ],
- },
- payload: {},
- };
- const issue: Issue = await store.dispatch(
- "issue/validateIssue",
- issueCreate
- );
-
- issueCreate.assigneeId = baseTemplate.assigneeId;
- issueCreate.pipeline = {
- name: issue.pipeline.name,
- stageList: issue.pipeline.stageList.map((stage) => ({
- name: stage.name,
- environmentId: stage.environment.id,
- taskList: stage.taskList.map((task) => {
- const payload = task.payload as TaskDatabaseSchemaUpdatePayload;
- return {
- name: task.name,
- status: task.status,
- type: task.type,
- instanceId: task.instance.id,
- databaseId: task.database?.id,
- migrationType: payload.migrationType,
- statement: payload.statement,
- rollbackStatement: "",
- earliestAllowedTs: task.earliestAllowedTs,
- };
- }),
- })),
- };
-
- return issueCreate;
- };
-
const findProject = async (): Promise<Project> => {
const projectId = route.query.project
? parseInt(route.query.project as string)
@@ -201,6 +141,67 @@ export default defineComponent({
return project;
};
+ const buildNewTenantSchemaUpdateIssue = async (): Promise<IssueCreate> => {
+ const helper = new IssueCreateHelper({
+ template: newIssueTemplate,
+ currentUser,
+ store,
+ route,
+ });
+ await helper.prepare();
+ // tenant issue is generated by specifying databaseName
+ helper.issueCreate!.createContext = {
+ migrationType: "MIGRATE",
+ updateSchemaDetailList: [
+ {
+ databaseName: route.query.databaseName,
+ statement: "/* YOUR_SQL_HERE */",
+ },
+ ],
+ };
+ await helper.validate();
+ return helper.generate();
+ };
+
+ const buildNewStandardIssue = async (): Promise<IssueCreate> => {
+ const helper = new IssueCreateHelper({
+ template: newIssueTemplate,
+ currentUser,
+ store,
+ route,
+ });
+ await helper.prepare();
+
+ const databaseList: Database[] = [];
+ if (route.query.databaseList) {
+ const databaseIdList = (route.query.databaseList as string).split(",");
+ for (const databaseId of databaseIdList) {
+ databaseList.push(store.getters["database/databaseById"](databaseId));
+ }
+ }
+ const issueType = route.query.template as IssueType;
+ // standard single-stage or multi-stage issue is generated by specifying
+ // databaseId for each stage
+ const migrationType: MigrationType =
+ issueType === "bb.issue.database.data.update" ? "DATA" : "MIGRATE";
+ helper.issueCreate!.createContext = {
+ migrationType,
+ updateSchemaDetailList: databaseList.map((db) => {
+ return {
+ databaseId: db.id,
+ statement: "/* YOUR_SQL_HERE */",
+ };
+ }),
+ };
+
+ await helper.validate();
+
+ // clean up createContext for standard issues
+ helper.issueCreate!.createContext = {};
+
+ return helper.generate();
+ };
+
const maybeBuildTenantDeployIssue = async (): Promise<
IssueCreate | false
> => {
@@ -214,7 +215,7 @@ export default defineComponent({
project.tenantMode === "TENANT" &&
issueType === "bb.issue.database.schema.update"
) {
- return buildNewTenantSchemaUpdateIssue(project);
+ return buildNewTenantSchemaUpdateIssue();
}
return false;
};
@@ -225,73 +226,11 @@ export default defineComponent({
return tenant;
}
- var newIssue: IssueCreate;
-
// Create issue from normal query parameter
- const databaseList: Database[] = [];
- if (router.currentRoute.value.query.databaseList) {
- for (const databaseId of (
- router.currentRoute.value.query.databaseList as string
- ).split(","))
- databaseList.push(store.getters["database/databaseById"](databaseId));
- }
-
- const environmentList: Environment[] = [];
- const approvalPolicyList: Policy[] = [];
- if (router.currentRoute.value.query.environment) {
- environmentList.push(
- store.getters["environment/environmentById"](
- router.currentRoute.value.query.environment
- )
- );
- } else if (databaseList.length > 0) {
- for (const database of databaseList) {
- environmentList.push(database.instance.environment);
- }
- } else {
- environmentList.push(...store.getters["environment/environmentList"]());
- }
-
- for (const environment of environmentList) {
- approvalPolicyList.push(
- store.getters["policy/policyByEnvironmentIdAndType"](
- environment.id,
- "bb.policy.pipeline-approval"
- )
- );
- }
-
- newIssue = {
- ...newIssueTemplate.value.buildIssue({
- environmentList,
- approvalPolicyList,
- databaseList,
- currentUser: currentUser.value,
- }),
- projectId: router.currentRoute.value.query.project
- ? parseInt(router.currentRoute.value.query.project as string)
- : UNKNOWN_ID,
- };
-
- // For demo mode, we assign the issue to the current user, so it can also experience the assignee user flow.
- if (store.getters["actuator/isDemo"]()) {
- newIssue.assigneeId = currentUser.value.id;
- }
+ const newIssue = await buildNewStandardIssue();
- if (router.currentRoute.value.query.name) {
- newIssue.name = router.currentRoute.value.query.name as string;
- }
- if (router.currentRoute.value.query.description) {
- newIssue.description = router.currentRoute.value.query
- .description as string;
- }
- if (router.currentRoute.value.query.assignee) {
- newIssue.assigneeId = parseInt(
- router.currentRoute.value.query.assignee as string
- );
- }
for (const field of newIssueTemplate.value.inputFieldList) {
- const value = router.currentRoute.value.query[field.slug] as string;
+ const value = route.query[field.slug] as string;
if (value) {
if (field.type == "Boolean") {
newIssue.payload[field.id] =
@@ -381,10 +320,118 @@ export default defineComponent({
};
return {
+ route,
state,
issue,
onStatusChanged,
};
},
});
+
+type IssueCreateHelperContext = {
+ template: Ref<IssueTemplate>;
+ currentUser: Ref<Principal>;
+ store: Store<any>;
+ route: _RouteLocationBase;
+};
+
+class IssueCreateHelper {
+ context: IssueCreateHelperContext;
+ issueCreate: IssueCreate | null;
+ issue: Issue | null;
+
+ constructor(context: IssueCreateHelperContext) {
+ this.context = context;
+ this.issueCreate = null;
+ this.issue = null;
+ }
+
+ async prepare(): Promise<IssueCreate> {
+ const { template, currentUser, store, route } = this.context;
+ const baseTemplate = template.value.buildIssue({
+ environmentList: [],
+ approvalPolicyList: [],
+ databaseList: [],
+ currentUser: currentUser.value,
+ });
+
+ const issueCreate: IssueCreate = {
+ projectId: parseInt(route.query.project as string) || UNKNOWN_ID,
+ name: (route.query.name as string) || baseTemplate.name,
+ type: "bb.issue.database.schema.update",
+ description: baseTemplate.description,
+ // validateOnly does not support assigneeId=-1
+ // so we need to specify here
+ // but will reset this to -1 if route.query.assignee is empty
+ assigneeId: currentUser.value.id,
+ createContext: {},
+ payload: {},
+ };
+
+ // For demo mode, we assign the issue to the current user, so it can also experience the assignee user flow.
+ if (store.getters["actuator/isDemo"]()) {
+ issueCreate.assigneeId = currentUser.value.id;
+ }
+ if (route.query.name) {
+ issueCreate.name = route.query.name as string;
+ }
+ if (route.query.description) {
+ issueCreate.description = route.query.description as string;
+ }
+ if (route.query.assignee) {
+ issueCreate.assigneeId = parseInt(route.query.assignee as string);
+ }
+
+ this.issueCreate = issueCreate;
+
+ return issueCreate;
+ }
+
+ async validate(): Promise<[IssueCreate, Issue]> {
+ const { store } = this.context;
+ const issue: Issue = await store.dispatch(
+ "issue/validateIssue",
+ this.issueCreate
+ );
+
+ this.issue = issue;
+
+ return [this.issueCreate!, this.issue];
+ }
+
+ async generate(): Promise<IssueCreate> {
+ const { route } = this.context;
+ const issueCreate = this.issueCreate!;
+ const issue = this.issue!;
+ if (!route.query.assignee) {
+ issueCreate.assigneeId =
+ parseInt(route.query.assignee as string) || UNKNOWN_ID;
+ }
+
+ // copy the generated pipeline to issueCreate
+ // issueCreate is an editable object for the whole issue UI
+ issueCreate.pipeline = {
+ name: issue.pipeline.name,
+ stageList: issue.pipeline.stageList.map((stage) => ({
+ name: stage.name,
+ environmentId: stage.environment.id,
+ taskList: stage.taskList.map((task) => {
+ const payload = task.payload as TaskDatabaseSchemaUpdatePayload;
+ return {
+ name: task.name,
+ status: task.status,
+ type: task.type,
+ instanceId: task.instance.id,
+ databaseId: task.database?.id,
+ migrationType: payload.migrationType,
+ statement: payload.statement || "",
+ earliestAllowedTs: task.earliestAllowedTs,
+ };
+ }),
+ })),
+ };
+
+ return issueCreate;
+ }
+}
</script>