Skip to content

Commit

Permalink
review: add "JSON" suffix to new DB fields with JSON payload
Browse files Browse the repository at this point in the history
As per our usual convention.
  • Loading branch information
majewsky committed Feb 24, 2025
1 parent 2b71f6a commit ed56a1b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 96 deletions.
50 changes: 25 additions & 25 deletions internal/api/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,16 @@ func (p *v1Provider) CreateProjectCommitment(w http.ResponseWriter, r *http.Requ
return
}
dbCommitment := db.ProjectCommitment{
AZResourceID: azResourceID,
Amount: req.Amount,
Duration: req.Duration,
CreatedAt: now,
CreatorUUID: token.UserUUID(),
CreatorName: fmt.Sprintf("%s@%s", token.UserName(), token.UserDomainName()),
ConfirmBy: confirmBy,
ConfirmedAt: nil, // may be set below
ExpiresAt: req.Duration.AddTo(unwrapOrDefault(confirmBy, now)),
CreationContext: json.RawMessage(buf),
AZResourceID: azResourceID,
Amount: req.Amount,
Duration: req.Duration,
CreatedAt: now,
CreatorUUID: token.UserUUID(),
CreatorName: fmt.Sprintf("%s@%s", token.UserName(), token.UserDomainName()),
ConfirmBy: confirmBy,
ConfirmedAt: nil, // may be set below
ExpiresAt: req.Duration.AddTo(unwrapOrDefault(confirmBy, now)),
CreationContextJSON: json.RawMessage(buf),
}
if req.ConfirmBy == nil {
// if not planned for confirmation in the future, confirm immediately (or fail)
Expand Down Expand Up @@ -559,7 +559,7 @@ func (p *v1Provider) MergeProjectCommitments(w http.ResponseWriter, r *http.Requ
if respondwith.ErrorText(w, err) {
return
}
dbMergedCommitment.CreationContext = json.RawMessage(buf)
dbMergedCommitment.CreationContextJSON = json.RawMessage(buf)

// Insert into database
err = tx.Insert(&dbMergedCommitment)
Expand All @@ -578,7 +578,7 @@ func (p *v1Provider) MergeProjectCommitments(w http.ResponseWriter, r *http.Requ
}
for _, dbCommitment := range dbCommitments {
dbCommitment.SupersededAt = &now
dbCommitment.SupersedeContext = liquids.PointerTo(json.RawMessage(buf))
dbCommitment.SupersedeContextJSON = liquids.PointerTo(json.RawMessage(buf))
dbCommitment.State = db.CommitmentStateSuperseded
_, err = tx.Update(&dbCommitment)
if respondwith.ErrorText(w, err) {
Expand Down Expand Up @@ -794,7 +794,7 @@ func (p *v1Provider) StartCommitmentTransfer(w http.ResponseWriter, r *http.Requ
}
dbCommitment.State = db.CommitmentStateSuperseded
dbCommitment.SupersededAt = &now
dbCommitment.SupersedeContext = liquids.PointerTo(json.RawMessage(buf))
dbCommitment.SupersedeContextJSON = liquids.PointerTo(json.RawMessage(buf))
_, err = tx.Update(&dbCommitment)
if respondwith.ErrorText(w, err) {
return
Expand Down Expand Up @@ -846,17 +846,17 @@ func (p *v1Provider) buildSplitCommitment(dbCommitment db.ProjectCommitment, amo
return db.ProjectCommitment{}, err
}
return db.ProjectCommitment{
AZResourceID: dbCommitment.AZResourceID,
Amount: amount,
Duration: dbCommitment.Duration,
CreatedAt: now,
CreatorUUID: dbCommitment.CreatorUUID,
CreatorName: dbCommitment.CreatorName,
ConfirmBy: dbCommitment.ConfirmBy,
ConfirmedAt: dbCommitment.ConfirmedAt,
ExpiresAt: dbCommitment.ExpiresAt,
CreationContext: json.RawMessage(buf),
State: dbCommitment.State,
AZResourceID: dbCommitment.AZResourceID,
Amount: amount,
Duration: dbCommitment.Duration,
CreatedAt: now,
CreatorUUID: dbCommitment.CreatorUUID,
CreatorName: dbCommitment.CreatorName,
ConfirmBy: dbCommitment.ConfirmBy,
ConfirmedAt: dbCommitment.ConfirmedAt,
ExpiresAt: dbCommitment.ExpiresAt,
CreationContextJSON: json.RawMessage(buf),
State: dbCommitment.State,
}, nil
}

Expand Down Expand Up @@ -1253,7 +1253,7 @@ func (p *v1Provider) ConvertCommitment(w http.ResponseWriter, r *http.Request) {
}
dbCommitment.State = db.CommitmentStateSuperseded
dbCommitment.SupersededAt = &now
dbCommitment.SupersedeContext = liquids.PointerTo(json.RawMessage(buf))
dbCommitment.SupersedeContextJSON = liquids.PointerTo(json.RawMessage(buf))
_, err = tx.Update(&dbCommitment)
if respondwith.ErrorText(w, err) {
return
Expand Down
4 changes: 2 additions & 2 deletions internal/api/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ func Test_MergeCommitments(t *testing.T) {
assert.DeepEqual(t, "commitment state", supersededCommitment.State, db.CommitmentStateSuperseded)
expectedContext := db.CommitmentWorkflowContext{Reason: db.CommitmentReasonMerge, RelatedCommitmentIDs: []db.ProjectCommitmentID{5}}
var supersedeContext db.CommitmentWorkflowContext
err = json.Unmarshal(*supersededCommitment.SupersedeContext, &supersedeContext)
err = json.Unmarshal(*supersededCommitment.SupersedeContextJSON, &supersedeContext)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1691,7 +1691,7 @@ func Test_MergeCommitments(t *testing.T) {
t.Fatal(err)
}
assert.DeepEqual(t, "commitment state", supersededCommitment.State, db.CommitmentStateSuperseded)
err = json.Unmarshal(*supersededCommitment.SupersedeContext, &supersedeContext)
err = json.Unmarshal(*supersededCommitment.SupersedeContextJSON, &supersedeContext)
if err != nil {
t.Fatal(err)
}
Expand Down
132 changes: 66 additions & 66 deletions internal/collector/commitment_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,29 @@ func TestCleanupOldCommitmentsJob(t *testing.T) {
buf, err := json.Marshal(creationContext)
mustT(t, err)
mustT(t, c.DB.Insert(&db.ProjectCommitment{
ID: 1,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now(),
ConfirmedAt: pointerTo(s.Clock.Now()),
ExpiresAt: commitmentForThreeYears.AddTo(s.Clock.Now()),
State: db.CommitmentStateActive,
CreationContext: buf,
ID: 1,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now(),
ConfirmedAt: pointerTo(s.Clock.Now()),
ExpiresAt: commitmentForThreeYears.AddTo(s.Clock.Now()),
State: db.CommitmentStateActive,
CreationContextJSON: buf,
}))

// test 1: create an expired commitment
s.Clock.StepBy(30 * oneDay)
mustT(t, c.DB.Insert(&db.ProjectCommitment{
ID: 2,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
State: db.CommitmentStateActive,
CreationContext: buf,
ID: 2,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
State: db.CommitmentStateActive,
CreationContextJSON: buf,
}))
tr.DBChanges().Ignore()

Expand All @@ -121,30 +121,30 @@ func TestCleanupOldCommitmentsJob(t *testing.T) {
buf, err = json.Marshal(creationContext)
mustT(t, err)
mustT(t, c.DB.Insert(&db.ProjectCommitment{
ID: 3,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
SupersededAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(5 * time.Minute)),
State: db.CommitmentStateSuperseded,
CreationContext: buf,
ID: 3,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
SupersededAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(5 * time.Minute)),
State: db.CommitmentStateSuperseded,
CreationContextJSON: buf,
}))
creationContext = db.CommitmentWorkflowContext{Reason: db.CommitmentReasonCreate}
buf, err = json.Marshal(creationContext)
mustT(t, err)
mustT(t, c.DB.Insert(&db.ProjectCommitment{
ID: 4,
AZResourceID: 2,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay).Add(5 * time.Minute),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
State: db.CommitmentStateActive,
CreationContext: buf,
ID: 4,
AZResourceID: 2,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay).Add(5 * time.Minute),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
State: db.CommitmentStateActive,
CreationContextJSON: buf,
}))
tr.DBChanges().Ignore()

Expand All @@ -165,44 +165,44 @@ func TestCleanupOldCommitmentsJob(t *testing.T) {
buf, err = json.Marshal(creationContext)
mustT(t, err)
commitment5 := db.ProjectCommitment{
ID: 5,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
SupersededAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(10 * time.Minute)),
State: db.CommitmentStateSuperseded,
CreationContext: buf,
ID: 5,
AZResourceID: 1,
Amount: 10,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay)),
ExpiresAt: s.Clock.Now(),
SupersededAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(10 * time.Minute)),
State: db.CommitmentStateSuperseded,
CreationContextJSON: buf,
}
mustT(t, c.DB.Insert(&commitment5))
commitment6 := db.ProjectCommitment{
ID: 6,
AZResourceID: 1,
Amount: 5,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay).Add(5 * time.Minute),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(5 * time.Minute)),
ExpiresAt: s.Clock.Now().Add(5 * time.Minute),
SupersededAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(10 * time.Minute)),
State: db.CommitmentStateSuperseded,
CreationContext: buf,
ID: 6,
AZResourceID: 1,
Amount: 5,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay).Add(5 * time.Minute),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(5 * time.Minute)),
ExpiresAt: s.Clock.Now().Add(5 * time.Minute),
SupersededAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(10 * time.Minute)),
State: db.CommitmentStateSuperseded,
CreationContextJSON: buf,
}
mustT(t, c.DB.Insert(&commitment6))
creationContext = db.CommitmentWorkflowContext{Reason: db.CommitmentReasonMerge, RelatedCommitmentIDs: []db.ProjectCommitmentID{5, 6}}
buf, err = json.Marshal(creationContext)
mustT(t, err)
mustT(t, c.DB.Insert(&db.ProjectCommitment{
ID: 7,
AZResourceID: 1,
Amount: 15,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay).Add(10 * time.Minute),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(10 * time.Minute)),
ExpiresAt: s.Clock.Now().Add(5 * time.Minute),
State: db.CommitmentStateActive,
CreationContext: buf,
ID: 7,
AZResourceID: 1,
Amount: 15,
Duration: commitmentForOneDay,
CreatedAt: s.Clock.Now().Add(-oneDay).Add(10 * time.Minute),
ConfirmedAt: pointerTo(s.Clock.Now().Add(-oneDay).Add(10 * time.Minute)),
ExpiresAt: s.Clock.Now().Add(5 * time.Minute),
State: db.CommitmentStateActive,
CreationContextJSON: buf,
}))
tr.DBChanges().Ignore()

Expand Down
6 changes: 3 additions & 3 deletions internal/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ type ProjectCommitment struct {

// Commitments can be superseded due to splits, conversions or merges.
// The context columns contain information about the reason and related commitments
SupersededAt *time.Time `db:"superseded_at"`
CreationContext json.RawMessage `db:"creation_context_json"`
SupersedeContext *json.RawMessage `db:"supersede_context_json"`
SupersededAt *time.Time `db:"superseded_at"`
CreationContextJSON json.RawMessage `db:"creation_context_json"`
SupersedeContextJSON *json.RawMessage `db:"supersede_context_json"`

// For a commitment to be transferred between projects, it must first be
// marked for transfer in the source project. Then a new commitment can be
Expand Down

0 comments on commit ed56a1b

Please sign in to comment.