feat: set up development database and test cleanup utilities
This commit is contained in:
@@ -51,6 +51,22 @@ function createDatabase() {
|
||||
}
|
||||
}
|
||||
|
||||
function dropDatabase() {
|
||||
try {
|
||||
const dbUrl = process.env.DATABASE_URL;
|
||||
const dbName = dbUrl.split('/').pop();
|
||||
|
||||
// Drop existing connections and database
|
||||
execSync(`psql "${dbUrl}" -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${dbName}' AND pid <> pg_backend_pid();" 2>/dev/null || true`, { stdio: 'pipe' });
|
||||
execSync(`psql "${dbUrl}" -c "DROP DATABASE IF EXISTS ${dbName};" 2>/dev/null || true`, { stdio: 'pipe' });
|
||||
console.log(`✅ Database "${dbName}" dropped`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to drop database:', error.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function runMigrations() {
|
||||
try {
|
||||
console.log('🔄 Running migrations...');
|
||||
@@ -76,6 +92,9 @@ function generatePrismaClient() {
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const shouldDrop = args.includes('--drop');
|
||||
|
||||
console.log('=== PostgreSQL Setup for EuchreCamp ===\n');
|
||||
|
||||
// Check if DATABASE_URL is set
|
||||
@@ -90,6 +109,14 @@ function main() {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Drop database if requested
|
||||
if (shouldDrop) {
|
||||
console.log('Dropping existing database...');
|
||||
if (!dropDatabase()) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Create database
|
||||
if (!createDatabase()) {
|
||||
process.exit(1);
|
||||
|
||||
Executable
+49
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Switch to development database
|
||||
* Creates a .env.development.local file with development database settings
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
|
||||
const envDevPath = path.join(__dirname, '..', '.env.development');
|
||||
const envDevLocalPath = path.join(__dirname, '..', '.env.development.local');
|
||||
|
||||
console.log('🔧 Setting up development database...\n');
|
||||
|
||||
// Check if .env.development exists
|
||||
if (!fs.existsSync(envDevPath)) {
|
||||
console.error('❌ .env.development file not found');
|
||||
console.error('Please create it first or run: npm run db:setup-dev');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Copy .env.development to .env.development.local if it doesn't exist
|
||||
if (!fs.existsSync(envDevLocalPath)) {
|
||||
fs.copyFileSync(envDevPath, envDevLocalPath);
|
||||
console.log('✅ Created .env.development.local');
|
||||
} else {
|
||||
console.log('ℹ️ .env.development.local already exists');
|
||||
}
|
||||
|
||||
// Set NODE_ENV for the current session
|
||||
process.env.NODE_ENV = 'development';
|
||||
process.env.DATABASE_PROVIDER = 'postgresql';
|
||||
|
||||
// Read the development database URL
|
||||
const envContent = fs.readFileSync(envDevPath, 'utf8');
|
||||
const match = envContent.match(/DATABASE_URL="([^"]+)"/);
|
||||
if (match) {
|
||||
process.env.DATABASE_URL = match[1];
|
||||
console.log(`✅ Development database URL: ${process.env.DATABASE_URL}`);
|
||||
}
|
||||
|
||||
console.log('\n✅ Development database configured!');
|
||||
console.log('\nNext steps:');
|
||||
console.log('1. Setup the dev database: npm run db:setup-dev');
|
||||
console.log('2. Start development server: npm run dev');
|
||||
console.log('\nNote: This script sets environment variables for the current session.');
|
||||
console.log('For persistent configuration, use .env.development.local');
|
||||
Reference in New Issue
Block a user