Blame · Line-by-line history
emergency-pat.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 0dc3291 | 1 | // Emergency PAT creator — bypasses the live site entirely. |
| 2 | // | |
| 3 | // Use case: the deployed site is broken (e.g. SW reload loop) such that | |
| 4 | // you can't reach /settings/tokens in the browser, but you need a PAT | |
| 5 | // to `git push` the fix. This script writes a PAT row directly into | |
| 6 | // the production database via DATABASE_URL. | |
| 7 | // | |
| 8 | // Run: bun run scripts/emergency-pat.ts | |
| 9d06598 | 9 | // Required env: DATABASE_URL (from your local .env, or set inline) |
| 0dc3291 | 10 | // |
| 9d06598 | 11 | // Uses RAW SQL via @neondatabase/serverless rather than drizzle's |
| 12 | // schema layer. Reason: when this script is needed, production is | |
| 13 | // usually missing recent migrations, so drizzle's SELECT (which lists | |
| 14 | // every schema column) blows up on missing columns. Raw SQL only | |
| 15 | // touches the columns we name, so schema drift doesn't matter. | |
| 0dc3291 | 16 | |
| 9d06598 | 17 | import { neon } from "@neondatabase/serverless"; |
| 0dc3291 | 18 | |
| 19 | function generateToken(): string { | |
| 20 | const bytes = crypto.getRandomValues(new Uint8Array(32)); | |
| 21 | return ( | |
| 22 | "glc_" + | |
| 23 | Array.from(bytes) | |
| 24 | .map((b) => b.toString(16).padStart(2, "0")) | |
| 25 | .join("") | |
| 26 | ); | |
| 27 | } | |
| 28 | ||
| 29 | async function hashToken(token: string): Promise<string> { | |
| 30 | const data = new TextEncoder().encode(token); | |
| 31 | const hash = await crypto.subtle.digest("SHA-256", data); | |
| 32 | return Array.from(new Uint8Array(hash)) | |
| 33 | .map((b) => b.toString(16).padStart(2, "0")) | |
| 34 | .join(""); | |
| 35 | } | |
| 36 | ||
| 37 | async function main() { | |
| 9d06598 | 38 | const url = process.env.DATABASE_URL; |
| 39 | if (!url) { | |
| 40 | console.error("DATABASE_URL is not set."); | |
| 41 | process.exit(1); | |
| 42 | } | |
| 43 | const sql = neon(url); | |
| 44 | ||
| 0dc3291 | 45 | const requestedUser = process.env.EMERGENCY_PAT_USER?.trim(); |
| 46 | ||
| 9d06598 | 47 | let userRow: { id: string; username: string } | undefined; |
| 0dc3291 | 48 | if (requestedUser) { |
| 9d06598 | 49 | const rows = (await sql` |
| 50 | SELECT id, username FROM users WHERE username = ${requestedUser} LIMIT 1 | |
| 51 | `) as Array<{ id: string; username: string }>; | |
| 52 | userRow = rows[0]; | |
| 53 | if (!userRow) { | |
| 0dc3291 | 54 | console.error(`No user with username "${requestedUser}".`); |
| 55 | process.exit(1); | |
| 56 | } | |
| 57 | } else { | |
| 9d06598 | 58 | const rows = (await sql` |
| 59 | SELECT id, username FROM users WHERE is_admin = true ORDER BY created_at ASC LIMIT 1 | |
| 60 | `) as Array<{ id: string; username: string }>; | |
| 61 | userRow = rows[0]; | |
| 62 | if (!userRow) { | |
| 0dc3291 | 63 | console.error( |
| 64 | "No admin user found. Set EMERGENCY_PAT_USER=<username> and rerun." | |
| 65 | ); | |
| 66 | process.exit(1); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | const token = generateToken(); | |
| 71 | const tokenHash = await hashToken(token); | |
| 9d06598 | 72 | const tokenPrefix = token.slice(0, 12); |
| 0dc3291 | 73 | |
| 9d06598 | 74 | await sql` |
| 75 | INSERT INTO api_tokens (user_id, name, token_hash, token_prefix, scopes) | |
| 76 | VALUES (${userRow.id}, 'emergency-deploy-fix', ${tokenHash}, ${tokenPrefix}, 'admin') | |
| 77 | `; | |
| 78 | ||
| 79 | console.log(""); | |
| 80 | console.log(`User: ${userRow.username}`); | |
| 0dc3291 | 81 | console.log(`Token: ${token}`); |
| 82 | console.log(""); | |
| 9d06598 | 83 | console.log("Copy the token NOW (only shown once). Then run these two:"); |
| 0dc3291 | 84 | console.log(""); |
| 9d06598 | 85 | console.log(` git remote set-url gluecron "https://${userRow.username}:${token}@gluecron.com/ccantynz/Gluecron.com.git"`); |
| 0dc3291 | 86 | console.log(" git push gluecron main"); |
| 87 | process.exit(0); | |
| 88 | } | |
| 89 | ||
| 90 | main().catch((err) => { | |
| 91 | console.error("Failed:", err); | |
| 92 | process.exit(1); | |
| 93 | }); |