Log Postgres test failures more verbosely, fix SubscriptionChange.IsEmpty.
SubscriptionChange.IsEmpty() would return false even if no actual database
operations are going to be performed. This is because we allow information we
_don't_ store in the database (Stripe source, Stripe email) to be specified in a
SubscriptionChange object, just so we can easily access them. Then we use the
Stripe API to store them in Stripe's databases, and turn them into data _we_
store in our database. Think of them as pre-processed values that are never
stored raw.
The problem is, we were treating these properties the same as the properties we
actually stored in the database, and (worse) were running database tests for
combinations of these properties, which was causing test failures because we
were trying to update no columns in the database. Whoops.
I removed these properties from the IsEmpty helper, and removed them from the
code that generates the SubscriptionChange permutations for testing. This allows
tests to pass, but also stays closer to what the system was designed to do.
In tracking down this bug, I discovered that the logging we had for errors when
running Postgres tests was inadequate, so I updated the logs when that failure
occurs while testing Postgres to help surface future failures faster.
4 "code.secondbit.org/uuid.hg"
7 func stripeSubscriptionInMemstore(stripeSubscription string, m *Memstore) bool {
8 for _, sub := range m.subscriptions {
9 if sub.StripeSubscription == stripeSubscription {
16 // CreateSubscription stores the passed Subscription in the Memstore. If
17 // a Subscription sharing the same UserID already exists, an
18 // ErrSubscriptionAlreadyExists error will be returned. If a Subscription
19 // sharing the same StripeSubscription already exists, an
20 // ErrStripeSubscriptionAlreadyExists error will be returned.
21 func (m *Memstore) CreateSubscription(sub Subscription) error {
22 m.subscriptionLock.Lock()
23 defer m.subscriptionLock.Unlock()
25 if _, ok := m.subscriptions[sub.UserID.String()]; ok {
26 return ErrSubscriptionAlreadyExists
28 if stripeSubscriptionInMemstore(sub.StripeSubscription, m) {
29 return ErrStripeSubscriptionAlreadyExists
31 m.subscriptions[sub.UserID.String()] = sub
35 // UpdateSubscription applies the SubscriptionChange passed to the Subscription
36 // stored in the Memstore associated with the passed ID. If change is empty,
37 // an ErrSubscriptionChangeEmpty error is returned. If no Subscription is found
38 // in the Memstore with the passed ID, an ErrSubscriptionNotFound error is returned.
39 // If change is updating the StripeSubscription, and a Subscription in the Memstore
40 // already has that value set for StripeSubscription, an
41 // ErrStripeSubscriptionAlreadyExists error is returned.
42 func (m *Memstore) UpdateSubscription(id uuid.ID, change SubscriptionChange) error {
44 return ErrSubscriptionChangeEmpty
47 m.subscriptionLock.Lock()
48 defer m.subscriptionLock.Unlock()
50 s, ok := m.subscriptions[id.String()]
52 return ErrSubscriptionNotFound
54 if change.StripeSubscription != nil {
55 if stripeSubscriptionInMemstore(*change.StripeSubscription, m) {
56 return ErrStripeSubscriptionAlreadyExists
60 m.subscriptions[id.String()] = s
64 // DeleteSubscription removes the Subscription stored in the Memstore associated
65 // with the passed ID from the Memstore. If no Subscription is found
66 // in the Memstore with the passed ID, an ErrSubscriptionNotFound error is returned.
67 func (m *Memstore) DeleteSubscription(id uuid.ID) error {
68 m.subscriptionLock.Lock()
69 defer m.subscriptionLock.Unlock()
71 _, ok := m.subscriptions[id.String()]
73 return ErrSubscriptionNotFound
75 delete(m.subscriptions, id.String())
79 // GetSubscriptions retrieves the Subscriptions stored in the Memstore associated
80 // with the passed IDs. If no IDs are passed, an ErrNoSubscriptionID error is
81 // returned. No matter how many of the IDs are found (including none), a map is
82 // returned, with the key being a String()ed version of the ID for the Subscription in
83 // the value. If no error is returned, the map will represent all of the Subscriptions// matching the passed IDs that exist in the Memstore, even if it's empty.
84 func (m *Memstore) GetSubscriptions(ids []uuid.ID) (map[string]Subscription, error) {
86 return map[string]Subscription{}, ErrNoSubscriptionID
88 m.subscriptionLock.RLock()
89 defer m.subscriptionLock.RUnlock()
91 result := map[string]Subscription{}
93 for _, id := range ids {
94 s, ok := m.subscriptions[id.String()]
98 result[s.UserID.String()] = s
103 // GetSubscriptionStats returns statistics about the subscription data stored in the
104 // Memstore as a SubscriptionStats variable. The number of Subscriptions, the
105 // breakdown of how many Subscriptions belong to each plan, the number of
106 // Subscriptions that are canceling, and the number of Subscriptions whose payment
107 // information is failing are all tracked.
108 func (m *Memstore) GetSubscriptionStats() (SubscriptionStats, error) {
109 m.subscriptionLock.RLock()
110 defer m.subscriptionLock.RUnlock()
112 stats := SubscriptionStats{
113 Plans: map[string]int64{},
116 for _, subscription := range m.subscriptions {
118 stats.Plans[subscription.Plan]++
120 if subscription.Canceling {
123 if subscription.Status == "past_due" || subscription.Status == "unpaid" {