Skip to content

Commit

Permalink
[BACK-2765] Fix schema migration loop (#684)
Browse files Browse the repository at this point in the history
* clear summary properly on schema migration

* add schema migration unit tests
  • Loading branch information
Roukoswarf authored Dec 3, 2023
1 parent f8e6c2c commit 2941bde
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
64 changes: 64 additions & 0 deletions data/summary/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,38 @@ var _ = Describe("Summary Stats Mongo", func() {
Expect(*userCGMSummaryWritten.Dates.OutdatedSinceLimit).To(Equal(now))
})

It("With an existing outdated CGM summary with schema migration reason", func() {
var userCGMSummaryWritten *types.Summary[types.CGMStats, *types.CGMStats]
now := time.Now().UTC().Truncate(time.Millisecond)
fiveMinutesAgo := now.Add(time.Duration(-5) * time.Minute)

userCGMSummary = test.RandomCGMSummary(userId)
userCGMSummary.Dates.OutdatedSince = &fiveMinutesAgo
userCGMSummary.Dates.OutdatedReason = []string{types.OutdatedReasonUploadCompleted}
userCGMSummary.Dates.OutdatedSinceLimit = pointer.FromAny(fiveMinutesAgo.Add(28 * time.Minute))
Expect(userCGMSummary.Stats.Buckets).ToNot(HaveLen(0))
Expect(userCGMSummary.Stats.Periods).ToNot(HaveLen(0))

err = cgmStore.UpsertSummary(ctx, userCGMSummary)
Expect(err).ToNot(HaveOccurred())

outdatedSince, err = cgmStore.SetOutdated(ctx, userId, types.OutdatedReasonSchemaMigration)
Expect(err).ToNot(HaveOccurred())
Expect(outdatedSince).ToNot(BeNil())

userCGMSummaryWritten, err = cgmStore.GetSummary(ctx, userId)
Expect(err).ToNot(HaveOccurred())
Expect(userCGMSummaryWritten.Dates.OutdatedSince).ToNot(BeNil())
Expect(userCGMSummaryWritten.Dates.OutdatedSince).To(Equal(outdatedSince))
Expect(*userCGMSummaryWritten.Dates.OutdatedSinceLimit).To(Equal(outdatedSince.Add(28 * time.Minute)))
Expect(userCGMSummaryWritten.Stats.Buckets).To(HaveLen(0))
Expect(userCGMSummaryWritten.Stats.Periods).To(HaveLen(0))
Expect(userCGMSummaryWritten.Dates.LastData).To(BeNil())
Expect(userCGMSummaryWritten.Dates.FirstData).To(BeNil())
Expect(userCGMSummaryWritten.Dates.LastUpdatedDate.IsZero()).To(BeTrue())
Expect(userCGMSummaryWritten.Dates.LastUploadDate).To(BeNil())
})

It("With no existing BGM summary", func() {
outdatedSince, err = bgmStore.SetOutdated(ctx, userId, types.OutdatedReasonDataAdded)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -696,6 +728,38 @@ var _ = Describe("Summary Stats Mongo", func() {
Expect(*userBGMSummaryWritten.Dates.OutdatedSince).To(Equal(now))
Expect(*userBGMSummaryWritten.Dates.OutdatedSinceLimit).To(Equal(now))
})

It("With an existing outdated BGM summary with schema migration reason", func() {
var userBGMSummaryWritten *types.Summary[types.BGMStats, *types.BGMStats]
now := time.Now().UTC().Truncate(time.Millisecond)
fiveMinutesAgo := now.Add(time.Duration(-5) * time.Minute)

userBGMSummary = test.RandomBGMSummary(userId)
userBGMSummary.Dates.OutdatedSince = &fiveMinutesAgo
userBGMSummary.Dates.OutdatedReason = []string{types.OutdatedReasonUploadCompleted}
userBGMSummary.Dates.OutdatedSinceLimit = pointer.FromAny(fiveMinutesAgo.Add(28 * time.Minute))
Expect(userBGMSummary.Stats.Buckets).ToNot(HaveLen(0))
Expect(userBGMSummary.Stats.Periods).ToNot(HaveLen(0))

err = bgmStore.UpsertSummary(ctx, userBGMSummary)
Expect(err).ToNot(HaveOccurred())

outdatedSince, err = bgmStore.SetOutdated(ctx, userId, types.OutdatedReasonSchemaMigration)
Expect(err).ToNot(HaveOccurred())
Expect(outdatedSince).ToNot(BeNil())

userBGMSummaryWritten, err = bgmStore.GetSummary(ctx, userId)
Expect(err).ToNot(HaveOccurred())
Expect(userBGMSummaryWritten.Dates.OutdatedSince).ToNot(BeNil())
Expect(userBGMSummaryWritten.Dates.OutdatedSince).To(Equal(outdatedSince))
Expect(*userBGMSummaryWritten.Dates.OutdatedSinceLimit).To(Equal(outdatedSince.Add(28 * time.Minute)))
Expect(userBGMSummaryWritten.Stats.Buckets).To(HaveLen(0))
Expect(userBGMSummaryWritten.Stats.Periods).To(HaveLen(0))
Expect(userBGMSummaryWritten.Dates.LastData).To(BeNil())
Expect(userBGMSummaryWritten.Dates.FirstData).To(BeNil())
Expect(userBGMSummaryWritten.Dates.LastUpdatedDate.IsZero()).To(BeTrue())
Expect(userBGMSummaryWritten.Dates.LastUploadDate).To(BeNil())
})
})

Context("GetSummary", func() {
Expand Down
4 changes: 4 additions & 0 deletions data/summary/types/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (s *Summary[T, A]) SetOutdated(reason string) {
set.Append(s.Dates.OutdatedReason...)
}

if reason == OutdatedReasonSchemaMigration {
*s = *Create[T, A](s.UserID)
}

s.Dates.OutdatedReason = set.ToSlice()

timestamp := time.Now().Truncate(time.Millisecond).UTC()
Expand Down

0 comments on commit 2941bde

Please sign in to comment.