An open-source, faster Devin alternative
TypeScript
billing: 10x memory tier prices + grandfather existing orgs (#142)
Mohamed Habibc21f21e96d91
diff --git a/cmd/migrate-prices/main.go b/cmd/migrate-prices/main.goindex 6e9c5fde..a766eeef 100644--- a/cmd/migrate-prices/main.go+++ b/cmd/migrate-prices/main.go@@ -8,6 +8,12 @@// DATABASE_URL=... STRIPE_SECRET_KEY=... \// go run ./cmd/migrate-prices --tier=8192 --live//+// By default, orgs with orgs.price_locked=TRUE are skipped (grandfathered).+// Pass --force to override and migrate every locked org at the given tier.+// To cherry-pick a single grandfathered org onto new pricing, pass+// --org=<uuid> — that scopes the run to just that org and implicitly+// overrides its lock (no need to combine with --force).+//// The command is idempotent: items already attached to the target price are// skipped. Usage already accrued this cycle stays at the previous rate because// we pass proration_behavior=none; metered events are matched to whichever@@ -32,6 +38,8 @@ func main() {tier := flag.Int("tier", 8192, "memory_mb tier to migrate (must be a key in billing.TierPriceKey)")dryRun := flag.Bool("dry-run", true, "if true, prints what would change without calling Stripe")live := flag.Bool("live", false, "must be set to actually mutate Stripe; overrides --dry-run")+ force := flag.Bool("force", false, "also migrate orgs with price_locked=TRUE (off by default — grandfathered orgs stay on their current price)")+ orgID := flag.String("org", "", "if set, scope the run to this single org UUID and override its price_locked status (no need to also pass --force)")flag.Parse()if *live {@@ -70,12 +78,21 @@ func main() {}log.Printf("target price for tier %d: %s (key=%s)", *tier, targetPriceID, billing.TierPriceKey[*tier])- rows, err := pool.Query(ctx,- `SELECT osi.org_id, osi.stripe_subscription_item_id, o.stripe_subscription_id+ query := `SELECT osi.org_id, osi.stripe_subscription_item_id, o.stripe_subscription_id, o.price_lockedFROM org_subscription_items osiJOIN orgs o ON o.id = osi.org_idWHERE osi.memory_mb = $1- AND o.stripe_subscription_id IS NOT NULL`, *tier)+ AND o.stripe_subscription_id IS NOT NULL`+ args := []interface{}{*tier}+ switch {+ case *orgID != "":+ // --org scopes to one org and implicitly overrides its lock.+ query += ` AND o.id = $2`+ args = append(args, *orgID)+ case !*force:+ query += ` AND o.price_locked = FALSE`+ }+ rows, err := pool.Query(ctx, query, args...)if err != nil {log.Fatalf("query items: %v", err)}@@ -83,11 +100,12 @@ func main() {type target struct {orgID, itemID, subID string+ locked bool}var targets []targetfor rows.Next() {var t target- if err := rows.Scan(&t.orgID, &t.itemID, &t.subID); err != nil {+ if err := rows.Scan(&t.orgID, &t.itemID, &t.subID, &t.locked); err != nil {log.Fatalf("scan: %v", err)}targets = append(targets, t)@@ -95,7 +113,14 @@ func main() {if err := rows.Err(); err != nil {log.Fatalf("rows err: %v", err)}- log.Printf("found %d candidate subscription item(s)", len(targets))+ switch {+ case *orgID != "":+ log.Printf("found %d candidate subscription item(s) (--org=%s: scoped to single org, price_locked ignored)", len(targets), *orgID)+ case *force:+ log.Printf("found %d candidate subscription item(s) (--force: price_locked ignored)", len(targets))+ default:+ log.Printf("found %d candidate subscription item(s) (price_locked=FALSE)", len(targets))+ }stripe.Key = stripeKey@@ -116,7 +141,11 @@ func main() {continue}- log.Printf("[PLAN] org=%s item=%s price %s → %s", t.orgID, t.itemID, currentPrice, targetPriceID)+ lockTag := ""+ if t.locked {+ lockTag = " [LOCKED-override]"+ }+ log.Printf("[PLAN]%s org=%s item=%s price %s → %s", lockTag, t.orgID, t.itemID, currentPrice, targetPriceID)if *dryRun {continue}diff --git a/internal/billing/pricing.go b/internal/billing/pricing.goindex 6091b848..6fa1da4e 100644--- a/internal/billing/pricing.go+++ b/internal/billing/pricing.go@@ -5,13 +5,17 @@ import ()// TierPricePerSecond maps memory_mb → USD per second.+// Rates were 10×-ed on 2026-04-15 on top of the per-second correction that+// landed earlier the same day. Existing paying orgs are grandfathered via+// orgs.price_locked (see migration 022); cmd/migrate-prices skips locked orgs+// by default so only new signups see these rates.var TierPricePerSecond = map[int]float64{- 1024: 0.000001080246914, // 1GB / 1 vCPU- 4096: 0.000005787037037, // 4GB / 1 vCPU- 8192: 0.00001350308642, // 8GB / 2 vCPU- 16384: 0.00002700617284, // 16GB / 4 vCPU- 32768: 0.0001929012346, // 32GB / 8 vCPU- 65536: 0.0005401234568, // 64GB / 16 vCPU+ 1024: 0.00001080246914, // 1GB / 1 vCPU+ 4096: 0.00005787037037, // 4GB / 1 vCPU+ 8192: 0.0001350308642, // 8GB / 2 vCPU+ 16384: 0.0002700617284, // 16GB / 4 vCPU+ 32768: 0.001929012346, // 32GB / 8 vCPU+ 65536: 0.005401234568, // 64GB / 16 vCPU}// TierMeterKey maps memory_mb → stable key used to derive the Stripe meter@@ -30,23 +34,21 @@ var TierMeterKey = map[int]string{// Bump the suffix (e.g. sandbox_8gb → sandbox_8gb_v2) whenever TierPricePerSecond// changes for that tier: Stripe Prices are immutable, so a new key forces// EnsureProducts to create a fresh Price at the new rate. Existing subscriptions-// must then be migrated to the new Price via cmd/migrate-prices.+// must then be migrated to the new Price via cmd/migrate-prices — unless the+// org is marked price_locked=TRUE, in which case they are grandfathered.//-// The suffixes were bumped one step (1gb→_v2, 4gb→_v2, 8gb_v2→_v3, 16gb→_v2,-// 32gb→_v2, 64gb→_v2) to force EnsureProducts to recreate every memory-tier-// Stripe Price at the documented per-second rate. The previously-deployed-// Prices were calibrated off a stale rate map and were under-billing-// subscribers by 60× (per-minute economics instead of per-second). Run-// `migrate-prices --tier=<X> --live` for every tier after deploy to move all-// existing subscriptions onto the corrected Prices — this is a correction,-// not a price change, so no grandfathering.+// The suffixes here are bumped one step above the per-second-correction set+// (which established _v2 for most tiers and _v3 for 8GB). This forces+// EnsureProducts to create fresh Prices at the 10×-higher rates below. Every+// org currently paying is locked via migration 022, so migrate-prices skips+// them by default — only new signups subscribe at the 10× rate.var TierPriceKey = map[int]string{- 1024: "sandbox_1gb_v2",- 4096: "sandbox_4gb_v2",- 8192: "sandbox_8gb_v3",- 16384: "sandbox_16gb_v2",- 32768: "sandbox_32gb_v2",- 65536: "sandbox_64gb_v2",+ 1024: "sandbox_1gb_v3",+ 4096: "sandbox_4gb_v3",+ 8192: "sandbox_8gb_v4",+ 16384: "sandbox_16gb_v3",+ 32768: "sandbox_32gb_v3",+ 65536: "sandbox_64gb_v3",}// Disk overage billing — every GB above DiskFreeAllowanceMB is metered for thediff --git a/internal/db/migrations/022_orgs_price_locked.down.sql b/internal/db/migrations/022_orgs_price_locked.down.sqlnew file mode 100644index 00000000..3bdbd55c--- /dev/null+++ b/internal/db/migrations/022_orgs_price_locked.down.sql@@ -0,0 +1 @@+ALTER TABLE orgs DROP COLUMN IF EXISTS price_locked;diff --git a/internal/db/migrations/022_orgs_price_locked.up.sql b/internal/db/migrations/022_orgs_price_locked.up.sqlnew file mode 100644index 00000000..a96410bf--- /dev/null+++ b/internal/db/migrations/022_orgs_price_locked.up.sql@@ -0,0 +1,8 @@+-- Grandfather existing paying orgs at whatever Stripe Price IDs they currently+-- have. price_locked=TRUE means cmd/migrate-prices (and any future price-swap+-- tool) will skip this org by default. New signups default to FALSE so the+-- next price migration naturally picks them up.+ALTER TABLE orgs ADD COLUMN price_locked BOOLEAN NOT NULL DEFAULT FALSE;++-- One-shot lock of every currently-paying org at this point in time.+UPDATE orgs SET price_locked = TRUE WHERE stripe_subscription_id IS NOT NULL;diff --git a/internal/db/store.go b/internal/db/store.goindex fca21b2f..6120eeff 100644--- a/internal/db/store.go+++ b/internal/db/store.go@@ -108,6 +108,7 @@ func (s *Store) Migrate(ctx context.Context) error {{21, "migrations/019_org_max_disk_mb.up.sql"},{22, "migrations/020_scale_events_disk_mb.up.sql"},{23, "migrations/021_migration_state.up.sql"},+ {24, "migrations/022_orgs_price_locked.up.sql"},}for _, m := range migrations {@@ -171,6 +172,7 @@ type Org struct {StripeCustomerID *string `json:"stripeCustomerId,omitempty"`StripeSubscriptionID *string `json:"stripeSubscriptionId,omitempty"`LastUsageReportedAt time.Time `json:"lastUsageReportedAt"`+ PriceLocked bool `json:"priceLocked"`}// orgColumns is the list of columns returned by all Org queries.@@ -178,7 +180,7 @@ const orgColumns = `id, name, slug, plan, max_concurrent_sandboxes, max_sandbox_custom_domain, cf_hostname_id, domain_verification_status, domain_ssl_status,verification_txt_name, verification_txt_value, ssl_txt_name, ssl_txt_value,workos_org_id, is_personal, owner_user_id, credit_balance_cents,- stripe_customer_id, stripe_subscription_id, last_usage_reported_at`+ stripe_customer_id, stripe_subscription_id, last_usage_reported_at, price_locked`// scanOrg scans a row into an Org struct.func scanOrg(row pgx.Row) (*Org, error) {@@ -190,6 +192,7 @@ func scanOrg(row pgx.Row) (*Org, error) {&org.VerificationTxtName, &org.VerificationTxtValue, &org.SSLTxtName, &org.SSLTxtValue,&org.WorkOSOrgID, &org.IsPersonal, &org.OwnerUserID, &org.CreditBalanceCents,&org.StripeCustomerID, &org.StripeSubscriptionID, &org.LastUsageReportedAt,+ &org.PriceLocked,)return org, err}