Commitc36e102unknown_key
fix(scripts): emergency-pat — list users on lookup miss + case-insensitive match
fix(scripts): emergency-pat — list users on lookup miss + case-insensitive match Two improvements when the operator's PAT lookup fails: 1. Match usernames case-insensitively (LOWER both sides). Some early signups stored mixed-case usernames; the login endpoint also does case-insensitive comparison, so the script should too. 2. When no user matches, dump the first 30 users so the operator can see what's actually in the DB and rerun with the right name. Avoids the dead-end "No user with username X" with no info on what IS available. https://claude.ai/code/session_01QFLWDxWw65DX6enMcS5Lwe
1 file changed+27−8c36e102f69baa69790f134247d860ee9fa38e819
1 changed file+27−8
Modifiedscripts/emergency-pat.ts+27−8View fileUnifiedSplit
@@ -46,25 +46,44 @@ async function main() {
4646
4747 let userRow: { id: string; username: string } | undefined;
4848 if (requestedUser) {
49 // Case-insensitive lookup — username is supposed to be unique-by-value
50 // but capitalization in some early signups varied.
4951 const rows = (await sql`
50 SELECT id, username FROM users WHERE username = ${requestedUser} LIMIT 1
52 SELECT id, username FROM users WHERE LOWER(username) = LOWER(${requestedUser}) LIMIT 1
5153 `) as Array<{ id: string; username: string }>;
5254 userRow = rows[0];
53 if (!userRow) {
54 console.error(`No user with username "${requestedUser}".`);
55 process.exit(1);
56 }
5755 } else {
5856 const rows = (await sql`
5957 SELECT id, username FROM users WHERE is_admin = true ORDER BY created_at ASC LIMIT 1
6058 `) as Array<{ id: string; username: string }>;
6159 userRow = rows[0];
62 if (!userRow) {
60 }
61
62 if (!userRow) {
63 // Dump the user list so the operator can see what's there.
64 const all = (await sql`
65 SELECT username, email, is_admin, created_at
66 FROM users ORDER BY created_at ASC LIMIT 30
67 `) as Array<{
68 username: string; email: string; is_admin: boolean; created_at: Date;
69 }>;
70 console.error("");
71 console.error(
72 requestedUser
73 ? `No user matched "${requestedUser}". Users in this DB:`
74 : "No admin user. All users in this DB:"
75 );
76 console.error("");
77 for (const u of all) {
6378 console.error(
64 "No admin user found. Set EMERGENCY_PAT_USER=<username> and rerun."
79 ` ${u.username.padEnd(24)} ${u.email.padEnd(40)} admin=${u.is_admin}`
6580 );
66 process.exit(1);
6781 }
82 console.error("");
83 console.error(
84 "Re-run with EMERGENCY_PAT_USER=<exact-username-from-above>"
85 );
86 process.exit(1);
6887 }
6988
7089 const token = generateToken();
7190