deckhearth/scripts/setup-neon-db.js
Randall Stillwell 1cc2e28423 Migrate Deck Hearth off Vercel/Neon to homelab Dokploy stack.
Replace @vercel/postgres, Blob, and Upstash with lib/sql.js, MinIO object
storage, and CT 102 Redis rate limits. Add Dockerfile for Dokploy deploy,
homelab runbooks, Neon data-copy helper, and point CI smoke/visual at the
homelab URL instead of Vercel previews.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-15 09:32:13 -05:00

116 lines
3.9 KiB
JavaScript

#!/usr/bin/env node
/**
* First-time / re-onboarding setup for the Deck Hearth Postgres database.
*
* Pipeline:
* 1. Validate `ADMIN_INITIAL_PASSWORD` is set (fail loud BEFORE touching the DB).
* 2. Spawn `npm run migrate up` (uses POSTGRES_URL_DIRECT) to apply migrations.
* 3. Seed the admin user with `ON CONFLICT (email) DO NOTHING`.
*
* Env:
* POSTGRES_URL / POSTGRES_URL_DIRECT — homelab:
* postgresql://deckhearth:…@192.168.68.102:5432/deckhearth
* See docs/HOMELAB_DATABASE.md
*
* Legacy script name kept as setup-neon-db.js until a follow-up rename lands.
*/
import dotenv from 'dotenv';
dotenv.config({ path: '.env.local' });
import { spawn } from 'node:child_process';
import { sql } from '../lib/sql.js';
import bcrypt from 'bcryptjs';
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.'
)
);
}
});
});
}
async function setupNeonDatabase() {
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);
}
if (!process.env.POSTGRES_URL && !process.env.POSTGRES_URL_DIRECT) {
console.error(
'❌ POSTGRES_URL or POSTGRES_URL_DIRECT must be set.\n' +
' Use POSTGRES_URL (homelab CT 102 deckhearth URL).\n' +
' See docs/HOMELAB_DATABASE.md\n'
);
process.exit(1);
}
if (!process.env.POSTGRES_URL_DIRECT) {
process.env.POSTGRES_URL_DIRECT = process.env.POSTGRES_URL;
}
if (!process.env.POSTGRES_URL) {
process.env.POSTGRES_URL = process.env.POSTGRES_URL_DIRECT;
}
try {
await runMigrations();
console.log('✅ Connecting to Postgres to seed admin user...');
const hashedPassword = await bcrypt.hash(adminPassword, 12);
await sql`
INSERT INTO users (email, password, role)
VALUES (${'admin@deckhearth.com'}, ${hashedPassword}, ${'admin'})
ON CONFLICT (email) DO NOTHING
`;
console.log('✅ Admin user ready (email: admin@deckhearth.com)');
console.log('🎉 Neon database setup completed successfully!');
console.log('');
console.log('📋 Database Details:');
console.log(' Database: Neon PostgreSQL');
console.log(' Schema: applied via node-pg-migrate (see migrations/)');
console.log(' Admin user ready (email: admin@deckhearth.com)');
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');
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');
process.exit(1);
}
}
setupNeonDatabase();