sync: clean stale local pricelists and migrate runtime config handling

This commit is contained in:
2026-02-08 10:01:27 +03:00
parent eb8555c11a
commit 593280de99
12 changed files with 434 additions and 426 deletions

View File

@@ -719,6 +719,47 @@ func (l *LocalDB) DeleteLocalPricelist(id uint) error {
return l.db.Delete(&LocalPricelist{}, id).Error
}
// DeleteUnusedLocalPricelistsMissingOnServer removes local pricelists that are absent on server
// and not referenced by active local configurations.
func (l *LocalDB) DeleteUnusedLocalPricelistsMissingOnServer(serverPricelistIDs []uint) (int, error) {
returned := 0
err := l.db.Transaction(func(tx *gorm.DB) error {
var candidates []LocalPricelist
query := tx.Model(&LocalPricelist{})
if len(serverPricelistIDs) > 0 {
query = query.Where("server_id NOT IN ?", serverPricelistIDs)
}
if err := query.Find(&candidates).Error; err != nil {
return err
}
for i := range candidates {
pl := candidates[i]
var refs int64
if err := tx.Model(&LocalConfiguration{}).
Where("pricelist_id = ? AND is_active = 1", pl.ServerID).
Count(&refs).Error; err != nil {
return err
}
if refs > 0 {
continue
}
if err := tx.Where("pricelist_id = ?", pl.ID).Delete(&LocalPricelistItem{}).Error; err != nil {
return err
}
if err := tx.Delete(&LocalPricelist{}, pl.ID).Error; err != nil {
return err
}
returned++
}
return nil
})
if err != nil {
return 0, err
}
return returned, nil
}
// PendingChange methods
// AddPendingChange adds a change to the sync queue