Skip to content

Commit 3425558

Browse files
authored
feat(store): billing invoice and subscription provider ids are unique over live rows only (#1981)
1 parent 13cc5f6 commit 3425558

4 files changed

Lines changed: 39 additions & 0 deletions

‎internal/store/postgres/billing_invoice_repository_pg_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ func (s *BillingInvoiceRepositoryPGTestSuite) TestSearchSkipsDeletedInvoicesCust
147147
s.Equal([]string{"bi-inv-live"}, out)
148148
}
149149

150+
func (s *BillingInvoiceRepositoryPGTestSuite) TestProviderIDIsUniqueOverLiveRowsOnly() {
151+
s.invoice("bi-cust-live", "bi-inv-again", "paid")
152+
s.exec(`UPDATE billing_invoices SET deleted_at = now() WHERE provider_id = 'bi-inv-again'`)
153+
154+
s.invoice("bi-cust-live", "bi-inv-again", "paid")
155+
156+
_, err := s.client.DB.ExecContext(s.ctx, `INSERT INTO billing_invoices (customer_id, provider_id, amount, currency, hosted_url, state)
157+
VALUES ((SELECT id FROM billing_customers WHERE name = 'bi-cust-live'), 'bi-inv-again', 100, 'usd', 'bi-inv-again', 'paid')`)
158+
s.Require().Error(err, "two live rows must not share a provider id")
159+
}
160+
150161
func TestBillingInvoiceRepositoryPG(t *testing.T) {
151162
suite.Run(t, new(BillingInvoiceRepositoryPGTestSuite))
152163
}

‎internal/store/postgres/billing_subscription_repository_pg_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ func (s *BillingSubscriptionRepositoryPGTestSuite) TestListSkipsDeleted() {
107107
s.Equal("bs-sub-live", got[0].ProviderID)
108108
}
109109

110+
func (s *BillingSubscriptionRepositoryPGTestSuite) TestProviderIDIsUniqueOverLiveRowsOnly() {
111+
s.subscription("bs-sub-again")
112+
s.exec(`UPDATE billing_subscriptions SET deleted_at = now() WHERE provider_id = 'bs-sub-again'`)
113+
114+
s.subscription("bs-sub-again")
115+
116+
_, err := s.client.DB.ExecContext(s.ctx, `INSERT INTO billing_subscriptions (customer_id, provider_id, plan_id, state)
117+
VALUES ((SELECT id FROM billing_customers WHERE name = 'bs-cust'), 'bs-sub-again',
118+
(SELECT id FROM billing_plans WHERE name = 'bs-plan'), 'active')`)
119+
s.Require().Error(err, "two live rows must not share a provider id")
120+
121+
live := scalarSQL(s.T(), s.ctx, s.client,
122+
`SELECT count(*)::text FROM billing_subscriptions WHERE provider_id = 'bs-sub-again' AND deleted_at IS NULL`)
123+
s.Equal("1", live)
124+
}
125+
110126
func TestBillingSubscriptionRepositoryPG(t *testing.T) {
111127
suite.Run(t, new(BillingSubscriptionRepositoryPGTestSuite))
112128
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ALTER TABLE billing_subscriptions ADD CONSTRAINT billing_subscriptions_provider_id_plan_id_key UNIQUE (provider_id, plan_id);
2+
ALTER TABLE billing_subscriptions ADD CONSTRAINT billing_subscriptions_provider_id_key UNIQUE (provider_id);
3+
DROP INDEX IF EXISTS uq_billing_subscriptions_provider_id_live;
4+
5+
ALTER TABLE billing_invoices ADD CONSTRAINT billing_invoices_provider_id_key UNIQUE (provider_id);
6+
DROP INDEX IF EXISTS uq_billing_invoices_provider_id_live;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE UNIQUE INDEX IF NOT EXISTS uq_billing_invoices_provider_id_live ON billing_invoices (provider_id) WHERE deleted_at IS NULL;
2+
ALTER TABLE billing_invoices DROP CONSTRAINT IF EXISTS billing_invoices_provider_id_key;
3+
4+
CREATE UNIQUE INDEX IF NOT EXISTS uq_billing_subscriptions_provider_id_live ON billing_subscriptions (provider_id) WHERE deleted_at IS NULL;
5+
ALTER TABLE billing_subscriptions DROP CONSTRAINT IF EXISTS billing_subscriptions_provider_id_key;
6+
ALTER TABLE billing_subscriptions DROP CONSTRAINT IF EXISTS billing_subscriptions_provider_id_plan_id_key;

0 commit comments

Comments
 (0)