photoprism/internal/entity/entity_save.go
Michael Mayer a2b7615c93 Go: Apply go fix modernizations across backend packages
Run `go fix ./...` and keep mechanical modernization updates.

- Replace `interface{}` with `any` in signatures and local types
- Apply formatter/style cleanups from go1.26 tooling
- Keep `omitempty` behavior-preserving simplifications suggested by fix
- No functional feature changes intended

Validation:
- go test ./... -run '^$' -count=1 (Go 1.26.0)
- GOTOOLCHAIN=go1.24.10 go test ./... -run '^$' -count=1

Signed-off-by: Michael Mayer <michael@photoprism.app>
2026-02-20 03:54:33 +01:00

34 lines
736 B
Go

package entity
import (
"fmt"
"strings"
)
// Save tries to update an existing record and falls back to insert semantics, retrying on lock errors.
func Save(m any, keyNames ...string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("save: %s (panic)", r)
}
}()
// Try a regular update first.
if err = Update(m, keyNames...); err == nil {
return nil
}
// Automatically insert/update record as needed.
if err = UnscopedDb().Save(m).Error; err == nil {
return nil
}
// Try again if database was locked, return otherwise.
if !strings.Contains(strings.ToLower(err.Error()), "lock") {
return err
} else if err = UnscopedDb().Save(m).Error; err == nil {
return nil
}
return err
}