sync: recover missing server config during update push
This commit is contained in:
@@ -685,17 +685,36 @@ func (s *Service) pushConfigurationUpdate(change *localdb.PendingChange) error {
|
||||
}
|
||||
|
||||
if localCfg.ServerID == nil {
|
||||
// Configuration hasn't been synced yet, try to find it on server by UUID
|
||||
serverCfg, err := configRepo.GetByUUID(cfg.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("configuration not yet synced to server: %w", err)
|
||||
// Configuration hasn't been synced yet, try to find it on server by UUID.
|
||||
// If not found (e.g. stale create was skipped), create it from current snapshot.
|
||||
serverCfg, getErr := configRepo.GetByUUID(cfg.UUID)
|
||||
if getErr != nil {
|
||||
if !errors.Is(getErr, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("loading configuration from server: %w", getErr)
|
||||
}
|
||||
if createErr := configRepo.Create(&cfg); createErr != nil {
|
||||
// Idempotency fallback: configuration may have been created concurrently.
|
||||
existing, existingErr := configRepo.GetByUUID(cfg.UUID)
|
||||
if existingErr != nil {
|
||||
return fmt.Errorf("creating missing configuration on server: %w", createErr)
|
||||
}
|
||||
cfg.ID = existing.ID
|
||||
}
|
||||
if cfg.ID == 0 {
|
||||
existing, existingErr := configRepo.GetByUUID(cfg.UUID)
|
||||
if existingErr != nil {
|
||||
return fmt.Errorf("loading created configuration from server: %w", existingErr)
|
||||
}
|
||||
cfg.ID = existing.ID
|
||||
}
|
||||
} else {
|
||||
cfg.ID = serverCfg.ID
|
||||
}
|
||||
cfg.ID = serverCfg.ID
|
||||
|
||||
// Update local with server ID
|
||||
serverID := serverCfg.ID
|
||||
localCfg.ServerID = &serverID
|
||||
s.localDB.SaveConfiguration(localCfg)
|
||||
// Update local with server ID
|
||||
serverID := cfg.ID
|
||||
localCfg.ServerID = &serverID
|
||||
s.localDB.SaveConfiguration(localCfg)
|
||||
} else {
|
||||
cfg.ID = *localCfg.ServerID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user