Hotfix to scripts/migrations/2026-05-24-rename-admin-email.js (shipped 2026-05-24 in pick-a-name PR #21). Script crashed on first invocation with 'TypeError: Cannot read properties of undefined (reading length)' at line 44. Root cause: architect designed against @vercel/postgres return shape { rows, rowCount } but the script uses @neondatabase/serverless's neon() tagged template which returns the rows array directly. AGENTS.md Gotcha #1 (two SQL clients in parallel) is exactly this kind of cross-contamination. Fix: drop the { rows: x } destructuring in all 3 sites + add a 4-line why comment block above the first site so the next migration author doesn't repeat. Verified hand-run against prod Neon DB: migrated 3 users (admin id=1, alice id=5, bob id=6) from @tcgvault.com to @deckhearth.com; idempotent re-run prints 'Nothing to migrate.' No data risk on the original crash — script exited at line 44 before reaching the UPDATE at line 54. PR #21 operator action item now complete in prod. Surfaces a P3 follow-up: add-neon-return-shape-rule (or fold into single-sql-client). All CI green: lint 128 baseline, vitest 21/21, Playwright smoke 3/3 in 1m2s, forbidden-cors-headers pass, forbidden-endpoints pass. PR #24, commit 98406fa.
89 lines
2.8 KiB
JavaScript
89 lines
2.8 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Migration: 2026-05-24 — Rename @tcgvault.com user emails to @deckhearth.com
|
|
*
|
|
* Part of the `pick-a-name` convoy. Renames every `users.email` row matching
|
|
* `%@tcgvault.com` to the `@deckhearth.com` equivalent (admin + alice + bob,
|
|
* plus any other accidentally-`@tcgvault.com` users if they exist).
|
|
*
|
|
* Idempotent: re-running after the first run prints "Nothing to migrate."
|
|
*
|
|
* Usage:
|
|
* node scripts/migrations/2026-05-24-rename-admin-email.js
|
|
*
|
|
* Required env: POSTGRES_URL (read from .env.local).
|
|
*
|
|
* Safety: the UPDATE uses REPLACE() so emails like `admin@tcgvault.com`
|
|
* become `admin@deckhearth.com`. The `users.email` UNIQUE constraint will
|
|
* fail loudly if a row with the target email already exists — which is the
|
|
* correct behavior (do NOT silently overwrite). If you see the constraint
|
|
* violation, inspect the DB manually before retrying.
|
|
*/
|
|
|
|
import dotenv from 'dotenv';
|
|
dotenv.config({ path: '.env.local' });
|
|
|
|
import { neon } from '@neondatabase/serverless';
|
|
|
|
async function main() {
|
|
if (!process.env.POSTGRES_URL) {
|
|
console.error('❌ POSTGRES_URL is not set. Set it in .env.local before running this migration.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const sql = neon(process.env.POSTGRES_URL);
|
|
|
|
// NOTE: @neondatabase/serverless's tagged-template returns the rows array
|
|
// directly — NOT wrapped in { rows, rowCount } like @vercel/postgres does.
|
|
// See lib/database.js line 33 for the same finding. Do NOT destructure
|
|
// `{ rows: x }` from a `neon()` result; assign the result directly.
|
|
const before = await sql`
|
|
SELECT id, email, role
|
|
FROM users
|
|
WHERE email LIKE '%@tcgvault.com'
|
|
ORDER BY id
|
|
`;
|
|
|
|
if (before.length === 0) {
|
|
console.log('✅ Nothing to migrate. No users with @tcgvault.com emails found.');
|
|
return;
|
|
}
|
|
|
|
console.log(`Found ${before.length} user(s) with @tcgvault.com emails:`);
|
|
for (const r of before) {
|
|
console.log(` id=${r.id} role=${r.role} email=${r.email}`);
|
|
}
|
|
|
|
await sql`
|
|
UPDATE users
|
|
SET email = REPLACE(email, '@tcgvault.com', '@deckhearth.com'),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
WHERE email LIKE '%@tcgvault.com'
|
|
`;
|
|
|
|
const after = await sql`
|
|
SELECT id, email, role
|
|
FROM users
|
|
WHERE email LIKE '%@deckhearth.com'
|
|
ORDER BY id
|
|
`;
|
|
|
|
console.log(`✅ Migrated ${before.length} user(s). Post-migration @deckhearth.com rows:`);
|
|
for (const r of after) {
|
|
console.log(` id=${r.id} role=${r.role} email=${r.email}`);
|
|
}
|
|
|
|
const stragglers = await sql`
|
|
SELECT COUNT(*)::int AS count FROM users WHERE email LIKE '%@tcgvault.com'
|
|
`;
|
|
if (stragglers[0].count !== 0) {
|
|
console.warn(`⚠️ ${stragglers[0].count} @tcgvault.com row(s) still present after migration — investigate.`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error('❌ Migration failed:', err);
|
|
process.exit(1);
|
|
});
|