Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

fix: validate backup interval fields

immanuwellc258bfe4f28f
diff --git a/backend/internal/features/intervals/model.go b/backend/internal/features/intervals/model.go
index 77c6d13e..f1d056af 100644
--- a/backend/internal/features/intervals/model.go
+++ b/backend/internal/features/intervals/model.go
@@ -2,6 +2,7 @@ package intervals
import (
"errors"
+ "fmt"
"time"
"github.com/google/uuid"
@@ -33,16 +34,31 @@ func (i *Interval) Validate() error {
return errors.New("time of day is required for daily, weekly and monthly intervals")
}
+ if (i.Interval == IntervalDaily || i.Interval == IntervalWeekly || i.Interval == IntervalMonthly) &&
+ i.TimeOfDay != nil {
+ if _, err := time.Parse("15:04", *i.TimeOfDay); err != nil {
+ return fmt.Errorf("invalid time of day: %w", err)
+ }
+ }
+
// for weekly interval weekday is required
if i.Interval == IntervalWeekly && i.Weekday == nil {
return errors.New("weekday is required for weekly intervals")
}
+ if i.Interval == IntervalWeekly && i.Weekday != nil && (*i.Weekday < 0 || *i.Weekday > 7) {
+ return errors.New("weekday must be between 0 and 7")
+ }
+
// for monthly interval day of month is required
if i.Interval == IntervalMonthly && i.DayOfMonth == nil {
return errors.New("day of month is required for monthly intervals")
}
+ if i.Interval == IntervalMonthly && i.DayOfMonth != nil && (*i.DayOfMonth < 1 || *i.DayOfMonth > 31) {
+ return errors.New("day of month must be between 1 and 31")
+ }
+
// for cron interval cron expression is required and must be valid
if i.Interval == IntervalCron {
if i.CronExpression == nil || *i.CronExpression == "" {
diff --git a/backend/internal/features/intervals/model_test.go b/backend/internal/features/intervals/model_test.go
index fdd00227..0bc8d21e 100644
--- a/backend/internal/features/intervals/model_test.go
+++ b/backend/internal/features/intervals/model_test.go
@@ -6,6 +6,7 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestInterval_ShouldTriggerBackup_Hourly(t *testing.T) {
@@ -652,6 +653,32 @@ func TestInterval_Validate(t *testing.T) {
assert.NoError(t, err)
})
+ t.Run("Daily interval with invalid time of day is invalid", func(t *testing.T) {
+ timeOfDay := "25:00"
+ interval := &Interval{
+ ID: uuid.New(),
+ Interval: IntervalDaily,
+ TimeOfDay: &timeOfDay,
+ }
+ err := interval.Validate()
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "invalid time of day")
+ })
+
+ t.Run("Weekly interval with invalid weekday is invalid", func(t *testing.T) {
+ timeOfDay := "09:00"
+ weekday := 8
+ interval := &Interval{
+ ID: uuid.New(),
+ Interval: IntervalWeekly,
+ TimeOfDay: &timeOfDay,
+ Weekday: &weekday,
+ }
+ err := interval.Validate()
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "weekday must be between 0 and 7")
+ })
+
t.Run("Valid monthly interval", func(t *testing.T) {
timeOfDay := "09:00"
dayOfMonth := 15
@@ -665,6 +692,20 @@ func TestInterval_Validate(t *testing.T) {
assert.NoError(t, err)
})
+ t.Run("Monthly interval with invalid day of month is invalid", func(t *testing.T) {
+ timeOfDay := "09:00"
+ dayOfMonth := 0
+ interval := &Interval{
+ ID: uuid.New(),
+ Interval: IntervalMonthly,
+ TimeOfDay: &timeOfDay,
+ DayOfMonth: &dayOfMonth,
+ }
+ err := interval.Validate()
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "day of month must be between 1 and 31")
+ })
+
t.Run("Cron interval requires cron expression", func(t *testing.T) {
interval := &Interval{
ID: uuid.New(),