-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
42 lines (35 loc) · 925 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"log"
"github.com/jboverfelt/infest/models"
"github.com/markbates/pop"
)
func insertIntoDb(closures []models.Closure, db *pop.Connection) error {
for _, model := range closures {
err := db.Transaction(func(tx *pop.Connection) error {
cur := model
var found models.Closure
err := tx.Where("name = ?", cur.Name).Where("closuredate = ?", cur.ClosureDate).First(&found)
// if the record is not found then insert
if err != nil {
err = tx.Save(&cur)
if err != nil {
log.Printf("Failed to save new closure %v\n", cur)
}
} else {
// old record is found, update the reopen date and comments
found.ReopenDate = cur.ReopenDate
found.ReopenComments = cur.ReopenComments
err = tx.Save(&found)
if err != nil {
log.Printf("Failed to update closure %v\n", found)
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}