mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
refactor(backups): use upsert instead of create/update split
This commit is contained in:
@@ -29,6 +29,7 @@ import {
|
||||
getBackupSchedule,
|
||||
updateBackupSchedule,
|
||||
getBackupScheduleForVolume,
|
||||
upsertBackupSchedule,
|
||||
runBackupNow,
|
||||
} from "../sdk.gen";
|
||||
import { queryOptions, type UseMutationOptions, type DefaultError } from "@tanstack/react-query";
|
||||
@@ -75,6 +76,8 @@ import type {
|
||||
UpdateBackupScheduleData,
|
||||
UpdateBackupScheduleResponse,
|
||||
GetBackupScheduleForVolumeData,
|
||||
UpsertBackupScheduleData,
|
||||
UpsertBackupScheduleResponse,
|
||||
RunBackupNowData,
|
||||
RunBackupNowResponse,
|
||||
} from "../types.gen";
|
||||
@@ -865,6 +868,29 @@ export const getBackupScheduleForVolumeOptions = (options: Options<GetBackupSche
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Create or update a backup schedule for a volume
|
||||
*/
|
||||
export const upsertBackupScheduleMutation = (
|
||||
options?: Partial<Options<UpsertBackupScheduleData>>,
|
||||
): UseMutationOptions<UpsertBackupScheduleResponse, DefaultError, Options<UpsertBackupScheduleData>> => {
|
||||
const mutationOptions: UseMutationOptions<
|
||||
UpsertBackupScheduleResponse,
|
||||
DefaultError,
|
||||
Options<UpsertBackupScheduleData>
|
||||
> = {
|
||||
mutationFn: async (localOptions) => {
|
||||
const { data } = await upsertBackupSchedule({
|
||||
...options,
|
||||
...localOptions,
|
||||
throwOnError: true,
|
||||
});
|
||||
return data;
|
||||
},
|
||||
};
|
||||
return mutationOptions;
|
||||
};
|
||||
|
||||
export const runBackupNowQueryKey = (options: Options<RunBackupNowData>) => createQueryKey("runBackupNow", options);
|
||||
|
||||
/**
|
||||
|
||||
@@ -62,6 +62,8 @@ import type {
|
||||
UpdateBackupScheduleResponses,
|
||||
GetBackupScheduleForVolumeData,
|
||||
GetBackupScheduleForVolumeResponses,
|
||||
UpsertBackupScheduleData,
|
||||
UpsertBackupScheduleResponses,
|
||||
RunBackupNowData,
|
||||
RunBackupNowResponses,
|
||||
} from "./types.gen";
|
||||
@@ -426,6 +428,22 @@ export const getBackupScheduleForVolume = <ThrowOnError extends boolean = false>
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Create or update a backup schedule for a volume
|
||||
*/
|
||||
export const upsertBackupSchedule = <ThrowOnError extends boolean = false>(
|
||||
options?: Options<UpsertBackupScheduleData, ThrowOnError>,
|
||||
) => {
|
||||
return (options?.client ?? _heyApiClient).put<UpsertBackupScheduleResponses, unknown, ThrowOnError>({
|
||||
url: "/api/v1/backups/upsert",
|
||||
...options,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...options?.headers,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Trigger a backup immediately for a schedule
|
||||
*/
|
||||
|
||||
@@ -1063,6 +1063,62 @@ export type GetBackupScheduleForVolumeResponses = {
|
||||
export type GetBackupScheduleForVolumeResponse =
|
||||
GetBackupScheduleForVolumeResponses[keyof GetBackupScheduleForVolumeResponses];
|
||||
|
||||
export type UpsertBackupScheduleData = {
|
||||
body?: {
|
||||
cronExpression: string;
|
||||
enabled: boolean;
|
||||
repositoryId: string;
|
||||
volumeId: number;
|
||||
excludePatterns?: Array<string>;
|
||||
includePatterns?: Array<string>;
|
||||
retentionPolicy?: {
|
||||
keepDaily?: number;
|
||||
keepHourly?: number;
|
||||
keepLast?: number;
|
||||
keepMonthly?: number;
|
||||
keepWeekly?: number;
|
||||
keepWithinDuration?: string;
|
||||
keepYearly?: number;
|
||||
};
|
||||
tags?: Array<string>;
|
||||
};
|
||||
path?: never;
|
||||
query?: never;
|
||||
url: "/api/v1/backups/upsert";
|
||||
};
|
||||
|
||||
export type UpsertBackupScheduleResponses = {
|
||||
/**
|
||||
* Backup schedule upserted successfully
|
||||
*/
|
||||
200: {
|
||||
createdAt: number;
|
||||
cronExpression: string;
|
||||
enabled: boolean;
|
||||
excludePatterns: Array<string> | null;
|
||||
id: number;
|
||||
includePatterns: Array<string> | null;
|
||||
lastBackupAt: number | null;
|
||||
lastBackupError: string | null;
|
||||
lastBackupStatus: "error" | "success" | null;
|
||||
nextBackupAt: number | null;
|
||||
repositoryId: string;
|
||||
retentionPolicy: {
|
||||
keepDaily?: number;
|
||||
keepHourly?: number;
|
||||
keepLast?: number;
|
||||
keepMonthly?: number;
|
||||
keepWeekly?: number;
|
||||
keepWithinDuration?: string;
|
||||
keepYearly?: number;
|
||||
} | null;
|
||||
updatedAt: number;
|
||||
volumeId: number;
|
||||
};
|
||||
};
|
||||
|
||||
export type UpsertBackupScheduleResponse = UpsertBackupScheduleResponses[keyof UpsertBackupScheduleResponses];
|
||||
|
||||
export type RunBackupNowData = {
|
||||
body?: never;
|
||||
path: {
|
||||
|
||||
@@ -9,8 +9,7 @@ import { OnOff } from "~/components/onoff";
|
||||
import type { Volume } from "~/lib/types";
|
||||
import {
|
||||
listRepositoriesOptions,
|
||||
createBackupScheduleMutation,
|
||||
updateBackupScheduleMutation,
|
||||
upsertBackupScheduleMutation,
|
||||
getBackupScheduleForVolumeOptions,
|
||||
} from "~/api-client/@tanstack/react-query.gen";
|
||||
import { parseError } from "~/lib/errors";
|
||||
@@ -76,27 +75,15 @@ export const VolumeBackupsTabContent = ({ volume }: Props) => {
|
||||
};
|
||||
}, [existingSchedule, selectedRepository, volume.name]);
|
||||
|
||||
const createSchedule = useMutation({
|
||||
...createBackupScheduleMutation(),
|
||||
const upsertSchedule = useMutation({
|
||||
...upsertBackupScheduleMutation(),
|
||||
onSuccess: () => {
|
||||
toast.success("Backup schedule created successfully");
|
||||
toast.success("Backup schedule saved successfully");
|
||||
queryClient.invalidateQueries({ queryKey: ["listBackupSchedules"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["getBackupScheduleForVolume", volume.id.toString()] });
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error("Failed to create backup schedule", {
|
||||
description: parseError(error)?.message,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const updateSchedule = useMutation({
|
||||
...updateBackupScheduleMutation(),
|
||||
onSuccess: () => {
|
||||
toast.success("Backup schedule updated successfully");
|
||||
queryClient.invalidateQueries({ queryKey: ["listBackupSchedules"] });
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error("Failed to update backup schedule", {
|
||||
toast.error("Failed to save backup schedule", {
|
||||
description: parseError(error)?.message,
|
||||
});
|
||||
},
|
||||
@@ -113,27 +100,15 @@ export const VolumeBackupsTabContent = ({ volume }: Props) => {
|
||||
if (formValues.keepMonthly) retentionPolicy.keepMonthly = formValues.keepMonthly;
|
||||
if (formValues.keepYearly) retentionPolicy.keepYearly = formValues.keepYearly;
|
||||
|
||||
if (existingSchedule) {
|
||||
updateSchedule.mutate({
|
||||
path: { scheduleId: existingSchedule.id.toString() },
|
||||
body: {
|
||||
repositoryId: formValues.repositoryId,
|
||||
enabled: isEnabled,
|
||||
cronExpression,
|
||||
retentionPolicy: Object.keys(retentionPolicy).length > 0 ? retentionPolicy : undefined,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
createSchedule.mutate({
|
||||
body: {
|
||||
volumeId: volume.id,
|
||||
repositoryId: formValues.repositoryId,
|
||||
enabled: true,
|
||||
cronExpression,
|
||||
retentionPolicy: Object.keys(retentionPolicy).length > 0 ? retentionPolicy : undefined,
|
||||
},
|
||||
});
|
||||
}
|
||||
upsertSchedule.mutate({
|
||||
body: {
|
||||
volumeId: volume.id,
|
||||
repositoryId: formValues.repositoryId,
|
||||
enabled: existingSchedule ? isEnabled : true,
|
||||
cronExpression,
|
||||
retentionPolicy: Object.keys(retentionPolicy).length > 0 ? retentionPolicy : undefined,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
if (loadingRepositories || loadingSchedules) {
|
||||
@@ -180,9 +155,9 @@ export const VolumeBackupsTabContent = ({ volume }: Props) => {
|
||||
if (!existingSchedule) return;
|
||||
|
||||
setIsEnabled(enabled);
|
||||
updateSchedule.mutate({
|
||||
path: { scheduleId: existingSchedule.id.toString() },
|
||||
upsertSchedule.mutate({
|
||||
body: {
|
||||
volumeId: existingSchedule.volumeId,
|
||||
repositoryId: existingSchedule.repositoryId,
|
||||
enabled,
|
||||
cronExpression: existingSchedule.cronExpression,
|
||||
|
||||
Reference in New Issue
Block a user