mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem
We solved manually some `troubleshooting_entries` table missing data and
mismatches.
- **Rows without slugs.** Some `troubleshooting_entries` rows had no
slug, so no guide could match them and the guides looked "new".
- **Duplicate discussions.** A guide that didn't match its row got a
fresh GitHub discussion, leaving the original behind (sometimes with
extras).
- **Slug mismatch.** The fetch stripped the hash suffix from file names
while the table keeps it, so those guides never matched their rows.
- **Overlapping runs.** The workflow had no concurrency control, so two
runs could both create a discussion for the same guide, and `upsert`
silently overwrote the first run's row.
- **Missing checksums.** Rows without a checksum counted as "changed"
and were re-pushed to GitHub on every run.
- **Weak dry run.** `--dry-run` only printed slugs, so it was hard to
see what a run would actually do.
- **Fragile lookup and duplicated code.** Rows were fetched with every
slug in the URL (`.in('slug', …)`), and both scripts carried their own
copy of the guide reader and checksum.
## Solution
After those steps we applied successfully to the database, we revamp our
existing sync scripts to allow dry runs to make sure the changes we
apply are going to generate the correct impact.
A new improvement to prevent bulk discussion errors on GitHub API.
<!--
## Preview links
If relevant, include links to changed pages for easy review access.
Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.
| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->
<!-- ## Additional context
Optionally add any other context or screenshots.
-->
## Review instructions
_Sadly, we need to merge, and pray..._
## Checklist
Check all before review:
- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
// Pacing for the sync's GitHub GraphQL mutations. GitHub rate-limits content
|
|
// creation, so createPacer() keeps a minimum gap between mutations and
|
|
// isRateLimitError() lets the scripts stop cleanly if the limit is hit anyway.
|
|
// Mutations are never retried: a retry after a timeout could create a second
|
|
// discussion (callers pass `request: { retries: 0 }` to disable the plugin's).
|
|
|
|
export const DEFAULT_MIN_INTERVAL_MS = 2000
|
|
|
|
const realSleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))
|
|
|
|
/** Resolves once `minIntervalMs` has passed since the previous call resolved. */
|
|
export function createPacer(
|
|
minIntervalMs: number,
|
|
{
|
|
now = Date.now,
|
|
sleep = realSleep,
|
|
}: { now?: () => number; sleep?: (ms: number) => Promise<void> } = {}
|
|
): () => Promise<void> {
|
|
let lastAt = Number.NEGATIVE_INFINITY
|
|
return async () => {
|
|
const wait = lastAt + minIntervalMs - now()
|
|
if (wait > 0) await sleep(wait)
|
|
lastAt = now()
|
|
}
|
|
}
|
|
|
|
type GitHubErrorLike = {
|
|
status?: number
|
|
message?: string
|
|
response?: { headers?: Record<string, string | number | undefined> } // RequestError
|
|
headers?: Record<string, string | number | undefined> // GraphqlResponseError
|
|
errors?: Array<{ type?: string }>
|
|
}
|
|
|
|
/** A GraphQL RATE_LIMITED error, any 429, or a 403 that signals rate limiting. */
|
|
export function isRateLimitError(error: unknown): boolean {
|
|
if (typeof error !== 'object' || error === null) return false
|
|
const e = error as GitHubErrorLike
|
|
|
|
if (e.errors?.some((err) => err.type === 'RATE_LIMITED')) return true
|
|
if (e.status === 429) return true
|
|
if (e.status !== 403) return false
|
|
|
|
const headers = e.response?.headers ?? e.headers ?? {}
|
|
return (
|
|
/rate limit|abuse detection/i.test(e.message ?? '') ||
|
|
Number.isFinite(Number(headers['retry-after'])) ||
|
|
String(headers['x-ratelimit-remaining']) === '0'
|
|
)
|
|
}
|
|
|
|
/** `--delay-ms=3000` overrides the minimum gap between mutations. */
|
|
export function readMinIntervalMs(argv: string[] = process.argv): number {
|
|
const raw = argv.find((arg) => arg.startsWith('--delay-ms='))?.split('=')[1]
|
|
const parsed = Number(raw)
|
|
return raw !== undefined && Number.isFinite(parsed) && parsed >= 0
|
|
? parsed
|
|
: DEFAULT_MIN_INTERVAL_MS
|
|
}
|