-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg.go
92 lines (78 loc) · 2.08 KB
/
pg.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
91
92
package planning
import (
"context"
"fmt"
"github.com/ttab/elephant-repository/postgres"
"github.com/ttab/newsdoc"
)
func UpdateDatabase(
ctx context.Context,
tx postgres.DBTX,
doc newsdoc.Document,
version int64,
) error {
q := postgres.New(tx)
item, err := NewItemFromDocument(doc)
if err != nil {
return fmt.Errorf("failed to extract planning information from document: %w", err)
}
rows, err := item.ToRows(version)
if err != nil {
return fmt.Errorf("failed to create row updates from planning item: %w", err)
}
err = q.SetPlanningItem(ctx, rows.Item)
if err != nil {
return fmt.Errorf("failed to update planning item: %w", err)
}
for _, a := range rows.Assignments {
err = q.SetPlanningAssignment(ctx, a)
if err != nil {
return fmt.Errorf(
"failed to update assignment %q: %w",
a.UUID, err)
}
}
for _, a := range rows.Assignees {
err = q.SetPlanningAssignee(ctx, a)
if err != nil {
return fmt.Errorf(
"failed to update assignee %q: %w",
a.Assignee, err)
}
}
for _, a := range rows.Deliverables {
err = q.SetPlanningItemDeliverable(ctx, a)
if err != nil {
return fmt.Errorf(
"failed to update deliverable %q: %w",
a.Document, err)
}
}
// Cleanup phase. All rows inlude the version of the document they were
// based on, so the cleanup process deletes all rows that don't match
// the version we're currently writing.
err = q.CleanUpAssignments(ctx, postgres.CleanUpAssignmentsParams{
PlanningItem: rows.Item.UUID,
Version: rows.Item.Version,
})
if err != nil {
return fmt.Errorf("failed to clean up removed assignments: %w", err)
}
for _, a := range rows.Assignments {
err = q.CleanUpAssignees(ctx, postgres.CleanUpAssigneesParams{
Assignment: a.UUID,
Version: a.Version,
})
if err != nil {
return fmt.Errorf("failed to clean up removed assignnees: %w", err)
}
err = q.CleanUpDeliverables(ctx, postgres.CleanUpDeliverablesParams{
Assignment: a.UUID,
Version: a.Version,
})
if err != nil {
return fmt.Errorf("failed to clean up removed deliverables: %w", err)
}
}
return nil
}