From dc9ddae42efaa889cb04c07530958ed302757091 Mon Sep 17 00:00:00 2001 From: Rostislav Dugin Date: Tue, 30 Sep 2025 12:26:57 +0300 Subject: [PATCH] FIX (copying): Fix persisting same interval over copying --- backend/cmd/main.go | 5 --- .../internal/features/backups/config/model.go | 15 ++++++++ .../features/backups/config/service.go | 38 +++++++------------ backend/internal/features/intervals/model.go | 10 +++++ 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index aa749dc..ae100d5 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -20,7 +20,6 @@ import ( "postgresus-backend/internal/features/disk" healthcheck_attempt "postgresus-backend/internal/features/healthcheck/attempt" healthcheck_config "postgresus-backend/internal/features/healthcheck/config" - postgres_monitoring_collectors "postgresus-backend/internal/features/monitoring/postgres/collectors" postgres_monitoring_metrics "postgresus-backend/internal/features/monitoring/postgres/metrics" postgres_monitoring_settings "postgresus-backend/internal/features/monitoring/postgres/settings" "postgresus-backend/internal/features/notifiers" @@ -210,10 +209,6 @@ func runBackgroundTasks(log *slog.Logger) { go runWithPanicLogging(log, "postgres monitoring metrics background service", func() { postgres_monitoring_metrics.GetPostgresMonitoringMetricsBackgroundService().Run() }) - - go runWithPanicLogging(log, "postgres monitoring collectors background service", func() { - postgres_monitoring_collectors.GetDbMonitoringBackgroundService().Run() - }) } func runWithPanicLogging(log *slog.Logger, serviceName string, fn func()) { diff --git a/backend/internal/features/backups/config/model.go b/backend/internal/features/backups/config/model.go index 491526e..bf8c8e0 100644 --- a/backend/internal/features/backups/config/model.go +++ b/backend/internal/features/backups/config/model.go @@ -90,3 +90,18 @@ func (b *BackupConfig) Validate() error { return nil } + +func (b *BackupConfig) Copy(newDatabaseID uuid.UUID) *BackupConfig { + return &BackupConfig{ + DatabaseID: newDatabaseID, + IsBackupsEnabled: b.IsBackupsEnabled, + StorePeriod: b.StorePeriod, + BackupIntervalID: uuid.Nil, + BackupInterval: b.BackupInterval.Copy(), + StorageID: b.StorageID, + SendNotificationsOn: b.SendNotificationsOn, + IsRetryIfFailed: b.IsRetryIfFailed, + MaxFailedTriesCount: b.MaxFailedTriesCount, + CpuCount: b.CpuCount, + } +} diff --git a/backend/internal/features/backups/config/service.go b/backend/internal/features/backups/config/service.go index 8a1f69b..71197e4 100644 --- a/backend/internal/features/backups/config/service.go +++ b/backend/internal/features/backups/config/service.go @@ -130,6 +130,20 @@ func (s *BackupConfigService) GetBackupConfigsWithEnabledBackups() ([]*BackupCon return s.backupConfigRepository.GetWithEnabledBackups() } +func (s *BackupConfigService) OnDatabaseCopied(originalDatabaseID, newDatabaseID uuid.UUID) { + originalConfig, err := s.GetBackupConfigByDbId(originalDatabaseID) + if err != nil { + return + } + + newConfig := originalConfig.Copy(newDatabaseID) + + _, err = s.SaveBackupConfig(newConfig) + if err != nil { + return + } +} + func (s *BackupConfigService) initializeDefaultConfig( databaseID uuid.UUID, ) error { @@ -164,27 +178,3 @@ func storageIDsEqual(id1, id2 *uuid.UUID) bool { } return *id1 == *id2 } - -func (s *BackupConfigService) OnDatabaseCopied(originalDatabaseID, newDatabaseID uuid.UUID) { - originalConfig, err := s.GetBackupConfigByDbId(originalDatabaseID) - if err != nil { - return - } - - newConfig := &BackupConfig{ - DatabaseID: newDatabaseID, - IsBackupsEnabled: originalConfig.IsBackupsEnabled, - StorePeriod: originalConfig.StorePeriod, - BackupIntervalID: originalConfig.BackupIntervalID, - StorageID: originalConfig.StorageID, - SendNotificationsOn: originalConfig.SendNotificationsOn, - IsRetryIfFailed: originalConfig.IsRetryIfFailed, - MaxFailedTriesCount: originalConfig.MaxFailedTriesCount, - CpuCount: originalConfig.CpuCount, - } - - _, err = s.SaveBackupConfig(newConfig) - if err != nil { - return - } -} diff --git a/backend/internal/features/intervals/model.go b/backend/internal/features/intervals/model.go index 08c1135..91f2112 100644 --- a/backend/internal/features/intervals/model.go +++ b/backend/internal/features/intervals/model.go @@ -64,6 +64,16 @@ func (i *Interval) ShouldTriggerBackup(now time.Time, lastBackupTime *time.Time) } } +func (i *Interval) Copy() *Interval { + return &Interval{ + ID: uuid.Nil, + Interval: i.Interval, + TimeOfDay: i.TimeOfDay, + Weekday: i.Weekday, + DayOfMonth: i.DayOfMonth, + } +} + // daily trigger: honour the TimeOfDay slot and catch up the previous one func (i *Interval) shouldTriggerDaily(now, lastBackup time.Time) bool { if i.TimeOfDay == nil {