2025-07-23 22:26:54 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
/**
|
2026-05-27 00:01:58 -04:00
|
|
|
* First-time / re-onboarding setup for the Deck Hearth Neon database.
|
|
|
|
|
*
|
|
|
|
|
* Pipeline (post-`migration-tool` convoy, 2026-05-26):
|
|
|
|
|
* 1. Validate `ADMIN_INITIAL_PASSWORD` is set (fail loud BEFORE touching the DB).
|
|
|
|
|
* 2. Spawn `npm run migrate up` to apply every pending migration under
|
|
|
|
|
* `migrations/`. The initial backfill migration (1779853647564_initial-schema)
|
|
|
|
|
* uses `CREATE TABLE IF NOT EXISTS` and is idempotent against fresh or
|
|
|
|
|
* pre-existing envs.
|
|
|
|
|
* 3. Seed the admin user with `ON CONFLICT (email) DO NOTHING`.
|
|
|
|
|
*
|
|
|
|
|
* Make sure you have `POSTGRES_URL` set in `.env.local`. See README §
|
|
|
|
|
* "First-time admin setup" for the operator runbook.
|
2025-07-23 22:26:54 -04:00
|
|
|
*/
|
|
|
|
|
|
2026-05-23 16:07:25 -04:00
|
|
|
import dotenv from 'dotenv';
|
|
|
|
|
dotenv.config({ path: '.env.local' });
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2026-05-27 00:01:58 -04:00
|
|
|
import { spawn } from 'node:child_process';
|
2026-06-02 01:37:03 -04:00
|
|
|
import { sql } from '@vercel/postgres';
|
2026-05-23 16:07:25 -04:00
|
|
|
import bcrypt from 'bcryptjs';
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2026-05-27 00:01:58 -04:00
|
|
|
function runMigrations() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
console.log('✅ Running migrations (npm run migrate up)...');
|
|
|
|
|
const child = spawn('npm', ['run', 'migrate', '--', 'up'], {
|
|
|
|
|
stdio: 'inherit',
|
|
|
|
|
shell: false,
|
|
|
|
|
});
|
|
|
|
|
child.on('error', (err) => reject(err));
|
|
|
|
|
child.on('exit', (code, signal) => {
|
|
|
|
|
if (code === 0) {
|
|
|
|
|
resolve();
|
|
|
|
|
} else {
|
|
|
|
|
reject(
|
|
|
|
|
new Error(
|
|
|
|
|
`npm run migrate up exited with code=${code} signal=${signal}. ` +
|
|
|
|
|
'See output above for the failing migration.'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 22:26:54 -04:00
|
|
|
async function setupNeonDatabase() {
|
2026-05-23 15:57:31 -04:00
|
|
|
const adminPassword = process.env.ADMIN_INITIAL_PASSWORD;
|
|
|
|
|
if (!adminPassword || !adminPassword.trim()) {
|
|
|
|
|
console.error(
|
|
|
|
|
'❌ ADMIN_INITIAL_PASSWORD environment variable is not set.\n' +
|
|
|
|
|
'\n' +
|
|
|
|
|
' Set it in .env.local for local dev, or as a CI secret if you run setup from CI.\n' +
|
|
|
|
|
' Generate a strong password with: openssl rand -base64 24\n' +
|
|
|
|
|
' See README.md → "First-time admin setup" for the full flow.\n'
|
|
|
|
|
);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 00:01:58 -04:00
|
|
|
if (!process.env.POSTGRES_URL) {
|
|
|
|
|
console.error(
|
|
|
|
|
'❌ POSTGRES_URL environment variable is not set.\n' +
|
|
|
|
|
' Set it in .env.local (Neon connection string) before running setup.\n'
|
|
|
|
|
);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
try {
|
2026-05-27 00:01:58 -04:00
|
|
|
await runMigrations();
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2026-05-27 00:01:58 -04:00
|
|
|
console.log('✅ Connecting to Neon database to seed admin user...');
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2026-05-23 15:57:31 -04:00
|
|
|
const hashedPassword = await bcrypt.hash(adminPassword, 12);
|
2026-05-27 00:01:58 -04:00
|
|
|
|
2025-07-23 22:26:54 -04:00
|
|
|
await sql`
|
2026-05-27 00:01:58 -04:00
|
|
|
INSERT INTO users (email, password, role)
|
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
|
|
|
VALUES (${'admin@deckhearth.com'}, ${hashedPassword}, ${'admin'})
|
2025-07-23 22:26:54 -04:00
|
|
|
ON CONFLICT (email) DO NOTHING
|
|
|
|
|
`;
|
2026-05-27 00:01:58 -04:00
|
|
|
console.log('✅ Admin user ready (email: admin@deckhearth.com)');
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
console.log('🎉 Neon database setup completed successfully!');
|
|
|
|
|
console.log('');
|
|
|
|
|
console.log('📋 Database Details:');
|
|
|
|
|
console.log(' Database: Neon PostgreSQL');
|
2026-05-27 00:01:58 -04:00
|
|
|
console.log(' Schema: applied via node-pg-migrate (see migrations/)');
|
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
|
|
|
console.log(' Admin user ready (email: admin@deckhearth.com)');
|
2025-07-23 22:26:54 -04:00
|
|
|
console.log('');
|
|
|
|
|
console.log('🔧 Next Steps:');
|
|
|
|
|
console.log(' 1. Test the API endpoints');
|
|
|
|
|
console.log(' 2. Start building the frontend');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('❌ Database setup failed:', error.message);
|
|
|
|
|
console.log('');
|
|
|
|
|
console.log('🔧 Troubleshooting:');
|
|
|
|
|
console.log(' 1. Make sure POSTGRES_URL is set in .env.local');
|
2026-05-27 00:01:58 -04:00
|
|
|
console.log(' 2. Make sure ADMIN_INITIAL_PASSWORD is set in .env.local');
|
|
|
|
|
console.log(' 3. Check your Neon database connection');
|
|
|
|
|
console.log(' 4. If the migrate step failed, inspect the SQL above and');
|
|
|
|
|
console.log(' see migrations/ for the failing file. To re-try just the');
|
|
|
|
|
console.log(' migration step run: npm run migrate up');
|
2025-07-23 22:26:54 -04:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 00:01:58 -04:00
|
|
|
setupNeonDatabase();
|