Skip to content

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

For VCS workflow, add a button in database migration history table to establish the baseline based on current schema

天舟a26e3ecdf40f
diff --git a/api/task.go b/api/task.go
index ebcaa388b3..e90980b15a 100644
--- a/api/task.go
+++ b/api/task.go
@@ -141,7 +141,7 @@ type TaskCreate struct {
Collation string `jsonapi:"attr,collation"`
BackupId *int `jsonapi:"attr,backupId"`
VCSPushEvent *common.VCSPushEvent
- MigrationType db.MigrationType
+ MigrationType db.MigrationType `jsonapi:"attr,migrationType"`
}
type TaskFind struct {
diff --git a/frontend/src/components/DatabaseMigrationHistoryPanel.vue b/frontend/src/components/DatabaseMigrationHistoryPanel.vue
index 40ed5096c8..214251623f 100644
--- a/frontend/src/components/DatabaseMigrationHistoryPanel.vue
+++ b/frontend/src/components/DatabaseMigrationHistoryPanel.vue
@@ -1,6 +1,16 @@
<template>
<div class="flex flex-col space-y-4">
- <div class="text-lg leading-6 font-medium text-main">Migration History</div>
+ <div class="text-lg leading-6 font-medium text-main">
+ Migration History
+ <button
+ v-if="allowEdit && database.project.workflowType == 'VCS'"
+ type="button"
+ class="ml-4 btn-primary"
+ @click.prevent="state.showBaselineModal = true"
+ >
+ Establish new baseline
+ </button>
+ </div>
<MigrationHistoryTable
v-if="state.migrationSetupStatus == 'OK'"
:databaseSectionList="[database]"
@@ -14,6 +24,22 @@
@click-action="configInstance"
/>
</div>
+
+ <BBAlert
+ v-if="state.showBaselineModal"
+ :style="'INFO'"
+ :okText="'Establish baseline'"
+ :cancelText="'Cancel'"
+ :title="`Establish new baseline for \'${database.name}\'`"
+ :description="'Bytebase will use the current schema as the new baseline. You should check that the current schema does reflect the desired state.\n\nFor VCS workflow, there must exist a baseline before any incremental migration change can be applied to the database.'"
+ @ok="
+ () => {
+ doCreateBaseline();
+ }
+ "
+ @cancel="state.showBaselineModal = false"
+ >
+ </BBAlert>
</template>
<script lang="ts">
@@ -28,10 +54,12 @@ import {
} from "../types";
import { useRouter } from "vue-router";
import { BBTableSectionDataSource } from "../bbkit/types";
-import { instanceSlug, isDBAOrOwner } from "../utils";
+import { instanceSlug, isDBAOrOwner, issueSlug } from "../utils";
+import { templateForType } from "../plugins";
interface LocalState {
migrationSetupStatus: MigrationSchemaStatus;
+ showBaselineModal: boolean;
}
export default {
@@ -42,6 +70,10 @@ export default {
required: true,
type: Object as PropType<Database>,
},
+ allowEdit: {
+ required: true,
+ type: Boolean,
+ },
},
setup(props, ctx) {
const store = useStore();
@@ -49,6 +81,7 @@ export default {
const state = reactive<LocalState>({
migrationSetupStatus: "OK",
+ showBaselineModal: false,
});
const currentUser = computed(() => store.getters["auth/currentUser"]());
@@ -103,12 +136,29 @@ export default {
router.push(`/instance/${instanceSlug(props.database.instance)}`);
};
+ const doCreateBaseline = () => {
+ state.showBaselineModal = false;
+ router.push({
+ name: "workspace.issue.detail",
+ params: {
+ issueSlug: "new",
+ },
+ query: {
+ template: "bb.issue.database.schema.baseline",
+ name: `Establish ${props.database.name} baseline`,
+ project: props.database.project.id,
+ databaseList: `${props.database.id}`,
+ },
+ });
+ };
+
return {
state,
allowConfigInstance,
attentionTitle,
migrationHistorySectionList,
configInstance,
+ doCreateBaseline,
};
},
};
diff --git a/frontend/src/plugins/issue/DatabaseSchemaBaselineTemplate.ts b/frontend/src/plugins/issue/DatabaseSchemaBaselineTemplate.ts
new file mode 100644
index 0000000000..09e3bb0a9b
--- /dev/null
+++ b/frontend/src/plugins/issue/DatabaseSchemaBaselineTemplate.ts
@@ -0,0 +1,50 @@
+import { IssueCreate, StageCreate, UNKNOWN_ID } from "../../types";
+import { IssueTemplate, TemplateContext } from "../types";
+
+const template: IssueTemplate = {
+ type: "bb.issue.database.schema.baseline",
+ buildIssue: (
+ ctx: TemplateContext
+ ): Omit<IssueCreate, "projectId" | "creatorId"> => {
+ const payload: any = {};
+ const stageList: StageCreate[] = [];
+ for (let i = 0; i < ctx.databaseList.length; i++) {
+ stageList.push({
+ name: `[${ctx.environmentList[i].name}] ${ctx.databaseList[i].name}`,
+ environmentId: ctx.environmentList[i].id,
+ taskList: [
+ {
+ name: `Establish ${ctx.databaseList[i].name} baseline`,
+ type: "bb.task.database.schema.update",
+ instanceId: ctx.databaseList[i].instance.id,
+ databaseId: ctx.databaseList[i].id,
+ statement: "/* Establish baseline using current schema */",
+ rollbackStatement: "",
+ migrationType: "BASELINE",
+ },
+ ],
+ });
+ }
+ return {
+ name:
+ ctx.databaseList.length == 1
+ ? `Establish ${ctx.databaseList[0].name} baseline`
+ : "Establish database baseline",
+ type: "bb.issue.database.schema.update",
+ description: "",
+ assigneeId: UNKNOWN_ID,
+ pipeline: {
+ stageList,
+ name:
+ ctx.databaseList.length == 1
+ ? `Establish ${ctx.databaseList[0].name} baseline pipeline`
+ : "Establish database baseline pipeline",
+ },
+ payload,
+ };
+ },
+ inputFieldList: [],
+ outputFieldList: [],
+};
+
+export default template;
diff --git a/frontend/src/plugins/issue/template.ts b/frontend/src/plugins/issue/template.ts
index 658695b50c..f47cf179e1 100644
--- a/frontend/src/plugins/issue/template.ts
+++ b/frontend/src/plugins/issue/template.ts
@@ -1,22 +1,26 @@
-import { FieldId, IssueTemplate, FieldInfo } from "../types";
import { IssueType } from "../../types";
-import DefaultTemplate from "./DefaultTemplate";
+import { FieldId, FieldInfo, IssueTemplate } from "../types";
import DatabaseCreateTemplate from "./DatabaseCreateTemplate";
import DatabaseGrantTemplate from "./DatabaseGrantTemplate";
+import DatabaseSchemaBaselineTemplate from "./DatabaseSchemaBaselineTemplate";
import DatabaseSchemaUpdateTemplate from "./DatabaseSchemaUpdateTemplate";
+import DefaultTemplate from "./DefaultTemplate";
+
+type TemplateType = IssueType | "bb.issue.database.schema.baseline";
const allIssueTemplateList: IssueTemplate[] = [
DefaultTemplate,
DatabaseCreateTemplate,
DatabaseGrantTemplate,
DatabaseSchemaUpdateTemplate,
+ DatabaseSchemaBaselineTemplate,
];
export function defaulTemplate(): IssueTemplate {
return DefaultTemplate;
}
-export function templateForType(type: IssueType): IssueTemplate | undefined {
+export function templateForType(type: TemplateType): IssueTemplate | undefined {
return allIssueTemplateList.find((template) => template.type === type);
}
diff --git a/frontend/src/views/DatabaseDetail.vue b/frontend/src/views/DatabaseDetail.vue
index 1533583fec..1fc0c2aad1 100644
--- a/frontend/src/views/DatabaseDetail.vue
+++ b/frontend/src/views/DatabaseDetail.vue
@@ -231,7 +231,10 @@
<DatabaseOverviewPanel :database="database" />
</template>
<template v-if="state.selectedIndex == MIGRAITON_HISTORY_TAB">
- <DatabaseMigrationHistoryPanel :database="database" />
+ <DatabaseMigrationHistoryPanel
+ :database="database"
+ :allowEdit="allowEdit"
+ />
</template>
<template v-if="state.selectedIndex == BACKUP_TAB">
<DatabaseBackupPanel