deckhearth/scripts/migrations/2026-05-24-rename-admin-email.js

90 lines
2.8 KiB
JavaScript
Raw Normal View History

feat(brand): unify on Deck Hearth across in-repo strings + infra (P1 brand decision) Resolves the launch-blocking 'TCG Vault vs Deck Hearth' inconsistency called out in AGENTS.md line 5 since project setup. Operator gate-0 decision: Deck Hearth wins. Two briefs applied serially. B1 (mechanical): 7-file display + comment sweep. B2 (infrastructure): Redis prefix rename in lib/rate-limit.js (5 prefixes, accept one-time counter reset), package.json + lockfile regen (STOP-on-churn confirmed only name lines changed), admin/alice/bob email rename in seed scripts + login pre-fill + NEW idempotent migration script scripts/migrations/2026-05-24-rename-admin-email.js. Risk 4 PRESERVE applied: test/lib/permission-middleware.test.js retains admin@tcgvault.com literal with 7-line architect-authored why comment (documents pre-fix-auth-bypass bug shape; preserves historical truth per project's gotcha-documentation convention). All 5 D-decisions ratified at gate-1 (Deck Hearth / deck-hearth / deckhearth / admin@deckhearth.com / full deckhearth Redis prefix). Local: lint 128 baseline (B1 + B2), vitest 21/21 (B1 + B2). CI all green: Playwright smoke 3/3 against rebranded preview in 1m4s, forbidden-cors-headers pass, forbidden-endpoints pass, Screenshot diff pass, Vercel deployment complete. Cross-validation lineage: 4th convoy where the same 3-test smoke spec defends auth surface through sweeping change (after PR #15 Layout default-user, PR #19 CORS, PR #20 rate-limit, now this PR #21 brand rename). OPERATOR POST-MERGE ACTION REQUIRED: run 'node scripts/migrations/2026-05-24-rename-admin-email.js' against prod Neon DB before next admin login (ordering: migration FIRST, then any subsequent setup-db invocation). Migration is ESM, idempotent, UNIQUE-collision-safe. PR #21 architect-commit 50ce9ab, B1 ac8c998, B2 1c18d21.
2026-05-25 03:28:29 -04:00
#!/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);
fix(migration): neon() returns rows array directly, not { rows, rowCount } The pick-a-name B2 migration script (shipped 2026-05-24 in `9abbab6`) was authored against the @vercel/postgres return shape `{ rows: [...], rowCount: N }` but uses @neondatabase/serverless's `neon()` tagged template, which returns the rows array DIRECTLY. As shipped, the first query at line 37 produced `before === undefined` and crashed at the `before.length` check on line 44 with `TypeError: Cannot read properties of undefined (reading 'length')`. Verified pattern from `lib/database.js` line 33: the wrapper adapter explicitly checks `Array.isArray(result)` because `neon()` returns the array directly. AGENTS.md Gotcha #1 mentions @neondatabase and @vercel/postgres run in parallel; this is exactly the kind of cross- contamination that creates. Fix: drop the `{ rows: x }` destructuring in all 3 sites (lines 37, 61, 73) and assign the result directly. Added a 4-line "why" comment block above the first site so the next person to write a migration script doesn't make the same mistake. Hand-verified post-fix: - node --check: exit 0 - Live run against prod Neon DB: migrated 3 users (admin id=1, alice id=5, bob id=6) from @tcgvault.com → @deckhearth.com. Idempotent re-run prints "Nothing to migrate." - No data loss; the broken first attempt didn't reach the UPDATE statement (crashed before line 54), so prod was unchanged. Operator action satisfied by this fix: - The `pick-a-name` PR #21 / squash `9abbab6` post-merge operator action ("run the migration script before next admin login") is now complete in prod. Admin login uses `admin@deckhearth.com`. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-25 12:25:43 -04:00
// 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`
feat(brand): unify on Deck Hearth across in-repo strings + infra (P1 brand decision) Resolves the launch-blocking 'TCG Vault vs Deck Hearth' inconsistency called out in AGENTS.md line 5 since project setup. Operator gate-0 decision: Deck Hearth wins. Two briefs applied serially. B1 (mechanical): 7-file display + comment sweep. B2 (infrastructure): Redis prefix rename in lib/rate-limit.js (5 prefixes, accept one-time counter reset), package.json + lockfile regen (STOP-on-churn confirmed only name lines changed), admin/alice/bob email rename in seed scripts + login pre-fill + NEW idempotent migration script scripts/migrations/2026-05-24-rename-admin-email.js. Risk 4 PRESERVE applied: test/lib/permission-middleware.test.js retains admin@tcgvault.com literal with 7-line architect-authored why comment (documents pre-fix-auth-bypass bug shape; preserves historical truth per project's gotcha-documentation convention). All 5 D-decisions ratified at gate-1 (Deck Hearth / deck-hearth / deckhearth / admin@deckhearth.com / full deckhearth Redis prefix). Local: lint 128 baseline (B1 + B2), vitest 21/21 (B1 + B2). CI all green: Playwright smoke 3/3 against rebranded preview in 1m4s, forbidden-cors-headers pass, forbidden-endpoints pass, Screenshot diff pass, Vercel deployment complete. Cross-validation lineage: 4th convoy where the same 3-test smoke spec defends auth surface through sweeping change (after PR #15 Layout default-user, PR #19 CORS, PR #20 rate-limit, now this PR #21 brand rename). OPERATOR POST-MERGE ACTION REQUIRED: run 'node scripts/migrations/2026-05-24-rename-admin-email.js' against prod Neon DB before next admin login (ordering: migration FIRST, then any subsequent setup-db invocation). Migration is ESM, idempotent, UNIQUE-collision-safe. PR #21 architect-commit 50ce9ab, B1 ac8c998, B2 1c18d21.
2026-05-25 03:28:29 -04:00
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'
`;
fix(migration): neon() returns rows array directly, not { rows, rowCount } The pick-a-name B2 migration script (shipped 2026-05-24 in `9abbab6`) was authored against the @vercel/postgres return shape `{ rows: [...], rowCount: N }` but uses @neondatabase/serverless's `neon()` tagged template, which returns the rows array DIRECTLY. As shipped, the first query at line 37 produced `before === undefined` and crashed at the `before.length` check on line 44 with `TypeError: Cannot read properties of undefined (reading 'length')`. Verified pattern from `lib/database.js` line 33: the wrapper adapter explicitly checks `Array.isArray(result)` because `neon()` returns the array directly. AGENTS.md Gotcha #1 mentions @neondatabase and @vercel/postgres run in parallel; this is exactly the kind of cross- contamination that creates. Fix: drop the `{ rows: x }` destructuring in all 3 sites (lines 37, 61, 73) and assign the result directly. Added a 4-line "why" comment block above the first site so the next person to write a migration script doesn't make the same mistake. Hand-verified post-fix: - node --check: exit 0 - Live run against prod Neon DB: migrated 3 users (admin id=1, alice id=5, bob id=6) from @tcgvault.com → @deckhearth.com. Idempotent re-run prints "Nothing to migrate." - No data loss; the broken first attempt didn't reach the UPDATE statement (crashed before line 54), so prod was unchanged. Operator action satisfied by this fix: - The `pick-a-name` PR #21 / squash `9abbab6` post-merge operator action ("run the migration script before next admin login") is now complete in prod. Admin login uses `admin@deckhearth.com`. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-25 12:25:43 -04:00
const after = await sql`
feat(brand): unify on Deck Hearth across in-repo strings + infra (P1 brand decision) Resolves the launch-blocking 'TCG Vault vs Deck Hearth' inconsistency called out in AGENTS.md line 5 since project setup. Operator gate-0 decision: Deck Hearth wins. Two briefs applied serially. B1 (mechanical): 7-file display + comment sweep. B2 (infrastructure): Redis prefix rename in lib/rate-limit.js (5 prefixes, accept one-time counter reset), package.json + lockfile regen (STOP-on-churn confirmed only name lines changed), admin/alice/bob email rename in seed scripts + login pre-fill + NEW idempotent migration script scripts/migrations/2026-05-24-rename-admin-email.js. Risk 4 PRESERVE applied: test/lib/permission-middleware.test.js retains admin@tcgvault.com literal with 7-line architect-authored why comment (documents pre-fix-auth-bypass bug shape; preserves historical truth per project's gotcha-documentation convention). All 5 D-decisions ratified at gate-1 (Deck Hearth / deck-hearth / deckhearth / admin@deckhearth.com / full deckhearth Redis prefix). Local: lint 128 baseline (B1 + B2), vitest 21/21 (B1 + B2). CI all green: Playwright smoke 3/3 against rebranded preview in 1m4s, forbidden-cors-headers pass, forbidden-endpoints pass, Screenshot diff pass, Vercel deployment complete. Cross-validation lineage: 4th convoy where the same 3-test smoke spec defends auth surface through sweeping change (after PR #15 Layout default-user, PR #19 CORS, PR #20 rate-limit, now this PR #21 brand rename). OPERATOR POST-MERGE ACTION REQUIRED: run 'node scripts/migrations/2026-05-24-rename-admin-email.js' against prod Neon DB before next admin login (ordering: migration FIRST, then any subsequent setup-db invocation). Migration is ESM, idempotent, UNIQUE-collision-safe. PR #21 architect-commit 50ce9ab, B1 ac8c998, B2 1c18d21.
2026-05-25 03:28:29 -04:00
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}`);
}
fix(migration): neon() returns rows array directly, not { rows, rowCount } The pick-a-name B2 migration script (shipped 2026-05-24 in `9abbab6`) was authored against the @vercel/postgres return shape `{ rows: [...], rowCount: N }` but uses @neondatabase/serverless's `neon()` tagged template, which returns the rows array DIRECTLY. As shipped, the first query at line 37 produced `before === undefined` and crashed at the `before.length` check on line 44 with `TypeError: Cannot read properties of undefined (reading 'length')`. Verified pattern from `lib/database.js` line 33: the wrapper adapter explicitly checks `Array.isArray(result)` because `neon()` returns the array directly. AGENTS.md Gotcha #1 mentions @neondatabase and @vercel/postgres run in parallel; this is exactly the kind of cross- contamination that creates. Fix: drop the `{ rows: x }` destructuring in all 3 sites (lines 37, 61, 73) and assign the result directly. Added a 4-line "why" comment block above the first site so the next person to write a migration script doesn't make the same mistake. Hand-verified post-fix: - node --check: exit 0 - Live run against prod Neon DB: migrated 3 users (admin id=1, alice id=5, bob id=6) from @tcgvault.com → @deckhearth.com. Idempotent re-run prints "Nothing to migrate." - No data loss; the broken first attempt didn't reach the UPDATE statement (crashed before line 54), so prod was unchanged. Operator action satisfied by this fix: - The `pick-a-name` PR #21 / squash `9abbab6` post-merge operator action ("run the migration script before next admin login") is now complete in prod. Admin login uses `admin@deckhearth.com`. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-25 12:25:43 -04:00
const stragglers = await sql`
feat(brand): unify on Deck Hearth across in-repo strings + infra (P1 brand decision) Resolves the launch-blocking 'TCG Vault vs Deck Hearth' inconsistency called out in AGENTS.md line 5 since project setup. Operator gate-0 decision: Deck Hearth wins. Two briefs applied serially. B1 (mechanical): 7-file display + comment sweep. B2 (infrastructure): Redis prefix rename in lib/rate-limit.js (5 prefixes, accept one-time counter reset), package.json + lockfile regen (STOP-on-churn confirmed only name lines changed), admin/alice/bob email rename in seed scripts + login pre-fill + NEW idempotent migration script scripts/migrations/2026-05-24-rename-admin-email.js. Risk 4 PRESERVE applied: test/lib/permission-middleware.test.js retains admin@tcgvault.com literal with 7-line architect-authored why comment (documents pre-fix-auth-bypass bug shape; preserves historical truth per project's gotcha-documentation convention). All 5 D-decisions ratified at gate-1 (Deck Hearth / deck-hearth / deckhearth / admin@deckhearth.com / full deckhearth Redis prefix). Local: lint 128 baseline (B1 + B2), vitest 21/21 (B1 + B2). CI all green: Playwright smoke 3/3 against rebranded preview in 1m4s, forbidden-cors-headers pass, forbidden-endpoints pass, Screenshot diff pass, Vercel deployment complete. Cross-validation lineage: 4th convoy where the same 3-test smoke spec defends auth surface through sweeping change (after PR #15 Layout default-user, PR #19 CORS, PR #20 rate-limit, now this PR #21 brand rename). OPERATOR POST-MERGE ACTION REQUIRED: run 'node scripts/migrations/2026-05-24-rename-admin-email.js' against prod Neon DB before next admin login (ordering: migration FIRST, then any subsequent setup-db invocation). Migration is ESM, idempotent, UNIQUE-collision-safe. PR #21 architect-commit 50ce9ab, B1 ac8c998, B2 1c18d21.
2026-05-25 03:28:29 -04:00
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);
});