-
Notifications
You must be signed in to change notification settings - Fork 4
/
yh_meals.go
90 lines (74 loc) · 2.35 KB
/
yh_meals.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package kmedia
import (
"database/sql"
"runtime/debug"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries/qm"
"github.com/Bnei-Baruch/mdb/common"
"github.com/Bnei-Baruch/mdb/importer/kmedia/kmodels"
"github.com/Bnei-Baruch/mdb/models"
"github.com/Bnei-Baruch/mdb/utils"
)
func ImportFlatCatalogs() {
clock := Init()
stats = NewImportStatistics()
utils.Must(importFlatCatalog(120, common.CT_FRIENDS_GATHERING))
utils.Must(importFlatCatalog(4791, common.CT_MEAL))
stats.dump()
Shutdown()
log.Info("Success")
log.Infof("Total run time: %s", time.Now().Sub(clock).String())
}
func importFlatCatalog(catalogID int, cuType string) error {
catalog, err := kmodels.Catalogs(kmdb,
qm.Where("id=?", catalogID),
qm.Load("Containers")).One()
if err != nil {
return errors.Wrapf(err, "Load catalog %d", catalogID)
}
stats.CatalogsProcessed.Inc(1)
for i := range catalog.R.Containers {
tx, err := mdb.Begin()
utils.Must(err)
if err = importFlatContainer(tx, catalog.R.Containers[i], cuType); err != nil {
utils.Must(tx.Rollback())
stats.TxRolledBack.Inc(1)
log.Error(err)
debug.PrintStack()
continue
} else {
utils.Must(tx.Commit())
stats.TxCommitted.Inc(1)
}
}
return nil
}
func importFlatContainer(exec boil.Executor, container *kmodels.Container, cuType string) error {
stats.ContainersProcessed.Inc(1)
unit, err := models.ContentUnits(mdb, qm.Where("(properties->>'kmedia_id')::int = ?", container.ID)).One()
if err != nil {
if err == sql.ErrNoRows {
log.Infof("New CU %d %s", container.ID, container.Name.String)
_, err := importContainerWOCollectionNewCU(exec, container, cuType)
return err
}
return errors.Wrapf(err, "Lookup content unit kmid %d", container.ID)
}
log.Infof("CU exists [%d] container: %s %d", unit.ID, container.Name.String, container.ID)
_, err = importContainer(exec, container, nil, cuType, "", 0)
if err != nil {
return errors.Wrapf(err, "Import container %d", container.ID)
}
if cuType != common.CONTENT_TYPE_REGISTRY.ByID[unit.TypeID].Name {
log.Infof("Overriding CU Type to %s", cuType)
unit.TypeID = common.CONTENT_TYPE_REGISTRY.ByName[cuType].ID
err = unit.Update(exec, "type_id")
if err != nil {
return errors.Wrapf(err, "Update CU type %d", unit.ID)
}
}
return nil
}