mirror of
https://github.com/Luzifer/worktime.git
synced 2024-12-22 22:11:16 +00:00
Save only if data has changed
This commit is contained in:
parent
6f09ab93c2
commit
52399b6938
1 changed files with 15 additions and 0 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/Luzifer/go_helpers/str"
|
||||
"github.com/cnf/structhash"
|
||||
couch "github.com/lancecarlson/couchgo"
|
||||
)
|
||||
|
||||
|
@ -23,6 +24,8 @@ const (
|
|||
TagOnCall = "on-call"
|
||||
)
|
||||
|
||||
const schemaVersion = 1
|
||||
|
||||
func evalTags(tags []string, tag string) []string {
|
||||
rawTag := strings.TrimLeft(tag, "+-")
|
||||
out := tags
|
||||
|
@ -64,6 +67,8 @@ type Day struct {
|
|||
IsHoliday bool `json:"is_holiday,omitempty"`
|
||||
IsEvent bool `json:"is_event,omitempty"`
|
||||
Homeoffice bool `json:"homeoffice,omitempty"`
|
||||
|
||||
initialHash string `hash:"-"`
|
||||
}
|
||||
|
||||
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.initialHash = fmt.Sprintf("%x", structhash.Sha1(doc, schemaVersion))
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
|
@ -131,6 +138,10 @@ func (d *Day) Save(db *couch.Client) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if !d.hasChanged() {
|
||||
return nil
|
||||
}
|
||||
|
||||
res, err := db.Save(d)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -139,6 +150,10 @@ func (d *Day) Save(db *couch.Client) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (d *Day) hasChanged() bool {
|
||||
return d.initialHash != fmt.Sprintf("%x", structhash.Sha1(d, schemaVersion))
|
||||
}
|
||||
|
||||
type Time struct {
|
||||
ID string `json:"id"`
|
||||
Start string `json:"start"`
|
||||
|
|
Loading…
Reference in a new issue