Files
Jeremias Menichelli 30833db87e chore: Refactor sync troubleshooting scripts after data reconciliation (#52713)
## 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)
2026-11-04 10:24:19 +01:00

87 lines
2.7 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { createPacer, isRateLimitError, readMinIntervalMs } from './github-throttle.ts'
const requestError = (status: number, message = 'error', headers: Record<string, string> = {}) =>
Object.assign(new Error(message), { status, response: { headers } })
describe('isRateLimitError', () => {
it('recognizes a secondary rate limit 403 by its message', () => {
expect(isRateLimitError(requestError(403, 'You have exceeded a secondary rate limit'))).toBe(
true
)
})
it('recognizes a 403 carrying Retry-After or an exhausted quota', () => {
expect(isRateLimitError(requestError(403, 'Forbidden', { 'retry-after': '30' }))).toBe(true)
expect(isRateLimitError(requestError(403, 'Forbidden', { 'x-ratelimit-remaining': '0' }))).toBe(
true
)
})
it('recognizes any 429', () => {
expect(isRateLimitError(requestError(429))).toBe(true)
})
it('recognizes a GraphQL RATE_LIMITED response error', () => {
const error = Object.assign(new Error('API rate limit already exceeded'), {
errors: [{ type: 'RATE_LIMITED' }],
headers: {},
})
expect(isRateLimitError(error)).toBe(true)
})
it('does not treat other failures as rate limits', () => {
expect(isRateLimitError(requestError(403, 'Resource not accessible by integration'))).toBe(
false
)
expect(isRateLimitError(requestError(404))).toBe(false)
expect(isRateLimitError(requestError(502))).toBe(false)
expect(isRateLimitError(new TypeError('fetch failed'))).toBe(false)
expect(isRateLimitError('boom')).toBe(false)
expect(isRateLimitError(null)).toBe(false)
})
})
describe('createPacer', () => {
it('waits out the remainder of the minimum interval between calls', async () => {
let clock = 1_000
const sleep = vi.fn(async (ms: number) => {
clock += ms
})
const pace = createPacer(2_000, { now: () => clock, sleep })
await pace() // the first call never waits
clock += 500
await pace()
expect(sleep).toHaveBeenCalledTimes(1)
expect(sleep).toHaveBeenCalledWith(1_500)
})
it('does not wait when the interval has already passed', async () => {
let clock = 0
const sleep = vi.fn(async () => {})
const pace = createPacer(2_000, { now: () => clock, sleep })
await pace()
clock += 5_000
await pace()
expect(sleep).not.toHaveBeenCalled()
})
})
describe('readMinIntervalMs', () => {
it('defaults to 2s', () => {
expect(readMinIntervalMs(['node', 'script'])).toBe(2000)
})
it('reads --delay-ms and ignores invalid values', () => {
expect(readMinIntervalMs(['--delay-ms=5000'])).toBe(5000)
expect(readMinIntervalMs(['--delay-ms=abc'])).toBe(2000)
expect(readMinIntervalMs(['--delay-ms=-1'])).toBe(2000)
})
})