1
0
Fork 0
mirror of https://github.com/Luzifer/worktime.git synced 2024-10-18 08:04:22 +00:00

Save only if data has changed

This commit is contained in:
Knut Ahlers 2016-10-18 07:17:25 +02:00
parent 6f09ab93c2
commit 52399b6938
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/Luzifer/go_helpers/str" "github.com/Luzifer/go_helpers/str"
"github.com/cnf/structhash"
couch "github.com/lancecarlson/couchgo" couch "github.com/lancecarlson/couchgo"
) )
@ -23,6 +24,8 @@ const (
TagOnCall = "on-call" TagOnCall = "on-call"
) )
const schemaVersion = 1
func evalTags(tags []string, tag string) []string { func evalTags(tags []string, tag string) []string {
rawTag := strings.TrimLeft(tag, "+-") rawTag := strings.TrimLeft(tag, "+-")
out := tags out := tags
@ -64,6 +67,8 @@ type Day struct {
IsHoliday bool `json:"is_holiday,omitempty"` IsHoliday bool `json:"is_holiday,omitempty"`
IsEvent bool `json:"is_event,omitempty"` IsEvent bool `json:"is_event,omitempty"`
Homeoffice bool `json:"homeoffice,omitempty"` Homeoffice bool `json:"homeoffice,omitempty"`
initialHash string `hash:"-"`
} }
func (d *Day) Tag(tag string) { func (d *Day) Tag(tag string) {
@ -123,6 +128,8 @@ func LoadDay(db *couch.Client, date time.Time, mayCreate bool) (*Day, error) {
} }
} }
doc.migrate() doc.migrate()
doc.initialHash = fmt.Sprintf("%x", structhash.Sha1(doc, schemaVersion))
return doc, nil return doc, nil
} }
@ -131,6 +138,10 @@ func (d *Day) Save(db *couch.Client) error {
return err return err
} }
if !d.hasChanged() {
return nil
}
res, err := db.Save(d) res, err := db.Save(d)
if err != nil { if err != nil {
return err return err
@ -139,6 +150,10 @@ func (d *Day) Save(db *couch.Client) error {
return nil return nil
} }
func (d *Day) hasChanged() bool {
return d.initialHash != fmt.Sprintf("%x", structhash.Sha1(d, schemaVersion))
}
type Time struct { type Time struct {
ID string `json:"id"` ID string `json:"id"`
Start string `json:"start"` Start string `json:"start"`