-
Notifications
You must be signed in to change notification settings - Fork 514
[Feat] new scripts on migrate/seed/init run for internal #1421
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
Open
nams1570
wants to merge
2
commits into
dev
Choose a base branch
from
migrate-plans-and-products-ui
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * Grants the `free` plan to every billing team on Stack Auth's own | ||
| * billing project that doesn't already have a plan. Runs at deploy / | ||
| * db init time. | ||
| * | ||
| * Why we need it: we used to give the free plan implicitly via an | ||
| * "include-by-default" rule. Removing that left some old teams with no | ||
| * subscription at all, which made plan-limit checks (user count, | ||
| * analytics events, etc.) read 0 quota and reject every request. This | ||
| * script puts everyone back on a clean baseline. | ||
| * | ||
| * Safe to re-run: a team that already has a plan in the free product | ||
| * line is left alone. | ||
| */ | ||
|
|
||
| import { ensureFreePlanForBillingTeam } from "@/lib/payments/ensure-free-plan"; | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated -- idiomatic way to get the internal tenancy today (see plan-entitlements.ts) | ||
| import { DEFAULT_BRANCH_ID, getSoleTenancyFromProjectBranch, type Tenancy } from "@/lib/tenancies"; | ||
| import { globalPrismaClient } from "@/prisma-client"; | ||
| import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors"; | ||
| import { getOrUndefined } from "@stackframe/stack-shared/dist/utils/objects"; | ||
|
|
||
| // Page size for streaming teams. Big enough to amortise round-trips, | ||
| // small enough to stay tiny in memory (~18KB per page). | ||
| const TEAM_BATCH_SIZE = 500; | ||
|
|
||
| function log(msg: string) { | ||
| console.log(`[Backfill][InternalFreePlans] ${msg}`); | ||
| } | ||
|
|
||
| /** | ||
| * Yields every billing team in the internal tenancy, page by page, | ||
| * ordered by `teamId`. Keyset pagination (`teamId > cursor`) so this | ||
| * stays fast on tenancies with millions of teams. | ||
| */ | ||
| async function* iterateInternalTeamIds( | ||
| internalTenancy: Tenancy, | ||
| batchSize: number, | ||
| ): AsyncIterable<string> { | ||
| let cursor: string | null = null; | ||
| while (true) { | ||
| const batch: { teamId: string }[] = await globalPrismaClient.team.findMany({ | ||
| where: { | ||
| tenancyId: internalTenancy.id, | ||
| ...(cursor != null ? { teamId: { gt: cursor } } : {}), | ||
| }, | ||
| select: { teamId: true }, | ||
| orderBy: { teamId: "asc" }, | ||
| take: batchSize, | ||
| }); | ||
| if (batch.length === 0) return; | ||
| for (const { teamId } of batch) { | ||
| yield teamId; | ||
| } | ||
| cursor = batch[batch.length - 1].teamId; | ||
| } | ||
| } | ||
|
|
||
| export async function runBackfillInternalFreePlans(): Promise<{ | ||
| granted: number, | ||
| failed: number, | ||
| total: number, | ||
| }> { | ||
| log("Starting..."); | ||
| const internalTenancy = await getSoleTenancyFromProjectBranch("internal", DEFAULT_BRANCH_ID, true); | ||
| if (internalTenancy == null) { | ||
| throw new StackAssertionError("Internal billing tenancy not found", { | ||
| billingProjectId: "internal", | ||
| branchId: DEFAULT_BRANCH_ID, | ||
| }); | ||
| } | ||
|
|
||
| // Fail fast if the `free` product is misconfigured. The grant call | ||
| // below silently no-ops in that case; raising here makes the deploy | ||
| // log point at the actual cause instead of "0 granted out of N teams". | ||
| const freePlanProduct = getOrUndefined(internalTenancy.config.payments.products, "free"); | ||
| if ( | ||
| freePlanProduct == null | ||
| || freePlanProduct.customerType !== "team" | ||
| || freePlanProduct.productLineId == null | ||
| ) { | ||
| throw new StackAssertionError( | ||
| "Internal tenancy `free` product is not configured as a team-typed, product-line-tagged plan; cannot run backfill", | ||
| { freePlanProduct }, | ||
| ); | ||
| } | ||
|
|
||
| let granted = 0; | ||
| let failed = 0; | ||
| let total = 0; | ||
|
|
||
| for await (const teamId of iterateInternalTeamIds(internalTenancy, TEAM_BATCH_SIZE)) { | ||
| total++; | ||
| try { | ||
| if (await ensureFreePlanForBillingTeam(teamId)) granted++; | ||
| } catch (e) { | ||
|
nams1570 marked this conversation as resolved.
|
||
| // Per-team isolation: log and keep going. One team's transient | ||
| // DB blip shouldn't leave every later team unprocessed; the next | ||
| // run will retry whatever failed here. | ||
| failed++; | ||
| const err = e instanceof Error ? e : new Error(String(e)); | ||
| console.error( | ||
| `[Backfill][InternalFreePlans][team=${teamId}] Failed: ${err.message}`, | ||
| err, | ||
| ); | ||
| } | ||
| if (total % 100 === 0) { | ||
| log(`Progress: ${total} (granted=${granted}, failed=${failed})`); | ||
| } | ||
| } | ||
|
|
||
| log(`Done. granted=${granted} failed=${failed} total=${total}`); | ||
| return { granted, failed, total }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.