Skip to content

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

chore(frontend): move rollback button from issue comment to sidebar (#12981)

boojackec8a57a27ca4
diff --git a/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCommentAction.vue b/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCommentAction.vue
index 6bc330fea8..f2cc08523f 100644
--- a/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCommentAction.vue
+++ b/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCommentAction.vue
@@ -13,17 +13,9 @@
<ActionSentence :issue="issue" :issue-comment="issueComment" />
- <NButton
- v-if="showRestoreButton(issueComment)"
- size="small"
- @click.prevent="createRestoreIssue(issueComment)"
- >
- <span>{{ $t("activity.restore") }}</span>
- </NButton>
-
<HumanizeTs
:ts="(issueComment.createTime?.getTime() ?? 0) / 1000"
- class="ml-1"
+ class="ml-1 text-gray-400"
/>
<span
@@ -61,107 +53,20 @@
</template>
<script lang="ts" setup>
-import dayjs from "dayjs";
-import { NButton } from "naive-ui";
-import { computed } from "vue";
-import { useRouter } from "vue-router";
-import { useIssueContext, databaseForTask } from "@/components/IssueV1/logic";
import HumanizeTs from "@/components/misc/HumanizeTs.vue";
-import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
-import {
- IssueCommentType,
- useSQLStore,
- type ComposedIssueComment,
-} from "@/store";
-import { useSheetV1Store, useUserStore } from "@/store";
+import { IssueCommentType, type ComposedIssueComment } from "@/store";
+import { useUserStore } from "@/store";
import type { ComposedIssue } from "@/types";
-import { Engine } from "@/types/proto/v1/common";
-import { IssueComment_TaskPriorBackup } from "@/types/proto/v1/issue_service";
-import type { IssueComment } from "@/types/proto/v1/issue_service";
-import {
- extractUserResourceName,
- extractProjectResourceName,
- sheetNameOfTaskV1,
- extractDatabaseResourceName,
-} from "@/utils";
+import { extractUserResourceName } from "@/utils";
import ActionCreator from "./ActionCreator.vue";
import ActionSentence from "./ActionSentence.vue";
-const props = defineProps<{
+defineProps<{
issue: ComposedIssue;
index: number;
issueComment: ComposedIssueComment;
similar: ComposedIssueComment[];
}>();
-const { selectedTask } = useIssueContext();
-const router = useRouter();
const userStore = useUserStore();
-
-const coreDatabaseInfo = computed(() => {
- return databaseForTask(props.issue, selectedTask.value);
-});
-
-const showRestoreButton = (comment: ComposedIssueComment) => {
- if (comment.type !== IssueCommentType.TASK_PRIOR_BACKUP) {
- return false;
- }
- if (!selectedTask.value) {
- return false;
- }
- if (coreDatabaseInfo.value.instanceResource.engine !== Engine.MYSQL) {
- return false;
- }
- return true;
-};
-
-const createRestoreIssue = async (comment: IssueComment) => {
- const {
- tables,
- originalLine,
- database: backupDatabase,
- } = IssueComment_TaskPriorBackup.fromPartial(comment.taskPriorBackup || {});
-
- const issueNameParts: string[] = [];
- issueNameParts.push(
- `Restore the sql at line ${originalLine} of (${props.issue.title})`
- );
- const datetime = dayjs().format("%MM-DD HH:mm");
- const tz = "UTC" + dayjs().format("ZZ");
- issueNameParts.push(`${datetime} ${tz}`);
- const sheetName = sheetNameOfTaskV1(selectedTask.value);
- const sheet = useSheetV1Store().getSheetByName(sheetName);
- if (!sheet) {
- console.error(`Sheet ${sheetName} not found`);
- return;
- }
-
- const { instance } = extractDatabaseResourceName(selectedTask.value.target);
- const { statement: restoreSQL } = await useSQLStore().generateRestoreSQL({
- name: selectedTask.value.target,
- sheet: sheet.name,
- backupDataSource: `${instance}/databases/${backupDatabase.length > 0 ? backupDatabase : "bbdataarchive"}`,
- backupTable: tables[0].table,
- });
- if (!restoreSQL) {
- console.error("Failed to generate restore SQL");
- return;
- }
-
- const query: Record<string, any> = {
- template: "bb.issue.database.data.update",
- name: issueNameParts.join(" "),
- databaseList: selectedTask.value.target,
- sql: restoreSQL,
- };
-
- router.push({
- name: PROJECT_V1_ROUTE_ISSUE_DETAIL,
- params: {
- projectId: extractProjectResourceName(selectedTask.value.name),
- issueSlug: "create",
- },
- query,
- });
-};
</script>
diff --git a/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCreatedComment.vue b/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCreatedComment.vue
index 963868ee4a..0a3b9c42a1 100644
--- a/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCreatedComment.vue
+++ b/frontend/src/components/IssueV1/components/IssueCommentSection/IssueCommentView/IssueCreatedComment.vue
@@ -24,7 +24,7 @@
<span>{{ $t("activity.sentence.created-issue") }}</span>
<HumanizeTs
:ts="(issue.createTime?.getTime() ?? 0) / 1000"
- class="ml-1"
+ class="ml-1 text-gray-400"
/>
</div>
</div>
diff --git a/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/PreBackupSection.vue b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/PreBackupSection.vue
index 7ae5c3f07a..b5a027150f 100644
--- a/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/PreBackupSection.vue
+++ b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/PreBackupSection.vue
@@ -1,22 +1,31 @@
<template>
- <div
- v-if="showPreBackupSection"
- class="flex items-center justify-between gap-x-2 min-h-[34px] whitespace-nowrap"
- >
- <NTooltip>
- <template #trigger>
- <div class="textlabel flex items-center">
- {{ $t("task.prior-backup") }}
- </div>
- </template>
- <template #default>
- <div class="max-w-[20rem]">
- {{ $t("task.prior-backup-tips") }}
- </div>
- </template>
- </NTooltip>
- <div class="flex items-center gap-x-2">
- <PreBackupSwitch />
+ <div>
+ <div
+ v-if="showPreBackupSection"
+ class="w-full flex items-center justify-between gap-x-2 gap-y-1 flex-wrap min-h-[34px] whitespace-nowrap"
+ >
+ <NTooltip>
+ <template #trigger>
+ <div class="textlabel flex items-center">
+ <span>{{ $t("task.prior-backup") }}</span>
+ <span
+ v-if="showRollbackSection"
+ class="ml-1 px-1 py-0.5 rounded-lg text-xs bg-green-100 text-green-800"
+ >
+ {{ $t("common.on") }}
+ </span>
+ </div>
+ </template>
+ <template #default>
+ <div class="max-w-[20rem]">
+ {{ $t("task.prior-backup-tips") }}
+ </div>
+ </template>
+ </NTooltip>
+ <div class="flex items-center">
+ <TaskRollbackButton v-if="showRollbackSection" />
+ <PreBackupSwitch v-else />
+ </div>
</div>
</div>
</template>
@@ -24,7 +33,8 @@
<script lang="ts" setup>
import { NTooltip } from "naive-ui";
import PreBackupSwitch from "./PreBackupSwitch.vue";
+import TaskRollbackButton from "./TaskRollbackButton.vue";
import { usePreBackupContext } from "./common";
-const { showPreBackupSection } = usePreBackupContext();
+const { showPreBackupSection, showRollbackSection } = usePreBackupContext();
</script>
diff --git a/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/TaskRollbackButton.vue b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/TaskRollbackButton.vue
new file mode 100644
index 0000000000..99522019c3
--- /dev/null
+++ b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/TaskRollbackButton.vue
@@ -0,0 +1,86 @@
+<template>
+ <NButton size="small" :loading="isLoading" @click="createRestoreIssue">
+ <template #icon>
+ <Undo2Icon class="w-4 h-auto" />
+ </template>
+ {{ $t("common.rollback") }}
+ </NButton>
+</template>
+
+<script lang="ts" setup>
+import { Undo2Icon } from "lucide-vue-next";
+import { NButton } from "naive-ui";
+import { computed, ref } from "vue";
+import { useRouter } from "vue-router";
+import {
+ latestTaskRunForTask,
+ useIssueContext,
+} from "@/components/IssueV1/logic";
+import { PROJECT_V1_ROUTE_ISSUE_DETAIL } from "@/router/dashboard/projectV1";
+import { pushNotification, useSheetV1Store, useSQLStore } from "@/store";
+import { TaskRun_PriorBackupDetail_Item_Table } from "@/types/proto/v1/rollout_service";
+import { extractProjectResourceName, sheetNameOfTaskV1 } from "@/utils";
+import { usePreBackupContext } from "./common";
+
+const router = useRouter();
+const { issue, selectedTask } = useIssueContext();
+const { allowRollback } = usePreBackupContext();
+
+const isLoading = ref(false);
+
+const latestTaskRun = computed(() =>
+ latestTaskRunForTask(issue.value, selectedTask.value)
+);
+
+const createRestoreIssue = async () => {
+ if (!allowRollback.value) {
+ return;
+ }
+ if (!latestTaskRun.value?.priorBackupDetail) {
+ return;
+ }
+
+ const sheetName = sheetNameOfTaskV1(selectedTask.value);
+ const sheet = useSheetV1Store().getSheetByName(sheetName);
+ if (!sheet) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: `Sheet ${sheetName} not found`,
+ });
+ return;
+ }
+
+ isLoading.value = true;
+ const statements: string[] = [];
+ for (const item of latestTaskRun.value.priorBackupDetail.items) {
+ const targetTable = TaskRun_PriorBackupDetail_Item_Table.fromPartial({
+ ...item.targetTable,
+ });
+ const { statement } = await useSQLStore().generateRestoreSQL({
+ name: selectedTask.value.target,
+ sheet: sheet.name,
+ backupDataSource: targetTable.database,
+ backupTable: targetTable.table,
+ });
+ statements.push(statement);
+ }
+ isLoading.value = false;
+
+ const query: Record<string, any> = {
+ template: "bb.issue.database.data.update",
+ name: `Rollback ${selectedTask.value.title} in issue#${issue.value.uid}`,
+ databaseList: selectedTask.value.target,
+ sql: statements.join("\n"),
+ description: `This issue is created to rollback the data of ${selectedTask.value.title} in issue#${issue.value.uid}`,
+ };
+ router.push({
+ name: PROJECT_V1_ROUTE_ISSUE_DETAIL,
+ params: {
+ projectId: extractProjectResourceName(issue.value.name),
+ issueSlug: "create",
+ },
+ query,
+ });
+};
+</script>
diff --git a/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/common.ts b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/common.ts
index f1c8f49315..445f011115 100644
--- a/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/common.ts
+++ b/frontend/src/components/IssueV1/components/Sidebar/PreBackupSection/common.ts
@@ -3,6 +3,7 @@ import { computed } from "vue";
import { useI18n } from "vue-i18n";
import {
databaseForTask,
+ latestTaskRunForTask,
notifyNotEditableLegacyIssue,
specForTask,
useIssueContext,
@@ -14,48 +15,66 @@ import {
useIssueCommentStore,
} from "@/store";
import { Engine } from "@/types/proto/v1/common";
-import { Task_Status, Task_Type } from "@/types/proto/v1/rollout_service";
+import {
+ Task_Status,
+ Task_Type,
+ TaskRun_Status,
+} from "@/types/proto/v1/rollout_service";
import {
extractUserResourceName,
hasProjectPermissionV2,
isDatabaseChangeRelatedIssue,
} from "@/utils";
+const PRE_BACKUP_AVAILABLE_ENGINES = [
+ Engine.MYSQL,
+ Engine.TIDB,
+ Engine.MSSQL,
+ Engine.ORACLE,
+ Engine.POSTGRES,
+];
+
+const ROLLBACK_AVAILABLE_ENGINES = [Engine.MYSQL];
+
export const usePreBackupContext = () => {
- const currentUserV1 = useCurrentUserV1();
- const context = useIssueContext();
const { t } = useI18n();
- const { isCreating, issue, selectedTask: task, events } = context;
+ const currentUserV1 = useCurrentUserV1();
+ const { isCreating, issue, selectedTask, events } = useIssueContext();
+
const project = computed(() => issue.value.projectEntity);
+ const database = computed(() =>
+ databaseForTask(issue.value, selectedTask.value)
+ );
+
+ const latestTaskRun = computed(() =>
+ latestTaskRunForTask(issue.value, selectedTask.value)
+ );
+
const showPreBackupSection = computed((): boolean => {
if (!isDatabaseChangeRelatedIssue(issue.value)) {
return false;
}
- if (task.value.type !== Task_Type.DATABASE_DATA_UPDATE) {
+ if (selectedTask.value.type !== Task_Type.DATABASE_DATA_UPDATE) {
return false;
}
- const database = databaseForTask(issue.value, task.value);
- const { engine } = database.instanceResource;
- if (
- engine !== Engine.MYSQL &&
- engine !== Engine.TIDB &&
- engine !== Engine.MSSQL &&
- engine !== Engine.ORACLE &&
- engine !== Engine.POSTGRES
- ) {
+ const { engine } = database.value.instanceResource;
+ if (!PRE_BACKUP_AVAILABLE_ENGINES.includes(engine)) {
return false;
}
return true;
});
const allowPreBackup = computed((): boolean => {
+ // Always allow pre-backup when creating.
if (isCreating.value) {
return true;
}
+
+ // Only allow pre-backup for non-done and non-running tasks.
if (
- ![Task_Status.NOT_STARTED, Task_Status.PENDING].includes(
- task.value.status
+ [Task_Status.DONE, Task_Status.RUNNING].includes(
+ selectedTask.value.status
)
) {
return false;
@@ -63,8 +82,8 @@ export const usePreBackupContext = () => {
const user = currentUserV1.value;
+ // Allowed to the issue creator.
if (user.email === extractUserResourceName(issue.value.creator)) {
- // Allowed to the issue creator.
return true;
}
@@ -76,15 +95,14 @@ export const usePreBackupContext = () => {
});
const preBackupEnabled = computed((): boolean => {
- const spec = specForTask(issue.value.planEntity, task.value);
+ const spec = specForTask(issue.value.planEntity, selectedTask.value);
const database =
spec?.changeDatabaseConfig?.preUpdateBackupDetail?.database;
return database !== undefined && database !== "";
});
const archiveDatabase = computed((): string => {
- const database = databaseForTask(issue.value, task.value);
- const { engine } = database.instanceResource;
+ const { engine } = database.value.instanceResource;
if (engine === Engine.ORACLE) {
return "BBDATAARCHIVE";
}
@@ -94,12 +112,12 @@ export const usePreBackupContext = () => {
const togglePreBackup = async (on: boolean) => {
if (isCreating.value) {
- const spec = specForTask(issue.value.planEntity, task.value);
+ const spec = specForTask(issue.value.planEntity, selectedTask.value);
if (spec && spec.changeDatabaseConfig) {
- const database = databaseForTask(issue.value, task.value);
if (on) {
spec.changeDatabaseConfig.preUpdateBackupDetail = {
- database: database.instance + "/databases/" + archiveDatabase.value,
+ database:
+ database.value.instance + "/databases/" + archiveDatabase.value,
};
} else {
spec.changeDatabaseConfig.preUpdateBackupDetail = undefined;
@@ -108,15 +126,16 @@ export const usePreBackupContext = () => {
} else {
// patch plan to reconcile rollout/stages/tasks
const planPatch = cloneDeep(issue.value.planEntity);
- const spec = specForTask(planPatch, task.value);
+ const spec = specForTask(planPatch, selectedTask.value);
if (!planPatch || !spec || !spec.changeDatabaseConfig) {
notifyNotEditableLegacyIssue();
return;
}
- const database = databaseForTask(issue.value, task.value);
+
if (on) {
spec.changeDatabaseConfig.preUpdateBackupDetail = {
- database: database.instance + "/databases/" + archiveDatabase.value,
+ database:
+ database.value.instance + "/databases/" + archiveDatabase.value,
};
} else {
spec.changeDatabaseConfig.preUpdateBackupDetail = undefined;
@@ -128,7 +147,6 @@ export const usePreBackupContext = () => {
});
issue.value.planEntity = updatedPlan;
- const action = on ? "Enable" : "Disable";
events.emit("status-changed", { eager: true });
pushNotification({
module: "bytebase",
@@ -136,10 +154,11 @@ export const usePreBackupContext = () => {
title: t("common.updated"),
});
+ const action = on ? "Enable" : "Disable";
try {
await useIssueCommentStore().createIssueComment({
issueName: issue.value.name,
- comment: `${action} prior backup for task [${task.value.title}].`,
+ comment: `${action} prior backup for task [${selectedTask.value.title}].`,
});
} catch {
// fail to comment won't be too bad
@@ -147,10 +166,49 @@ export const usePreBackupContext = () => {
}
};
+ const showRollbackSection = computed((): boolean => {
+ if (!showPreBackupSection.value) {
+ return false;
+ }
+ if (!preBackupEnabled.value) {
+ return false;
+ }
+ if (
+ !ROLLBACK_AVAILABLE_ENGINES.includes(
+ database.value.instanceResource.engine
+ )
+ ) {
+ return false;
+ }
+ if (!latestTaskRun.value) {
+ return false;
+ }
+ if (latestTaskRun.value.status !== TaskRun_Status.DONE) {
+ return false;
+ }
+ if (latestTaskRun.value.priorBackupDetail?.items.length === 0) {
+ return false;
+ }
+ return true;
+ });
+
+ const allowRollback = computed((): boolean => {
+ return hasProjectPermissionV2(
+ project.value,
+ currentUserV1.value,
+ "bb.issues.create"
+ );
+ });
+
return {
+ // Pre-backup related.
showPreBackupSection,
preBackupEnabled,
allowPreBackup,
togglePreBackup,
+
+ // Rollback related.
+ showRollbackSection,
+ allowRollback,
};
};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 13c14be1cf..24cc05cb36 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -278,7 +278,8 @@
"closed": "Closed",
"options": "Options",
"reset": "Reset",
- "refresh": "Refresh"
+ "refresh": "Refresh",
+ "rollback": "Rollback"
},
"error-page": {
"go-back-home": "Go back home"
@@ -398,7 +399,7 @@
"disable": "Disable online migration",
"advice-split-statement-to-single-online-migration-issue": "Advice splitting this statement to single online migration issue."
},
- "prior-backup-tips": "Back up affected data ahead of time"
+ "prior-backup-tips": "Backup affected data ahead of time"
},
"task-run": {
"status": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 5741f40cfd..ddd5135e48 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -278,7 +278,8 @@
"closed": "Cerrado",
"options": "Opciones",
"reset": "Reiniciar",
- "refresh": "Actualizar"
+ "refresh": "Actualizar",
+ "rollback": "Retroceder"
},
"error-page": {
"go-back-home": "Volver al inicio"
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 51b395d219..b9ed594bf6 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -278,7 +278,8 @@
"closed": "閉まっている",
"options": "オプション",
"reset": "リセット",
- "refresh": "リフレッシュ"
+ "refresh": "リフレッシュ",
+ "rollback": "ロールバック"
},
"error-page": {
"go-back-home": "ホームページに戻る"
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 9ed1c1ef02..28d55a9d7c 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -278,7 +278,8 @@
"closed": "已关闭",
"options": "配置项",
"reset": "重置",
- "refresh": "刷新"
+ "refresh": "刷新",
+ "rollback": "回滚"
},
"error-page": {
"go-back-home": "回到主页"
@@ -3644,4 +3645,4 @@
"self": "最后更新"
}
}
-}
+}
\ No newline at end of file