Skip to content

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

feat(frontend): PITR to new db (#1978)

Liu Ji2e7bb8b312a4
diff --git a/frontend/src/components/DatabaseDetail/CreatePITRDatabaseForm.vue b/frontend/src/components/DatabaseDetail/CreatePITRDatabaseForm.vue
new file mode 100644
index 0000000000..35055de2c1
--- /dev/null
+++ b/frontend/src/components/DatabaseDetail/CreatePITRDatabaseForm.vue
@@ -0,0 +1,292 @@
+<template>
+ <div class="space-y-2">
+ <label class="textlabel">
+ {{ $t("common.project") }}
+ <span class="text-red-600">*</span>
+ </label>
+ <ProjectSelect
+ class="mt-1"
+ :disabled="true"
+ :include-default-project="true"
+ :selected-id="state.context.projectId"
+ @select-project-id="(id) => (state.context.projectId = id)"
+ />
+ </div>
+
+ <!-- Providing a preview of generated database name in template mode -->
+ <div v-if="isDbNameTemplateMode" class="space-y-2">
+ <label class="textlabel w-full flex items-center gap-1">
+ {{ $t("create-db.generated-database-name") }}
+ <NTooltip trigger="hover" placement="top">
+ <template #trigger>
+ <heroicons-outline:question-mark-circle
+ class="w-4 h-4 inline-block"
+ />
+ </template>
+ <div class="whitespace-nowrap">
+ {{
+ $t("create-db.db-name-generated-by-template", {
+ template: project.dbNameTemplate,
+ })
+ }}
+ </div>
+ </NTooltip>
+ </label>
+ <input
+ id="name"
+ disabled
+ name="name"
+ type="text"
+ class="textfield mt-1 w-full"
+ :value="generatedDatabaseName"
+ />
+ </div>
+
+ <div class="col-span-2 col-start-2 w-64">
+ <label for="name" class="textlabel">
+ {{ $t("create-db.new-database-name") }}
+ <span class="text-red-600">*</span>
+ </label>
+ <input
+ id="name"
+ v-model="state.context.databaseName"
+ required
+ name="name"
+ type="text"
+ class="textfield mt-1 w-full"
+ />
+ <span v-if="isReservedName" class="text-red-600">
+ <i18n-t keypath="create-db.reserved-db-error">
+ <template #databaseName>
+ {{ state.context.databaseName }}
+ </template>
+ </i18n-t>
+ </span>
+ </div>
+
+ <!-- Providing more dropdowns for required labels as if they are normal required props of DB -->
+ <DatabaseLabelForm
+ v-if="isTenantProject"
+ ref="labelForm"
+ :project="project"
+ :label-list="state.context.labelList"
+ filter="required"
+ />
+
+ <div class="space-y-2">
+ <label class="textlabel">
+ {{ $t("common.environment") }}
+ <span class="text-red-600">*</span>
+ </label>
+ <EnvironmentSelect
+ class="mt-1"
+ :selected-id="state.context.environmentId"
+ :disabled="true"
+ @select-environment-id="(id) => (state.context.environmentId = id)"
+ />
+ </div>
+
+ <div class="space-y-2">
+ <label class="textlabel w-full flex items-center gap-1">
+ <InstanceEngineIcon
+ v-if="state.context.instanceId"
+ :instance="selectedInstance"
+ />
+ <label for="instance" class="textlabel">
+ {{ $t("common.instance") }} <span class="text-red-600">*</span>
+ </label>
+ </label>
+ <InstanceSelect
+ class="mt-1"
+ :selected-id="state.context.instanceId"
+ :environment-id="state.context.environmentId"
+ :filter="instanceFilter"
+ @select-instance-id="(id) => (state.context.instanceId = id)"
+ />
+ </div>
+
+ <!-- Providing other dropdowns for optional labels as if they are normal optional props of DB -->
+ <DatabaseLabelForm
+ :project="project"
+ :label-list="state.context.labelList"
+ filter="optional"
+ />
+
+ <div class="space-y-2">
+ <label class="textlabel w-full flex gap-1">
+ {{
+ selectedInstance.engine == "POSTGRES"
+ ? $t("db.encoding")
+ : $t("db.character-set")
+ }}
+ </label>
+ <input
+ id="charset"
+ v-model="state.context.characterSet"
+ name="charset"
+ type="text"
+ class="textfield mt-1 w-full"
+ :placeholder="defaultCharset(selectedInstance.engine)"
+ />
+ </div>
+
+ <div class="col-span-2 col-start-2 w-64">
+ <label for="collation" class="textlabel">
+ {{ $t("db.collation") }}
+ </label>
+ <input
+ id="collation"
+ v-model="state.context.collation"
+ name="collation"
+ type="text"
+ class="textfield mt-1 w-full"
+ :placeholder="defaultCollation(selectedInstance.engine) || 'default'"
+ />
+ </div>
+
+ <!-- Assignee is not required. Since we are definitely DBA or Owner to see this form -->
+</template>
+
+<script lang="ts" setup>
+import { computed, onBeforeMount, PropType, reactive, ref, watch } from "vue";
+import { cloneDeep, isEmpty } from "lodash-es";
+import {
+ Database,
+ Instance,
+ Project,
+ defaultCharset,
+ defaultCollation,
+} from "@/types";
+import { CreatePITRDatabaseContext } from "./utils";
+import { DatabaseLabelForm } from "@/components/CreateDatabasePrepForm";
+import { useInstanceStore, useProjectStore } from "@/store";
+import { buildDatabaseNameByTemplateAndLabelList } from "@/utils";
+import { isPITRAvailableOnInstance } from "@/plugins/pitr";
+
+interface LocalState {
+ context: CreatePITRDatabaseContext;
+}
+
+const props = defineProps({
+ database: {
+ type: Object as PropType<Database>,
+ required: true,
+ },
+ context: {
+ type: Object as PropType<CreatePITRDatabaseContext>,
+ default: undefined,
+ },
+});
+
+const emit = defineEmits<{
+ (e: "update", context: CreatePITRDatabaseContext): void;
+}>();
+
+const extractLocalContextFromProps = (): CreatePITRDatabaseContext => {
+ const { database, context } = props;
+ if (context) {
+ return context;
+ } else {
+ return {
+ projectId: database.project.id,
+ instanceId: database.instance.id,
+ environmentId: database.instance.environment.id,
+ databaseName: `${database.name}_recovery`, // looks like "my_db_recovery"
+ characterSet: database.characterSet,
+ collation: database.collation,
+ labelList: cloneDeep(database.labels),
+ };
+ }
+};
+
+const instanceStore = useInstanceStore();
+const projectStore = useProjectStore();
+
+// Refresh the instance list
+const prepareInstanceList = () => {
+ instanceStore.fetchInstanceList();
+};
+
+onBeforeMount(prepareInstanceList);
+
+const state = reactive<LocalState>({
+ context: extractLocalContextFromProps(),
+});
+
+const project = computed((): Project => {
+ return projectStore.getProjectById(state.context.projectId);
+});
+
+const isReservedName = computed(() => {
+ return state.context.databaseName.toLowerCase() == "bytebase";
+});
+
+const isTenantProject = computed((): boolean => {
+ return project.value.tenantMode === "TENANT";
+});
+
+// reference to <DatabaseLabelForm /> to call validate()
+const labelForm = ref<InstanceType<typeof DatabaseLabelForm> | null>(null);
+
+const isDbNameTemplateMode = computed((): boolean => {
+ // true if dbNameTemplate is not empty
+ return !!project.value.dbNameTemplate;
+});
+
+const generatedDatabaseName = computed((): string => {
+ if (!isDbNameTemplateMode.value) {
+ // don't modify anything if we are not in template mode
+ return state.context.databaseName;
+ }
+
+ return buildDatabaseNameByTemplateAndLabelList(
+ project.value.dbNameTemplate,
+ state.context.databaseName,
+ state.context.labelList,
+ true // keepEmpty: true to keep non-selected values as original placeholders
+ );
+});
+
+const selectedInstance = computed((): Instance => {
+ return instanceStore.getInstanceById(state.context.instanceId);
+});
+
+const instanceFilter = (instance: Instance): boolean => {
+ return isPITRAvailableOnInstance(instance);
+};
+
+// Sync values from props when changes.
+watch([() => props.database, () => props.context], () => {
+ state.context = extractLocalContextFromProps();
+});
+
+// Emit 'update' event when local value changes.
+watch(
+ () => state.context,
+ (context) => {
+ emit("update", context);
+ },
+ {
+ deep: true,
+ immediate: true,
+ }
+);
+
+const validate = (): boolean => {
+ // If we are not in template mode, none of labels are required
+ // So we just treat this case as 'yes, valid'
+ const isLabelValid = isDbNameTemplateMode.value
+ ? labelForm.value?.validate()
+ : true;
+ return (
+ !isEmpty(state.context.databaseName) &&
+ !isReservedName.value &&
+ !!isLabelValid &&
+ !!state.context.projectId &&
+ !!state.context.environmentId &&
+ !!state.context.instanceId
+ );
+};
+
+defineExpose({ validate });
+</script>
diff --git a/frontend/src/components/DatabaseDetail/PITRRestoreButton.vue b/frontend/src/components/DatabaseDetail/PITRRestoreButton.vue
index 517a9852ae..ce7c6ceddd 100644
--- a/frontend/src/components/DatabaseDetail/PITRRestoreButton.vue
+++ b/frontend/src/components/DatabaseDetail/PITRRestoreButton.vue
@@ -62,8 +62,8 @@
>
<template #link>
<a
- class="normal-link inline-flex items-center gap-x-1"
- href="https://github.com/bytebase/bytebase/blob/main/docs/design/pitr-mysql.md"
+ class="normal-link inline-flex items-center"
+ href="https://www.bytebase.com/docs/disaster-recovery/point-in-time-recovery-for-mysql"
target="__BLANK"
>
{{ $t("common.learn-more") }}
@@ -83,15 +83,65 @@
</template>
<template v-else>
- <div class="w-64 space-y-2">
- <label class="textlabel w-full flex flex-col gap-1">
- <span>{{ $t("database.pitr.point-in-time") }}</span>
- <span class="text-gray-400 text-xs">{{ timezone }}</span>
- </label>
- <NDatePicker
- v-model:value="state.pitrTimestampMS"
- type="datetime"
- :disabled="state.mode === 'LAST_MIGRATION'"
+ <div class="w-[16.5rem] space-y-4">
+ <div class="space-y-2">
+ <label class="textlabel w-full flex items-baseline">
+ <span>{{ $t("database.pitr.point-in-time") }}</span>
+ <span class="text-gray-400 text-xs ml-2">{{ timezone }}</span>
+ <span class="text-red-600 ml-1">*</span>
+ </label>
+ <NDatePicker
+ v-model:value="state.pitrTimestampMS"
+ type="datetime"
+ :disabled="state.mode === 'LAST_MIGRATION'"
+ />
+ <div v-if="pitrTimestampError" class="text-sm text-red-600">
+ {{ pitrTimestampError }}
+ </div>
+ </div>
+
+ <div class="space-y-2">
+ <label class="textlabel w-full flex flex-col gap-1">
+ {{ $t("database.pitr.restore-to") }}
+ </label>
+ <div class="flex items-center gap-3 textlabel">
+ <label class="flex items-center">
+ <input
+ type="radio"
+ :checked="state.target === 'IN-PLACE'"
+ @input="state.target = 'IN-PLACE'"
+ />
+ <span class="ml-2">
+ {{ $t("database.pitr.restore-to-in-place") }}
+ </span>
+ </label>
+ <label class="flex items-center gap-2">
+ <input
+ type="radio"
+ :checked="state.target === 'NEW'"
+ @input="state.target = 'NEW'"
+ />
+ <span>{{ $t("database.pitr.restore-to-new-db") }}</span>
+ </label>
+ </div>
+
+ <div
+ v-if="state.target === 'IN-PLACE'"
+ class="flex items-center gap-2 text-error mt-2"
+ >
+ <heroicons-outline:exclamation-circle class="w-4 h-4" />
+ <span class="whitespace-nowrap text-sm">
+ {{ $t("database.pitr.will-overwrite-current-database") }}
+ </span>
+ </div>
+ </div>
+
+ <CreatePITRDatabaseForm
+ v-if="state.target === 'NEW'"
+ ref="createDatabaseForm"
+ :database="database"
+ :context="state.createContext"
+ @update="state.createContext = $event"
/>
</div>
</template>
@@ -116,20 +166,15 @@
{{ $t("common.next") }}
</button>
- <BBTooltipButton
+ <button
v-if="state.step === 'PITR_FORM'"
- type="primary"
- tooltip-mode="DISABLED-ONLY"
+ type="button"
+ class="btn-primary py-2 px-4"
:disabled="!!pitrTimestampError"
- @click="onConfirm"
+ @click.prevent="onConfirm"
>
{{ $t("common.confirm") }}
- <template #tooltip>
- <div class="whitespace-pre-wrap max-w-[20rem]">
- {{ pitrTimestampError }}
- </div>
- </template>
- </BBTooltipButton>
+ </button>
</div>
<div
@@ -149,15 +194,19 @@
</template>
<script lang="ts" setup>
-import { computed, PropType, reactive } from "vue";
+import { computed, PropType, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { NDatePicker } from "naive-ui";
import dayjs from "dayjs";
import { useI18n } from "vue-i18n";
-import { Database } from "@/types";
+import { CreateDatabaseContext, Database } from "@/types";
import { usePITRLogic } from "@/plugins";
import { issueSlug } from "@/utils";
import { featureToRef } from "@/store";
+import CreatePITRDatabaseForm from "./CreatePITRDatabaseForm.vue";
+import { CreatePITRDatabaseContext } from "./utils";
+
+type PITRTarget = "IN-PLACE" | "NEW";
type Mode = "LAST_MIGRATION" | "CUSTOM";
type Step = "LAST_MIGRATION_INFO" | "PITR_FORM";
@@ -167,6 +216,8 @@ interface LocalState {
mode: Mode;
step: Step;
pitrTimestampMS: number;
+ target: PITRTarget;
+ createContext: CreatePITRDatabaseContext | undefined;
loading: boolean;
showFeatureModal: boolean;
}
@@ -190,10 +241,14 @@ const state = reactive<LocalState>({
mode: "CUSTOM",
step: "PITR_FORM",
pitrTimestampMS: Date.now(),
+ target: "IN-PLACE",
+ createContext: undefined,
loading: false,
showFeatureModal: false,
});
+const createDatabaseForm = ref<InstanceType<typeof CreatePITRDatabaseForm>>();
+
const hasPITRFeature = featureToRef("bb.feature.disaster-recovery-pitr");
const timezone = computed(() => "UTC" + dayjs().format("ZZ"));
@@ -214,6 +269,8 @@ const earliest = computed((): number => {
return earliestAllowedRestoreTS * 1000;
});
+// Returns error message (string) if error occurs.
+// Returns undefined if validation passed.
const pitrTimestampError = computed((): string | undefined => {
const val = state.pitrTimestampMS;
const now = Date.now();
@@ -229,19 +286,40 @@ const pitrTimestampError = computed((): string | undefined => {
if (val > now) {
return t("database.pitr.no-later-than-now");
}
+
+ if (!createDatabaseForm.value?.validate()) {
+ return "";
+ }
+
return undefined;
});
+const createDatabaseContextError = computed((): boolean => {
+ const { target } = state;
+ if (target === "IN-PLACE") {
+ return false;
+ }
+ return !createDatabaseForm.value?.validate();
+});
+
+const isValidParams = computed((): boolean => {
+ return !pitrTimestampError.value && !createDatabaseContextError.value;
+});
+
const resetUI = () => {
state.loading = false;
state.showDatabasePITRModal = false;
state.pitrTimestampMS = Date.now();
+ state.target = "IN-PLACE";
+ state.createContext = undefined;
state.mode = "CUSTOM";
};
const openDialog = (step: Step = "PITR_FORM", mode: Mode = "CUSTOM") => {
state.showDatabasePITRModal = true;
state.pitrTimestampMS = Date.now();
+ state.target = "IN-PLACE";
+ state.createContext = undefined;
state.step = step;
state.mode = mode;
};
@@ -260,9 +338,31 @@ const onConfirm = async () => {
return;
}
+ if (!isValidParams.value) {
+ return;
+ }
+
state.loading = true;
try {
+ let createDatabaseContext: CreateDatabaseContext | undefined = undefined;
+ const { target, createContext: context } = state;
+ if (target === "NEW" && context) {
+ createDatabaseContext = {
+ projectId: context.projectId,
+ environmentId: context.environmentId,
+ instanceId: context.instanceId,
+ databaseName: context.databaseName,
+ characterSet: context.characterSet,
+ collation: context.collation,
+ owner: "",
+ cluster: "",
+ } as CreateDatabaseContext;
+ // Do not submit non-selected optional labels
+ const labelList = context.labelList.filter((label) => !!label.value);
+ createDatabaseContext.labels = JSON.stringify(labelList);
+ }
+
const issueNameParts: string[] = [
`Restore database [${props.database.name}]`,
];
@@ -278,6 +378,7 @@ const onConfirm = async () => {
}
const issue = await createPITRIssue(
Math.floor(state.pitrTimestampMS / 1000),
+ createDatabaseContext,
{
name: issueNameParts.join(" "),
}
diff --git a/frontend/src/components/DatabaseDetail/utils.ts b/frontend/src/components/DatabaseDetail/utils.ts
new file mode 100644
index 0000000000..04c1a5d2db
--- /dev/null
+++ b/frontend/src/components/DatabaseDetail/utils.ts
@@ -0,0 +1,12 @@
+import { DatabaseLabel, EnvironmentId, InstanceId, ProjectId } from "@/types";
+
+// MySQL only by now
+export type CreatePITRDatabaseContext = {
+ projectId: ProjectId;
+ environmentId: EnvironmentId;
+ instanceId: InstanceId;
+ databaseName: string;
+ characterSet: string;
+ collation: string;
+ labelList: DatabaseLabel[];
+};
diff --git a/frontend/src/components/InstanceSelect.vue b/frontend/src/components/InstanceSelect.vue
index 4cf0ab725e..0c41bdd487 100644
--- a/frontend/src/components/InstanceSelect.vue
+++ b/frontend/src/components/InstanceSelect.vue
@@ -15,7 +15,7 @@
<script lang="ts">
import { useInstanceStore } from "@/store";
-import { computed, defineComponent, reactive, watch } from "vue";
+import { computed, defineComponent, PropType, reactive, watch } from "vue";
import { Instance } from "../types";
interface LocalState {
@@ -34,6 +34,10 @@ export default defineComponent({
type: Number,
default: undefined,
},
+ filter: {
+ type: Function as PropType<(instance: Instance) => boolean>,
+ default: undefined,
+ },
disabled: {
default: false,
type: Boolean,
@@ -57,7 +61,7 @@ export default defineComponent({
});
const instanceList = computed(() => {
- return rawInstanceList.value.filter((instance) => {
+ const list = rawInstanceList.value.filter((instance) => {
if (instance.rowStatus === "NORMAL") {
return true;
}
@@ -67,6 +71,11 @@ export default defineComponent({
}
return false;
});
+
+ if (!props.filter) {
+ return list;
+ }
+ return list.filter(props.filter);
});
// The instance list might change if environmentId changes, and the previous selected id
diff --git a/frontend/src/components/Issue/IssueSidebar.vue b/frontend/src/components/Issue/IssueSidebar.vue
index 66422fe0b5..5caab918f6 100644
--- a/frontend/src/components/Issue/IssueSidebar.vue
+++ b/frontend/src/components/Issue/IssueSidebar.vue
@@ -296,7 +296,6 @@ import type {
Stage,
StageCreate,
Instance,
- TaskDatabaseCreatePayload,
DatabaseLabel,
Principal,
} from "@/types";
@@ -309,6 +308,7 @@ import {
hidePrefix,
taskSlug,
isOwnerOfProject,
+ extractDatabaseNameFromTask,
} from "@/utils";
import {
hasFeature,
@@ -397,23 +397,7 @@ const fieldValue = <T = string>(field: InputField): T => {
};
const databaseName = computed((): string | undefined => {
- if (props.database) {
- return props.database.name;
- }
-
- const stage = selectedStage.value as Stage;
- if (
- stage.taskList[0].type == "bb.task.database.create" ||
- stage.taskList[0].type == "bb.task.database.restore"
- ) {
- if (create.value) {
- const stage = selectedStage.value as StageCreate;
- return stage.taskList[0].databaseName;
- }
- return ((stage.taskList[0] as Task).payload as TaskDatabaseCreatePayload)
- .databaseName;
- }
- return undefined;
+ return extractDatabaseNameFromTask(selectedTask.value);
});
const environment = computed((): Environment => {
diff --git a/frontend/src/components/Issue/PipelinePITRFlow.vue b/frontend/src/components/Issue/PipelinePITRFlow.vue
index 06cb112b34..ffa8c65eb5 100644
--- a/frontend/src/components/Issue/PipelinePITRFlow.vue
+++ b/frontend/src/components/Issue/PipelinePITRFlow.vue
@@ -7,6 +7,7 @@
</PipelineStageList>
<div
+ v-if="taskList.length > 1"
class="task-list p-2 lg:flex lg:items-center relative space-y-2 lg:space-y-0"
>
<template v-for="(task, i) in taskList" :key="i">
@@ -27,7 +28,7 @@
v-if="isActiveTask(task)"
class="name w-5 h-5"
/>
- <div class="name">{{ databaseOfTask(task).name }}</div>
+ <div class="name">{{ databaseNameOfTask(task) }}</div>
</div>
<div
class="flex items-center px-1 py-1 whitespace-pre-wrap break-all"
@@ -51,15 +52,13 @@
<script lang="ts" setup>
import { computed, watchEffect } from "vue";
-import type {
- Pipeline,
- Stage,
- StageCreate,
- Task,
- TaskCreate,
- Database,
-} from "@/types";
-import { activeTask, activeTaskInStage, taskSlug } from "@/utils";
+import { Pipeline, Stage, StageCreate, Task, TaskCreate } from "@/types";
+import {
+ activeTask,
+ activeTaskInStage,
+ extractDatabaseNameFromTask,
+ taskSlug,
+} from "@/utils";
import TaskStatusIcon from "./TaskStatusIcon.vue";
import { useDatabaseStore } from "@/store";
import PipelineStageList from "./PipelineStageList.vue";
@@ -88,11 +87,8 @@ const taskList = computed(() => {
return selectedStage.value.taskList;
});
-const databaseOfTask = (task: Task | TaskCreate): Database => {
- if (create.value) {
- return databaseStore.getDatabaseById((task as TaskCreate).databaseId!);
- }
- return (task as Task).database!;
+const databaseNameOfTask = (task: Task | TaskCreate): string => {
+ return extractDatabaseNameFromTask(task);
};
const isSelectedTask = (task: Task | TaskCreate): boolean => {
diff --git a/frontend/src/components/Issue/logic/base.ts b/frontend/src/components/Issue/logic/base.ts
index 3126384b65..a533efd3e5 100644
--- a/frontend/src/components/Issue/logic/base.ts
+++ b/frontend/src/components/Issue/logic/base.ts
@@ -171,7 +171,11 @@ export const useBaseIssueLogic = (params: {
});
const isPITRMode = computed((): boolean => {
- return issue.value.type === "bb.issue.database.restore.pitr";
+ const { type } = issue.value;
+ return (
+ type === "bb.issue.database.restore.pitr" ||
+ type === "bb.issue.database.create"
+ );
});
const taskStatusOfStage = (stage: Stage | StageCreate) => {
diff --git a/frontend/src/components/Issue/logic/common.ts b/frontend/src/components/Issue/logic/common.ts
index 4a7cd0714f..ed843e6a15 100644
--- a/frontend/src/components/Issue/logic/common.ts
+++ b/frontend/src/components/Issue/logic/common.ts
@@ -87,6 +87,10 @@ export const useCommonLogic = () => {
const issueEntity = issue.value as Issue;
+ if (issueEntity.type === "bb.issue.database.restore.pitr") {
+ return false;
+ }
+
if (issueEntity.type === "bb.issue.database.create") {
// For standard mode projects, we are not allowed to edit the database
// creation SQL statement.
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vue
index fc730db1d0..2f26ca7fd8 100644
--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue
@@ -150,6 +150,10 @@ onMounted(async () => {
setPositionAtEndOfLine,
} = await useMonaco(language.value);
+ if (!editorContainerRef.value) {
+ return;
+ }
+
monacoInstanceRef.value = {
monaco,
dispose,
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 8a4ef87d98..2584f09b2f 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -846,9 +846,9 @@
"restore-to-point-in-time": "Restore to point in time",
"no-earlier-than": "Restore point-in-time cannot be earlier than [{earliest}] (the earliest available backup).",
"no-later-than-now": "Restore point-in-time cannot be later than now.",
+ "will-overwrite-current-database": "Will overwrite current database",
"restore-before-last-migration": "Restore to the point before last migration",
"restore-before-last-migration-help-info": "@:{'database.pitr.restore-before-last-migration'}. {link}.",
- "will-overwrite-current-database": "Will overwrite current database",
"restore-to": "Restore",
"restore-to-new-db": " To new database",
"restore-to-in-place": "In place"
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 61f0fb792d..689b3779e5 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -847,9 +847,9 @@
"restore-to-point-in-time": "恢复到指定时间点",
"no-earlier-than": "恢复的时间点不能早于 [{earliest}] (最早可用的备份)。",
"no-later-than-now": "恢复的时间点不能晚于当前时间。",
+ "will-overwrite-current-database": "会覆盖现有数据库",
"restore-before-last-migration": "恢复到上次变更历史之前",
"restore-before-last-migration-help-info": "@:{'database.pitr.restore-before-last-migration'}。{link}。",
- "will-overwrite-current-database": "会覆盖现有数据库",
"restore-to": "恢复到",
"restore-to-new-db": "新数据库",
"restore-to-in-place": "原数据库"
diff --git a/frontend/src/plugins/pitr/index.ts b/frontend/src/plugins/pitr/index.ts
index 8f469ed246..2d753ff5ad 100644
--- a/frontend/src/plugins/pitr/index.ts
+++ b/frontend/src/plugins/pitr/index.ts
@@ -1,14 +1,16 @@
import { computed, Ref, watch } from "vue";
import semverCompare from "semver/functions/compare";
import {
+ CreateDatabaseContext,
Database,
+ Instance,
IssueCreate,
MigrationHistory,
PITRContext,
- SYSTEM_BOT_ID,
} from "@/types";
import {
useBackupListByDatabaseId,
+ useCurrentUser,
useInstanceStore,
useIssueStore,
} from "@/store";
@@ -16,8 +18,17 @@ import { useI18n } from "vue-i18n";
export const MIN_PITR_SUPPORT_MYSQL_VERSION = "8.0.0";
+export const isPITRAvailableOnInstance = (instance: Instance): boolean => {
+ const { engine, engineVersion } = instance;
+ return (
+ engine === "MYSQL" &&
+ semverCompare(engineVersion, MIN_PITR_SUPPORT_MYSQL_VERSION) >= 0
+ );
+};
+
export const usePITRLogic = (database: Ref<Database>) => {
const { t } = useI18n();
+ const currentUser = useCurrentUser();
const instanceStore = useInstanceStore();
const backupList = useBackupListByDatabaseId(
@@ -80,18 +91,20 @@ export const usePITRLogic = (database: Ref<Database>) => {
const createPITRIssue = async (
pointTimeTs: number,
+ createDatabaseContext: CreateDatabaseContext | undefined = undefined,
params: Partial<IssueCreate> = {}
) => {
const issueStore = useIssueStore();
const createContext: PITRContext = {
databaseId: database.value.id,
pointInTimeTs: pointTimeTs,
+ createDatabaseContext,
};
const issueCreate: IssueCreate = {
name: `Restore database [${database.value.name}]`,
type: "bb.issue.database.restore.pitr",
description: "",
- assigneeId: SYSTEM_BOT_ID,
+ assigneeId: currentUser.value.id,
projectId: database.value.project.id,
payload: {},
createContext,
diff --git a/frontend/src/types/issue.ts b/frontend/src/types/issue.ts
index 4d63b166d7..f59672df50 100644
--- a/frontend/src/types/issue.ts
+++ b/frontend/src/types/issue.ts
@@ -39,8 +39,8 @@ export type CreateDatabaseContext = {
characterSet: string;
collation: string;
cluster: string;
- backupId: BackupId;
- backupName: string;
+ backupId?: BackupId;
+ backupName?: string;
labels?: string; // JSON encoded
};
@@ -70,6 +70,7 @@ export type PITRContext = {
databaseId: DatabaseId;
pointInTimeTs?: number; // UNIX timestamp
backupId?: BackupId;
+ createDatabaseContext?: CreateDatabaseContext;
};
// eslint-disable-next-line @typescript-eslint/ban-types
diff --git a/frontend/src/types/pipeline/task.ts b/frontend/src/types/pipeline/task.ts
index 37397e2ba1..012976ac6a 100644
--- a/frontend/src/types/pipeline/task.ts
+++ b/frontend/src/types/pipeline/task.ts
@@ -68,6 +68,8 @@ export type TaskDatabaseSchemaUpdateGhostCutoverPayload = {
export type TaskDatabasePITRRestorePayload = {
projectId: ProjectId;
pointInTimeTs: number; // UNIX timestamp
+ databaseName?: string; // used when PITR to new DB
+ targetInstanceId?: InstanceId; // used when PITR to new DB
};
export type TaskDatabasePITRCutoverPayload = {
diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts
index bb4824b039..4afb0d87ba 100644
--- a/frontend/src/utils/index.ts
+++ b/frontend/src/utils/index.ts
@@ -13,4 +13,5 @@ export * from "./types";
export * from "./util";
export * from "./label";
export * from "./deployment";
+export * from "./task";
export * from "./principal";
diff --git a/frontend/src/utils/task.ts b/frontend/src/utils/task.ts
new file mode 100644
index 0000000000..bc8705d1ec
--- /dev/null
+++ b/frontend/src/utils/task.ts
@@ -0,0 +1,55 @@
+import { useDatabaseStore } from "@/store";
+import {
+ Task,
+ TaskCreate,
+ TaskDatabaseCreatePayload,
+ TaskDatabasePITRRestorePayload,
+ unknown,
+} from "@/types";
+
+export const extractDatabaseNameFromTask = (
+ task: Task | TaskCreate
+): string => {
+ const taskEntity = task as Task;
+ const taskCreate = task as TaskCreate;
+ if (typeof taskEntity.id !== "number") {
+ // The task is not created yet
+ // Find the database by databaseId if possible
+ if (taskCreate.databaseId) {
+ return useDatabaseStore().getDatabaseById(taskCreate.databaseId!).name;
+ }
+ return unknown("DATABASE").name;
+ }
+
+ const taskType = taskEntity.type;
+ if (taskType === "bb.task.database.restore.pitr.restore") {
+ // When PITR to new DB, the database might not be created yet.
+ // So we need to extract the name from the task's payload。
+ const payload = taskEntity.payload as TaskDatabasePITRRestorePayload;
+ if (payload.databaseName) {
+ return payload.databaseName;
+ }
+
+ // When PITR in-place, taskEntity.database will be the database entity itself.
+ }
+
+ // The task entity is related to a database entity
+ if (taskEntity.database) {
+ return taskEntity.database.name;
+ }
+
+ // The task entity is irrelative to any databases or the related
+ // database is not created yet.
+ if (
+ taskType === "bb.task.database.create" ||
+ taskType === "bb.task.database.restore"
+ ) {
+ // The database is not created yet.
+ // extract database info from the task's and payload's properties.
+ const payload = taskEntity.payload as TaskDatabaseCreatePayload;
+ return payload.databaseName;
+ }
+
+ // Fallback to <<Unknown database>>. Won't be happy to see it.
+ return unknown("DATABASE").name;
+};