Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(frontend): in-place database restore for PostgreSQL (#2275)
Liu Ji4c28f478a139
diff --git a/frontend/src/components/BackupTable.vue b/frontend/src/components/BackupTable.vueindex b23240dc09..c69f877e4a 100644--- a/frontend/src/components/BackupTable.vue+++ b/frontend/src/components/BackupTable.vue@@ -85,12 +85,7 @@<button:disabled="!allowRestore(backup)"class="normal-link disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:no-underline"- @click.stop="- () => {- state.restoredBackup = backup;- state.showRestoreBackupModal = true;- }- "+ @click.stop="showRestoreDialog(backup)">{{ $t("database.restore") }}</button>@@ -98,50 +93,104 @@</template></BBTable><BBModal- v-if="state.showRestoreBackupModal"- :title="$t('database.restore-backup', [state.restoredBackup.name])"- @close="- () => {- state.showRestoreBackupModal = false;- state.restoredBackup = undefined;- }- "+ v-if="state.restoreBackupContext"+ :title="$t('database.restore-database')"+ @close="state.restoreBackupContext = undefined">- <!-- eslint-disable vue/attribute-hyphenation -->- <CreateDatabasePrepForm- :projectId="database.project.id"- :environmentId="database.instance.environment.id"- :instanceId="database.instance.id"- :backup="state.restoredBackup"- @dismiss="- () => {- state.showRestoreBackupModal = false;- state.restoredBackup = undefined;- }- "- />+ <div class="space-y-4">+ <RestoreTargetForm+ v-if="allowRestoreInPlace"+ :target="state.restoreBackupContext.target"+ @change="state.restoreBackupContext!.target = $event"+ />++ <CreateDatabasePrepForm+ v-if="state.restoreBackupContext.target === 'NEW'"+ :project-id="database.project.id"+ :environment-id="database.instance.environment.id"+ :instance-id="database.instance.id"+ :backup="state.restoreBackupContext.backup"+ @dismiss="state.restoreBackupContext = undefined"+ />+ </div>++ <div+ v-if="state.restoreBackupContext.target === 'IN-PLACE'"+ class="w-full pt-6 mt-4 flex justify-end gap-x-3 border-t border-block-border"+ >+ <button+ type="button"+ class="btn-normal py-2 px-4"+ @click="state.restoreBackupContext = undefined"+ >+ {{ $t("common.cancel") }}+ </button>++ <button+ type="button"+ class="btn-primary py-2 px-4"+ @click="doRestoreInPlace"+ >+ {{ $t("common.confirm") }}+ </button>+ </div>++ <div+ v-if="state.creatingRestoreIssue"+ class="absolute inset-0 z-10 bg-white/70 flex items-center justify-center"+ >+ <BBSpin />+ </div></BBModal>++ <FeatureModal+ v-if="state.showFeatureModal"+ feature="bb.feature.disaster-recovery-pitr"+ @cancel="state.showFeatureModal = false"+ /></template><script lang="ts">import { computed, defineComponent, PropType, reactive } from "vue";-import { BBTableColumn, BBTableSectionDataSource } from "../bbkit/types";-import { Backup, Database, MigrationHistory } from "../types";-import { bytesToString, databaseSlug, migrationHistorySlug } from "../utils";-import CreateDatabasePrepForm from "../components/CreateDatabasePrepForm.vue";import { useRouter } from "vue-router";import { useI18n } from "vue-i18n";-import { useInstanceStore } from "@/store";+import { BBTableColumn, BBTableSectionDataSource } from "../bbkit/types";+import {+ Backup,+ Database,+ IssueCreate,+ MigrationHistory,+ PITRContext,+ SYSTEM_BOT_ID,+} from "../types";+import {+ bytesToString,+ databaseSlug,+ issueSlug,+ migrationHistorySlug,+} from "../utils";+import { featureToRef, useInstanceStore, useIssueStore } from "@/store";+import CreateDatabasePrepForm from "../components/CreateDatabasePrepForm.vue";+import {+ default as RestoreTargetForm,+ RestoreTarget,+} from "../components/DatabaseBackup/RestoreTargetForm.vue";++type RestoreBackupContext = {+ target: RestoreTarget;+ backup: Backup;+};interface LocalState {- showRestoreBackupModal: boolean;- restoredBackup?: Backup;+ restoreBackupContext?: RestoreBackupContext;loadingMigrationHistory: boolean;+ creatingRestoreIssue: boolean;+ showFeatureModal: boolean;}export default defineComponent({name: "BackupTable",- components: { CreateDatabasePrepForm },+ components: { RestoreTargetForm, CreateDatabasePrepForm },props: {database: {required: true,@@ -161,10 +210,18 @@ export default defineComponent({const { t } = useI18n();const state = reactive<LocalState>({- showRestoreBackupModal: false,+ restoreBackupContext: undefined,loadingMigrationHistory: false,+ creatingRestoreIssue: false,+ showFeatureModal: false,+ });++ const allowRestoreInPlace = computed((): boolean => {+ return props.database.instance.engine === "POSTGRES";});+ const hasPITRFeature = featureToRef("bb.feature.disaster-recovery-pitr");+const EDIT_COLUMN_LIST: BBTableColumn[] = [{title: t("common.status"),@@ -298,6 +355,60 @@ export default defineComponent({return backup.status === "DONE";};+ const showRestoreDialog = (backup: Backup) => {+ state.restoreBackupContext = {+ target: "NEW",+ backup,+ };+ };++ const doRestoreInPlace = async () => {+ const { restoreBackupContext } = state;+ if (!restoreBackupContext) {+ return;+ }++ if (!hasPITRFeature.value) {+ state.showFeatureModal = true;+ return;+ }++ state.creatingRestoreIssue = true;++ try {+ const { backup } = restoreBackupContext;+ const { database } = props;+ const issueNameParts: string[] = [+ `Restore database [${database.name}]`,+ `to backup snapshot [${restoreBackupContext.backup.name}]`,+ ];++ const issueStore = useIssueStore();+ const createContext: PITRContext = {+ databaseId: database.id,+ backupId: backup.id,+ };+ const issueCreate: IssueCreate = {+ name: issueNameParts.join(" "),+ type: "bb.issue.database.restore.pitr",+ description: "",+ assigneeId: SYSTEM_BOT_ID,+ projectId: database.project.id,+ payload: {},+ createContext,+ };++ await issueStore.validateIssue(issueCreate);++ const issue = await issueStore.createIssue(issueCreate);++ const slug = issueSlug(issue.name, issue.id);+ router.push(`/issue/${slug}`);+ } catch {+ state.creatingRestoreIssue = false;+ }+ };+return {state,gotoMigrationHistory,@@ -306,6 +417,9 @@ export default defineComponent({backupSectionList,statusIconClass,allowRestore,+ allowRestoreInPlace,+ showRestoreDialog,+ doRestoreInPlace,};},});diff --git a/frontend/src/components/CreateDatabasePrepForm.vue b/frontend/src/components/CreateDatabasePrepForm.vueindex 439e7501c9..9e0317d5fb 100644--- a/frontend/src/components/CreateDatabasePrepForm.vue+++ b/frontend/src/components/CreateDatabasePrepForm.vue@@ -234,6 +234,12 @@feature="bb.feature.multi-tenancy"@cancel="state.showFeatureModal = false"/>+ <div+ v-if="state.creating"+ class="absolute inset-0 z-10 bg-white/70 flex items-center justify-center"+ >+ <BBSpin />+ </div></template><script lang="ts">@@ -298,6 +304,7 @@ interface LocalState {cluster: string;assigneeId?: PrincipalId;showFeatureModal: boolean;+ creating: boolean;}export default defineComponent({@@ -367,6 +374,7 @@ export default defineComponent({cluster: "",assigneeId: showAssigneeSelect.value ? undefined : SYSTEM_BOT_ID,showFeatureModal: false,+ creating: false,});const project = computed((): Project => {@@ -556,13 +564,20 @@ export default defineComponent({// Do not submit non-selected optional labelsconst labelList = state.labelList.filter((label) => !!label.value);context.labels = JSON.stringify(labelList);++ state.creating = true;useIssueStore().createIssue(newIssue)- .then((createdIssue) => {- router.push(- `/issue/${issueSlug(createdIssue.name, createdIssue.id)}`- );- });+ .then(+ (createdIssue) => {+ router.push(+ `/issue/${issueSlug(createdIssue.name, createdIssue.id)}`+ );+ },+ () => {+ state.creating = false;+ }+ );};// update `state.labelList` when selected Environment changeddiff --git a/frontend/src/components/DatabaseBackup/RestoreTargetForm.vue b/frontend/src/components/DatabaseBackup/RestoreTargetForm.vuenew file mode 100644index 0000000000..b529fe48b4--- /dev/null+++ b/frontend/src/components/DatabaseBackup/RestoreTargetForm.vue@@ -0,0 +1,77 @@+<template>+ <div class="mx-4 space-y-6 divide-y divide-block-border">+ <div class="grid gap-y-6 gap-x-4 grid-cols-4">+ <div class="col-span-2 col-start-2 w-64">+ <label class="textlabel">+ {{ $t("database.pitr.restore-to") }}+ </label>+ <div class="flex items-center gap-6 textlabel py-1">+ <label class="flex items-center gap-2">+ <input+ type="radio"+ :checked="state.target === 'NEW'"+ @input="$emit('change', 'NEW')"+ />+ <span>{{ $t("database.pitr.restore-to-new-db") }}</span>+ </label>+ <label class="flex items-center">+ <input+ type="radio"+ :checked="state.target === 'IN-PLACE'"+ @input="$emit('change', 'IN-PLACE')"+ />+ <span class="ml-2 flex items-center">+ {{ $t("database.pitr.restore-to-in-place") }}++ <FeatureBadge+ feature="bb.feature.disaster-recovery-pitr"+ class="text-accent ml-1"+ />+ </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>+ </div>+ </div>+</template>++<script lang="ts" setup>+import { PropType, reactive, watch } from "vue";++export type RestoreTarget = "IN-PLACE" | "NEW";++type LocalState = {+ target: RestoreTarget;+};++const props = defineProps({+ target: {+ type: String as PropType<RestoreTarget>,+ required: true,+ },+});++defineEmits<{+ (event: "change", target: RestoreTarget): void;+}>();++const state = reactive<LocalState>({+ target: props.target,+});++watch(+ () => props.target,+ (target) => {+ state.target = target;+ }+);+</script>diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 833dfda8e5..06a633c586 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -846,7 +846,11 @@"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.","restore-before-last-migration": "Restore to the point before last migration",- "restore-before-last-migration-help-info": "@:{'database.pitr.restore-before-last-migration'}. {link}."+ "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"},"show-reserved-tables": "Show Bytebase reserved tables","show-reserved-databases": "Show Bytebase reserved databases",@@ -867,7 +871,8 @@"synced-at": "Synced at {time}","not-found-last-successful-sync-was": "Not found, last successful sync was {time}","view-unassigned-databases": "View unassigned databases",- "unassigned-databases": "Unassigned databases"+ "unassigned-databases": "Unassigned databases",+ "restore-database": "Restore database"},"repository": {"branch-observe-file-change": "The branch where Bytebase observes the file change.",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex d10ce2d70e..3bd1f7d9a6 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -847,7 +847,11 @@"no-earlier-than": "恢复的时间点不能早于 [{earliest}] (最早可用的备份)。","no-later-than-now": "恢复的时间点不能晚于当前时间。","restore-before-last-migration": "恢复到上次变更历史之前",- "restore-before-last-migration-help-info": "@:{'database.pitr.restore-before-last-migration'}。{link}。"+ "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": "原数据库"},"show-reserved-tables": "显示 Bytebase 保留的表","show-reserved-databases": "显示 Bytebase 保留的数据库",@@ -868,7 +872,8 @@"synced-at": "同步于 {time}","not-found-last-successful-sync-was": "未找到,上次成功同步于 {time}","view-unassigned-databases": "查看未分配的数据库",- "unassigned-databases": "未分配的数据库"+ "unassigned-databases": "未分配的数据库",+ "restore-database": "恢复数据库"},"repository": {"branch-observe-file-change": "Bytebase 跟踪文件变更的分支。",diff --git a/frontend/src/types/issue.ts b/frontend/src/types/issue.tsindex 498e7d3a25..4d63b166d7 100644--- a/frontend/src/types/issue.ts+++ b/frontend/src/types/issue.ts@@ -68,7 +68,8 @@ export type UpdateSchemaGhostContext = {export type PITRContext = {databaseId: DatabaseId;- pointInTimeTs: number; // UNIX timestamp+ pointInTimeTs?: number; // UNIX timestamp+ backupId?: BackupId;};// eslint-disable-next-line @typescript-eslint/ban-types