Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACK-2765] Fix schema migration loop #684

Merged
merged 2 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading