-
Notifications
You must be signed in to change notification settings - Fork 513
[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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ | |
| "wait-until-postgres-is-ready:pg_isready": "until pg_isready -h localhost -p ${NEXT_PUBLIC_STACK_PORT_PREFIX:-81}28 && pg_isready -h localhost -p ${NEXT_PUBLIC_STACK_PORT_PREFIX:-81}34; do sleep 1; done", | ||
| "wait-until-postgres-is-ready": "command -v pg_isready >/dev/null 2>&1 && pnpm run wait-until-postgres-is-ready:pg_isready || sleep 10 # not everyone has pg_isready installed, so we fallback to sleeping", | ||
| "wait-until-clickhouse-is-ready": "pnpm exec wait-on http://localhost:${NEXT_PUBLIC_STACK_PORT_PREFIX:-81}36/ping", | ||
| "start-deps:no-delay": "pnpm pre && pnpm run deps-compose up --detach --build && pnpm run wait-until-postgres-is-ready && pnpm run wait-until-clickhouse-is-ready && pnpm run db:init && echo \"\\nDependencies started in the background as Docker containers. 'pnpm run stop-deps' to stop them\"n", | ||
| "start-deps:no-delay": "pnpm pre && pnpm run deps-compose up --detach --build && pnpm run wait-until-postgres-is-ready && pnpm run wait-until-clickhouse-is-ready && pnpm run db:init && pnpm run db:backfill-internal-free-plans && echo \"\\nDependencies started in the background as Docker containers. 'pnpm run stop-deps' to stop them\"n", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stray After JSON-unescaping, the shell sees: Bash performs adjacent-token concatenation, so 🐛 Proposed fix- "start-deps:no-delay": "pnpm pre && pnpm run deps-compose up --detach --build && pnpm run wait-until-postgres-is-ready && pnpm run wait-until-clickhouse-is-ready && pnpm run db:init && pnpm run db:backfill-internal-free-plans && echo \"\\nDependencies started in the background as Docker containers. 'pnpm run stop-deps' to stop them\"n",
+ "start-deps:no-delay": "pnpm pre && pnpm run deps-compose up --detach --build && pnpm run wait-until-postgres-is-ready && pnpm run wait-until-clickhouse-is-ready && pnpm run db:init && pnpm run db:backfill-internal-free-plans && echo \"\\nDependencies started in the background as Docker containers. 'pnpm run stop-deps' to stop them\"",🤖 Prompt for AI Agents |
||
| "start-deps": "POSTGRES_DELAY_MS=${POSTGRES_DELAY_MS:-0} pnpm run start-deps:no-delay", | ||
| "restart-deps": "pnpm pre && pnpm run stop-deps && pnpm run start-deps", | ||
| "restart-deps:no-delay": "pnpm pre && pnpm run stop-deps && pnpm run start-deps:no-delay", | ||
|
|
@@ -49,6 +49,7 @@ | |
| "db:seed": "pnpm pre && pnpm run --filter=@stackframe/backend db:seed", | ||
| "db:init": "pnpm pre && pnpm run --filter=@stackframe/backend db:init", | ||
| "db:migrate": "pnpm pre && pnpm run --filter=@stackframe/backend db:migrate", | ||
| "db:backfill-internal-free-plans": "pnpm pre && pnpm run --filter=@stackframe/backend db:backfill-internal-free-plans", | ||
| "fern": "pnpm pre && pnpm run --filter=@stackframe/docs fern", | ||
| "dev:full": "pnpm pre && concurrently -k \"pnpm run generate-sdks:watch\" \"pnpm run generate-setup-prompt-docs:watch\" \"turbo run dev --concurrency 99999\"", | ||
| "dev": "pnpm pre && concurrently -k \"pnpm run generate-sdks:watch\" \"pnpm run generate-openapi-docs:watch\" \"pnpm run generate-setup-prompt-docs:watch\" \"turbo run dev --concurrency 99999 --filter=./apps/* --filter=@stackframe/docs-mintlify --filter=@stackframe/stack-docs --filter=./packages/* --filter=./examples/demo \"", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.