Skip to content

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

FIX (intervals): Merge: Validate time of day, weekday, and day of month

Rostislav Dugin72df61bb7486
diff --cc backend/internal/features/intervals/model.go
index 77c6d13e,f1d056af..1ecce9bb
--- a/backend/internal/features/intervals/model.go
+++ b/backend/internal/features/intervals/model.go
@@@ -27,27 -28,42 +28,42 @@@ func (i *Interval) BeforeSave(tx *gorm.
}
func (i *Interval) Validate() error {
-- // for daily, weekly and monthly intervals time of day is required
-- if (i.Interval == IntervalDaily || i.Interval == IntervalWeekly || i.Interval == IntervalMonthly) &&
-- i.TimeOfDay == nil {
-- return errors.New("time of day is required for daily, weekly and monthly intervals")
- }
++ if i.Interval == IntervalDaily || i.Interval == IntervalWeekly || i.Interval == IntervalMonthly {
++ if i.TimeOfDay == nil {
++ 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 {
++ if 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")
++ // 0 and 7 both mean Sunday — see shouldTriggerWeekly for the alias handling
++ if *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 {
++ if 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")
++ if *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 == "" {
return errors.New("cron expression is required for cron intervals")
}
++
if err := i.validateCronExpression(*i.CronExpression); err != nil {
return err
}
diff --cc backend/internal/features/intervals/model_test.go
index fdd00227,0bc8d21e..47a91c72
--- a/backend/internal/features/intervals/model_test.go
+++ b/backend/internal/features/intervals/model_test.go
@@@ -652,6 -653,32 +653,71 @@@ 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("Daily interval with empty time of day is invalid", func(t *testing.T) {
++ timeOfDay := ""
++ 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("Weekly interval with negative weekday is invalid", func(t *testing.T) {
++ timeOfDay := "09:00"
++ weekday := -1
++ 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("Weekly interval with weekday 7 (Sunday alias) is valid", func(t *testing.T) {
++ timeOfDay := "09:00"
++ weekday := 7
++ interval := &Interval{
++ ID: uuid.New(),
++ Interval: IntervalWeekly,
++ TimeOfDay: &timeOfDay,
++ Weekday: &weekday,
++ }
++ err := interval.Validate()
++ assert.NoError(t, err)
++ })
++
t.Run("Valid monthly interval", func(t *testing.T) {
timeOfDay := "09:00"
dayOfMonth := 15
@@@ -665,6 -692,20 +731,47 @@@
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("Monthly interval with day of month above 31 is invalid", func(t *testing.T) {
++ timeOfDay := "09:00"
++ dayOfMonth := 32
++ 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("Monthly interval with day of month 31 is valid", func(t *testing.T) {
++ timeOfDay := "09:00"
++ dayOfMonth := 31
++ interval := &Interval{
++ ID: uuid.New(),
++ Interval: IntervalMonthly,
++ TimeOfDay: &timeOfDay,
++ DayOfMonth: &dayOfMonth,
++ }
++ err := interval.Validate()
++ assert.NoError(t, err)
++ })
++
t.Run("Cron interval requires cron expression", func(t *testing.T) {
interval := &Interval{
ID: uuid.New(),