Commit 914bb37
fix(api): tier upgrade promotes team default TTL + auto_24h deploys (P1) (#212)
* fix(api): tier upgrade promotes team default TTL + auto_24h deploys
A Pro-tier user (mastermanas805) just got an "expires in 6 hours" email
the day after upgrading free→pro. Root cause: subscription.charged only
called UpgradeTeamAllTiers, which lifts per-deploy ttl_policy as a
side-effect but never touches teams.default_deployment_ttl_policy — so
every NEXT POST /deploy/new still inherited 'auto_24h' and re-fired the
24h-expiry reminder cycle.
Wires a new models.PromoteDeploymentTTLsForTeam (single tx) into
handleSubscriptionCharged for tiers >= hobby:
- flips teams.default_deployment_ttl_policy auto_24h → permanent
(user-explicit non-auto_24h defaults are LEFT UNTOUCHED)
- promotes every non-terminal ttl_policy='auto_24h' deploy to
permanent + clears expires_at + resets the reminders ledger
- emits team.ttl_policies_promoted audit row + counter
instant_tier_upgrade_ttl_promote_total{outcome=success|noop|error}
Fail-open: the upgrade tx has already committed by promote-time, so a
promote error never 500s the webhook (operator runs cmd/backfill-tier-ttl
to repair the residual; the function is idempotent).
Coverage block (CLAUDE.md rule 17):
Symptom: team.default stays 'auto_24h' + auto_24h deploys keep
firing "expires in N hours" emails after paid upgrade
Enumeration: rg -F 'UpdatePlanTier' rg -F 'UpgradeTeamAllTiers'
rg -F 'default_deployment_ttl_policy'
Sites found: 4 (billing webhook, /internal/set-tier, admin tier change,
PATCH /api/v1/team/settings)
Sites touched: 1 — the Razorpay webhook is the ONLY paid-tier
promotion path (set-tier is dev-only, admin demote
keeps existing state by design, team-settings is the
user's own override and must not trigger promote)
Coverage test: e2e/reliability_contract_test.go's audit-kinds registry
iterator (rule 18) flags any new AuditKind* constant
that ships without a downstream-consumer entry, and
TestPlansRegistryUpgradeTargets_AllInvokePromoteGuard
iterates plans.Registry tiers so a new tier added
between free and hobby would loudly fail the guard.
Live verified: awaiting user verification of the next paid upgrade —
the new metric NR alert (tier-upgrade-ttl-promote-failed)
pages on any outcome=error tick within 10m.
Backfill: operator runs `DATABASE_URL=… go run ./cmd/backfill-tier-ttl
-apply` once to repair every paid team with stale auto_24h state. The
script is idempotent.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(backfill-tier-ttl): silence errcheck on all fmt.Fprint calls
* test(api#212): close 100% patch-coverage gap on tier-upgrade TTL promote
Adds the missing branch coverage for the Pro-upgrade auto-promote TTL
fix landed in 82de682, taking the three changed surfaces to 100%:
- cmd/backfill-tier-ttl: refactored main() → exitFn-wrapped run() with
injectable openDB + promoteFn seams (mirrors cmd/openapi-snapshot).
Ten tests cover usage errors, DB open/ping/query/scan/rows failures,
dry-run vs apply modes, mixed ok/error per-team tallying, env-var
fallback, and the main() exit-code dispatch.
- models.PromoteDeploymentTTLsForTeam: six sqlmock-driven tests for the
begin/exec/rows-affected/commit error wrappers that a real Postgres
test DB can't drive on demand.
- handlers.handleSubscriptionCharged + emitTTLPoliciesPromotedAudit:
added the promoteDeploymentTTLsForTeamFn seam (same pattern as
billingPortalFactory) so the fail-open promote-error branch is
reachable. Two tests: nil-db audit early-return + webhook still 200s
on a simulated promote tx failure.
No production behaviour change: the cmd refactor keeps the same exit
codes and the handler seam is a package-level var pointing at the real
models call, swapped only by tests.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* test(backfill-tier-ttl): cover openDB default factory (line 113)
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent db10a0e commit 914bb37
11 files changed
Lines changed: 1757 additions & 0 deletions
File tree
- cmd/backfill-tier-ttl
- e2e
- internal
- handlers
- metrics
- models
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
0 commit comments