Skip to content

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

Make "Restore backup to a new database" create a new issue

天舟5cb12b1df6f9
diff --git a/frontend/src/components/BackupTable.vue b/frontend/src/components/BackupTable.vue
index f314d7b40b..67606ed982 100644
--- a/frontend/src/components/BackupTable.vue
+++ b/frontend/src/components/BackupTable.vue
@@ -90,26 +90,65 @@
{{ backup.creator.name }}
</BBTableCell>
<BBTableCell>
- <BBButtonConfirm
- v-if="backup.status == 'DONE'"
- :style="'CLONE'"
- :requireConfirm="true"
- :okText="'Clone'"
- :confirmTitle="`Are you sure to clone a new database '${database.name}' based on backup '${backup.name}'?`"
- :confirmDescription="''"
- @confirm="restoreBackup(backup)"
- />
+ <button
+ class="btn-icon"
+ @click.stop="
+ () => {
+ state.restoredBackup = backup;
+ state.showRestoreBackupModal = true;
+ }
+ "
+ >
+ <svg
+ class="w-4 h-4"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"
+ ></path>
+ </svg>
+ </button>
</BBTableCell>
</template>
</BBTable>
+ <BBModal
+ v-if="state.showRestoreBackupModal"
+ :title="`Restore backup '${state.restoredBackup.name}' to a new database`"
+ @close="
+ () => {
+ state.showRestoreBackupModal = false;
+ state.restoredBackup = undefined;
+ }
+ "
+ >
+ <CreateDatabasePrepForm
+ :projectId="database.project.id"
+ :environmentId="database.instance.environment.id"
+ :instanceId="database.instance.id"
+ :backup="state.restoredBackup"
+ @dismiss="
+ () => {
+ state.showRestoreBackupModal = false;
+ state.restoredBackup = undefined;
+ }
+ "
+ />
+ </BBModal>
</template>
<script lang="ts">
-import { computed, PropType } from "vue";
+import { computed, PropType, reactive } from "vue";
import { BBTableColumn, BBTableSectionDataSource } from "../bbkit/types";
import { Backup, Database } from "../types";
import { bytesToString } from "../utils";
import { useStore } from "vuex";
+import CreateDatabasePrepForm from "../components/CreateDatabasePrepForm.vue";
const columnList: BBTableColumn[] = [
{
@@ -131,13 +170,18 @@ const columnList: BBTableColumn[] = [
title: "Creator",
},
{
- title: "Clone",
+ title: "Restore",
},
];
+interface LocalState {
+ showRestoreBackupModal: boolean;
+ restoredBackup?: Backup;
+}
+
export default {
name: "BackupTable",
- components: {},
+ components: { CreateDatabasePrepForm },
props: {
database: {
required: true,
@@ -151,6 +195,10 @@ export default {
setup(props, ctx) {
const store = useStore();
+ const state = reactive<LocalState>({
+ showRestoreBackupModal: false,
+ });
+
const backupSectionList = computed(() => {
const manualList: Backup[] = [];
const automaticList: Backup[] = [];
@@ -202,6 +250,7 @@ export default {
};
return {
+ state,
columnList,
bytesToString,
backupSectionList,
diff --git a/frontend/src/components/CreateDatabasePrepForm.vue b/frontend/src/components/CreateDatabasePrepForm.vue
index 8d026aabf7..17fa5e528c 100644
--- a/frontend/src/components/CreateDatabasePrepForm.vue
+++ b/frontend/src/components/CreateDatabasePrepForm.vue
@@ -40,6 +40,7 @@
class="mt-1 w-full"
id="environment"
name="environment"
+ :disabled="!allowEditEnvironment"
:selectedId="state.environmentId"
@select-environment-id="selectEnvironment"
/>
@@ -56,6 +57,7 @@
class="mt-1"
id="instance"
name="instance"
+ :disabled="!allowEditInstance"
:selectedId="state.instanceId"
:environmentId="state.environmentId"
@select-instance-id="selectInstance"
@@ -150,6 +152,8 @@ import {
IssueCreate,
SYSTEM_BOT_ID,
PrincipalId,
+ Backup,
+ StageCreate,
} from "../types";
import { isDBAOrOwner, issueSlug } from "../utils";
@@ -170,6 +174,16 @@ export default {
projectId: {
type: Number as PropType<ProjectId>,
},
+ environmentId: {
+ type: Number as PropType<EnvironmentId>,
+ },
+ instanceId: {
+ type: Number as PropType<InstanceId>,
+ },
+ // If specified, then we are creating a database from the backup.
+ backup: {
+ type: Object as PropType<Backup>,
+ },
},
components: {
InstanceSelect,
@@ -210,6 +224,8 @@ export default {
const state = reactive<LocalState>({
projectId: props.projectId,
+ environmentId: props.environmentId,
+ instanceId: props.instanceId,
characterSet: "utf8mb4",
collation: "utf8mb4_general_ci",
assigneeId: showAssigneeSelect.value ? undefined : SYSTEM_BOT_ID,
@@ -237,6 +253,16 @@ export default {
return !props.projectId;
});
+ // If environment has been specified, then we disallow changing it.
+ const allowEditEnvironment = computed(() => {
+ return !props.environmentId;
+ });
+
+ // If instance has been specified, then we disallow changing it.
+ const allowEditInstance = computed(() => {
+ return !props.instanceId;
+ });
+
const selectProject = (projectId: ProjectId) => {
state.projectId = projectId;
};
@@ -258,40 +284,75 @@ export default {
};
const create = async () => {
- const newIssue: IssueCreate = {
- name: `Create database ${state.databaseName}`,
- type: "bb.issue.database.create",
- description: "",
- assigneeId: state.assigneeId!,
- projectId: state.projectId!,
- pipeline: {
- stageList: [
+ const stageList: StageCreate[] = [
+ {
+ name: "Create database",
+ environmentId: state.environmentId!,
+ taskList: [
{
- name: "Create database",
- environmentId: state.environmentId!,
- taskList: [
- {
- name: `Create database ${state.databaseName}`,
- // If current user is DBA or Owner, then the created task will start automatically,
- // otherwise, it will require approval.
- status: isDBAOrOwner(currentUser.value.role)
- ? "PENDING"
- : "PENDING_APPROVAL",
- type: "bb.task.database.create",
- instanceId: state.instanceId!,
- statement: `CREATE DATABASE \`${state.databaseName}\`\nCHARACTER SET ${state.characterSet} COLLATE ${state.collation}`,
- rollbackStatement: "",
- databaseName: state.databaseName,
- characterSet: state.characterSet,
- collation: state.collation,
- },
- ],
+ name: `Create database '${state.databaseName}'`,
+ // If current user is DBA or Owner, then the created task will start automatically,
+ // otherwise, it will require approval.
+ status: isDBAOrOwner(currentUser.value.role)
+ ? "PENDING"
+ : "PENDING_APPROVAL",
+ type: "bb.task.database.create",
+ instanceId: state.instanceId!,
+ statement: `CREATE DATABASE \`${state.databaseName}\`\nCHARACTER SET ${state.characterSet} COLLATE ${state.collation}`,
+ rollbackStatement: "",
+ databaseName: state.databaseName,
+ characterSet: state.characterSet,
+ collation: state.collation,
},
],
- name: `Pipeline - Create database ${state.databaseName}`,
},
- payload: {},
- };
+ ];
+
+ // If backup is specified, then we add an additional stage to restore the backup to the newly created database.
+ if (props.backup) {
+ stageList.push({
+ name: "Restore backup",
+ environmentId: state.environmentId!,
+ taskList: [
+ {
+ name: `Restore backup '${props.backup.name}'`,
+ // Use "PENDING" here since we consider the required approval has already been granted in the first stage.
+ status: "PENDING",
+ type: "bb.task.database.restore",
+ instanceId: state.instanceId!,
+ statement: "",
+ rollbackStatement: "",
+ databaseName: state.databaseName,
+ backupId: props.backup.id,
+ },
+ ],
+ });
+ }
+ const newIssue: IssueCreate = props.backup
+ ? {
+ name: `Create database '${state.databaseName}' from backup '${props.backup.name}'`,
+ type: "bb.issue.database.create",
+ description: `Creating database from backup '${props.backup.name}'`,
+ assigneeId: state.assigneeId!,
+ projectId: state.projectId!,
+ pipeline: {
+ stageList,
+ name: `Pipeline - Create database '${state.databaseName}' from backup '${props.backup.name}'`,
+ },
+ payload: {},
+ }
+ : {
+ name: `Create database '${state.databaseName}'`,
+ type: "bb.issue.database.create",
+ description: "",
+ assigneeId: state.assigneeId!,
+ projectId: state.projectId!,
+ pipeline: {
+ stageList,
+ name: `Pipeline - Create database ${state.databaseName}`,
+ },
+ payload: {},
+ };
store.dispatch("issue/createIssue", newIssue).then((createdIssue) => {
router.push(`/issue/${issueSlug(createdIssue.name, createdIssue.id)}`);
});
@@ -302,6 +363,8 @@ export default {
isReservedName,
allowCreate,
allowEditProject,
+ allowEditEnvironment,
+ allowEditInstance,
showAssigneeSelect,
selectProject,
selectEnvironment,
diff --git a/frontend/src/types/pipeline/task.ts b/frontend/src/types/pipeline/task.ts
index 85bd70962c..a7aab31570 100644
--- a/frontend/src/types/pipeline/task.ts
+++ b/frontend/src/types/pipeline/task.ts
@@ -1,5 +1,5 @@
import { Database } from "../database";
-import { DatabaseId, InstanceId, TaskId, TaskRunId } from "../id";
+import { BackupId, DatabaseId, InstanceId, TaskId, TaskRunId } from "../id";
import { Instance } from "../instance";
import { Principal } from "../principal";
import { VCSPushEvent } from "../vcs";
@@ -9,7 +9,8 @@ import { Stage } from "./stage";
export type TaskType =
| "bb.task.general"
| "bb.task.database.create"
- | "bb.task.database.schema.update";
+ | "bb.task.database.schema.update"
+ | "bb.task.database.restore";
export type TaskStatus =
| "PENDING"
@@ -77,6 +78,7 @@ export type TaskCreate = {
databaseName?: string;
characterSet?: string;
collation?: string;
+ backupId?: BackupId;
};
export type TaskStatusPatch = {
diff --git a/server/issue.go b/server/issue.go
index 620aef6b06..32956c17c7 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -338,6 +338,13 @@ func (s *Server) CreateIssue(ctx context.Context, issueCreate *api.IssueCreate,
if taskCreate.Statement == "" {
return nil, fmt.Errorf("failed to create schema update task, sql statement missing")
}
+ } else if taskCreate.Type == api.TaskDatabaseRestore {
+ if taskCreate.DatabaseName == "" {
+ return nil, fmt.Errorf("failed to create restore database task, database name missing")
+ }
+ if taskCreate.BackupId == nil {
+ return nil, fmt.Errorf("failed to create restore database task, backup missing")
+ }
}
}
}
@@ -385,6 +392,15 @@ func (s *Server) CreateIssue(ctx context.Context, issueCreate *api.IssueCreate,
return nil, fmt.Errorf("failed to create schema update task, unable to marshal payload %w", err)
}
taskCreate.Payload = string(bytes)
+ } else if taskCreate.Type == api.TaskDatabaseRestore {
+ payload := api.TaskDatabaseRestorePayload{}
+ payload.DatabaseName = taskCreate.DatabaseName
+ payload.BackupId = *taskCreate.BackupId
+ bytes, err := json.Marshal(payload)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create restore database task, unable to marshal payload %w", err)
+ }
+ taskCreate.Payload = string(bytes)
}
_, err := s.TaskService.CreateTask(context.Background(), &taskCreate)
if err != nil {