From 08b95c293c801da717f9b9daab8d256a9c092ff2 Mon Sep 17 00:00:00 2001 From: Mikhail Chusavitin Date: Thu, 5 Feb 2026 15:17:06 +0300 Subject: [PATCH] Purge orphan sync queue entries before push --- internal/localdb/localdb.go | 10 ++++++++++ internal/services/sync/service.go | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/internal/localdb/localdb.go b/internal/localdb/localdb.go index 3a36271..3bffdd2 100644 --- a/internal/localdb/localdb.go +++ b/internal/localdb/localdb.go @@ -474,6 +474,16 @@ func (l *LocalDB) MarkChangesSynced(ids []int64) error { return l.db.Where("id IN ?", ids).Delete(&PendingChange{}).Error } +// PurgeOrphanConfigurationPendingChanges removes configuration pending changes +// whose entity_uuid no longer exists in local_configurations. +func (l *LocalDB) PurgeOrphanConfigurationPendingChanges() (int64, error) { + tx := l.db.Where( + "entity_type = ? AND entity_uuid NOT IN (SELECT uuid FROM local_configurations)", + "configuration", + ).Delete(&PendingChange{}) + return tx.RowsAffected, tx.Error +} + // GetPendingCount returns the total number of pending changes (alias for CountPendingChanges) func (l *LocalDB) GetPendingCount() int64 { return l.CountPendingChanges() diff --git a/internal/services/sync/service.go b/internal/services/sync/service.go index b448a09..c2d983c 100644 --- a/internal/services/sync/service.go +++ b/internal/services/sync/service.go @@ -398,6 +398,13 @@ func (s *Service) SyncPricelistsIfNeeded() error { // PushPendingChanges pushes all pending changes to the server func (s *Service) PushPendingChanges() (int, error) { + removed, err := s.localDB.PurgeOrphanConfigurationPendingChanges() + if err != nil { + slog.Warn("failed to purge orphan configuration pending changes", "error", err) + } else if removed > 0 { + slog.Info("purged orphan configuration pending changes", "removed", removed) + } + changes, err := s.localDB.GetPendingChanges() if err != nil { return 0, fmt.Errorf("getting pending changes: %w", err)