114 lines
3.1 KiB
JavaScript
Executable File
114 lines
3.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Script to set up PostgreSQL database for EuchreCamp
|
|
* This script creates the database and runs migrations
|
|
*/
|
|
|
|
const { execSync } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
function checkPostgresConnection() {
|
|
try {
|
|
const dbUrl = process.env.DATABASE_URL;
|
|
if (!dbUrl || !dbUrl.startsWith('postgresql://')) {
|
|
console.error('❌ DATABASE_URL is not set for PostgreSQL');
|
|
console.error('Please update your .env file with:');
|
|
console.error('DATABASE_PROVIDER=postgresql');
|
|
console.error('DATABASE_URL="postgresql://username:password@localhost:5432/euchre_camp"');
|
|
return false;
|
|
}
|
|
|
|
// Try to connect using psql
|
|
execSync(`psql "${dbUrl}" -c "SELECT 1"`, { stdio: 'pipe' });
|
|
console.log('✅ PostgreSQL connection successful');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ PostgreSQL connection failed:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function createDatabase() {
|
|
try {
|
|
const dbUrl = process.env.DATABASE_URL;
|
|
const dbName = dbUrl.split('/').pop();
|
|
|
|
// Create database if it doesn't exist
|
|
execSync(`psql "${dbUrl}" -c "CREATE DATABASE ${dbName};" 2>/dev/null || true`, { stdio: 'pipe' });
|
|
console.log(`✅ Database "${dbName}" ready`);
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ Failed to create database:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function runMigrations() {
|
|
try {
|
|
console.log('🔄 Running migrations...');
|
|
execSync('npx prisma migrate deploy', { stdio: 'inherit' });
|
|
console.log('✅ Migrations completed successfully');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ Migration failed:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function generatePrismaClient() {
|
|
try {
|
|
console.log('🔄 Generating Prisma client...');
|
|
execSync('npx prisma generate', { stdio: 'inherit' });
|
|
console.log('✅ Prisma client generated successfully');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('❌ Prisma client generation failed:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function main() {
|
|
console.log('=== PostgreSQL Setup for EuchreCamp ===\n');
|
|
|
|
// Check if DATABASE_URL is set
|
|
if (!process.env.DATABASE_URL) {
|
|
console.error('❌ DATABASE_URL environment variable is not set');
|
|
console.error('Please add it to your .env file');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Check PostgreSQL connection
|
|
if (!checkPostgresConnection()) {
|
|
process.exit(1);
|
|
}
|
|
|
|
// Create database
|
|
if (!createDatabase()) {
|
|
process.exit(1);
|
|
}
|
|
|
|
// Generate Prisma client
|
|
if (!generatePrismaClient()) {
|
|
process.exit(1);
|
|
}
|
|
|
|
// Run migrations
|
|
if (!runMigrations()) {
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('\n✅ PostgreSQL setup completed successfully!');
|
|
console.log('\nNext steps:');
|
|
console.log('1. Start your development server: npm run dev');
|
|
console.log('2. Test the connection by visiting http://localhost:3000');
|
|
}
|
|
|
|
// Run if called directly
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
module.exports = { checkPostgresConnection, createDatabase, runMigrations, generatePrismaClient };
|