feat(backup): add pre-migration backup, periodic backup, backfill warning, and backup management UI

Four improvements to the database backup mechanism:

1. Auto backup before schema migration - creates a snapshot when
   upgrading from an older database version, providing a safety net
   beyond the existing SAVEPOINT rollback mechanism.

2. Periodic startup backup - checks on app launch whether the latest
   backup is older than 24 hours and creates a new one if needed,
   ensuring all users have recent backups regardless of usage patterns.

3. Backfill failure notification - switch now returns SwitchResult with
   warnings instead of silently ignoring backfill errors, so users are
   informed when their manual config changes may not have been saved.

4. Backup management UI - new BackupListSection in Settings > Data
   Management showing all backup snapshots with restore capability,
   including a confirmation dialog and automatic safety backup before
   restore.
This commit is contained in:
Jason
2026-02-21 23:56:56 +08:00
parent 5ebc879f09
commit 3afec8a10f
18 changed files with 490 additions and 24 deletions
+32
View File
@@ -0,0 +1,32 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { backupsApi } from "@/lib/api";
export function useBackupManager() {
const queryClient = useQueryClient();
const {
data: backups = [],
isLoading,
refetch,
} = useQuery({
queryKey: ["db-backups"],
queryFn: () => backupsApi.listDbBackups(),
});
const restoreMutation = useMutation({
mutationFn: (filename: string) => backupsApi.restoreDbBackup(filename),
onSuccess: async () => {
// Invalidate all queries to refresh data from restored database
await queryClient.invalidateQueries();
// Refetch backup list
await refetch();
},
});
return {
backups,
isLoading,
restore: restoreMutation.mutateAsync,
isRestoring: restoreMutation.isPending,
};
}
+12 -1
View File
@@ -134,9 +134,20 @@ export function useProviderActions(activeApp: AppId) {
const switchProvider = useCallback(
async (provider: Provider) => {
try {
await switchProviderMutation.mutateAsync(provider.id);
const result = await switchProviderMutation.mutateAsync(provider.id);
await syncClaudePlugin(provider);
// Show backfill warning if present
if (result?.warnings?.length) {
toast.warning(
t("notifications.backfillWarning", {
defaultValue:
"切换成功,但旧供应商配置回填失败,您手动修改的配置可能未保存",
}),
{ duration: 5000 },
);
}
// 根据供应商类型显示不同的成功提示
if (
activeApp === "claude" &&