Merge pull request #34 from fedor-git/feature/sync-db

fix: multiply node run db issue
This commit is contained in:
Fedor Sorokin 2026-04-21 14:08:28 +02:00 committed by GitHub
commit 25ebe49726
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 4 deletions

View File

@ -575,7 +575,7 @@ func (r *SqlRepo) getOrCreateInterface(
Identifier: id,
}
err := tx.Attrs(interfaceDefaults).FirstOrCreate(&in, id).Error
err := tx.Preload("Addresses").Attrs(interfaceDefaults).FirstOrCreate(&in, id).Error
if err != nil {
return nil, err
}
@ -592,9 +592,13 @@ func (r *SqlRepo) upsertInterface(ui *domain.ContextUserInfo, tx *gorm.DB, in *d
return err
}
err = tx.Model(in).Association("Addresses").Replace(in.Addresses)
if err != nil {
return fmt.Errorf("failed to update interface addresses: %w", err)
// Only update addresses if they were explicitly set (not nil)
// This prevents accidentally deleting all addresses when loading interface without preload
if in.Addresses != nil {
err = tx.Model(in).Association("Addresses").Replace(in.Addresses)
if err != nil {
return fmt.Errorf("failed to update interface addresses: %w", err)
}
}
return nil