81 lines
2.4 KiB
JavaScript
Executable File
81 lines
2.4 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Script to switch between SQLite and PostgreSQL databases
|
|
* Usage: node scripts/switch-database.js [sqlite|postgres]
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const envFile = '.env';
|
|
const envExampleFile = '.env.example';
|
|
|
|
function updateEnvFile(provider) {
|
|
const envPath = path.join(process.cwd(), envFile);
|
|
|
|
// Read current .env file or create from example
|
|
let envContent = '';
|
|
if (fs.existsSync(envPath)) {
|
|
envContent = fs.readFileSync(envPath, 'utf8');
|
|
} else if (fs.existsSync(envExampleFile)) {
|
|
envContent = fs.readFileSync(envExampleFile, 'utf8');
|
|
}
|
|
|
|
// Update DATABASE_PROVIDER
|
|
const providerRegex = /^DATABASE_PROVIDER=.*$/m;
|
|
if (providerRegex.test(envContent)) {
|
|
envContent = envContent.replace(providerRegex, `DATABASE_PROVIDER=${provider}`);
|
|
} else {
|
|
envContent += `\nDATABASE_PROVIDER=${provider}\n`;
|
|
}
|
|
|
|
// Update DATABASE_URL based on provider
|
|
if (provider === 'sqlite') {
|
|
const sqliteUrl = 'DATABASE_URL="file:./prisma/dev.db"';
|
|
const urlRegex = /^DATABASE_URL=.*$/m;
|
|
if (urlRegex.test(envContent)) {
|
|
envContent = envContent.replace(urlRegex, sqliteUrl);
|
|
} else {
|
|
envContent += `${sqliteUrl}\n`;
|
|
}
|
|
} else if (provider === 'postgresql') {
|
|
const pgUrl = 'DATABASE_URL="postgresql://username:password@localhost:5432/euchre_camp"';
|
|
const urlRegex = /^DATABASE_URL=.*$/m;
|
|
if (urlRegex.test(envContent)) {
|
|
envContent = envContent.replace(urlRegex, pgUrl);
|
|
} else {
|
|
envContent += `${pgUrl}\n`;
|
|
}
|
|
}
|
|
|
|
// Write updated content
|
|
fs.writeFileSync(envPath, envContent);
|
|
console.log(`✅ Updated ${envFile} to use ${provider} database`);
|
|
}
|
|
|
|
function main() {
|
|
const args = process.argv.slice(2);
|
|
const provider = args[0];
|
|
|
|
if (!provider) {
|
|
console.error('❌ Usage: node scripts/switch-database.js [sqlite|postgres]');
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!['sqlite', 'postgres', 'postgresql'].includes(provider)) {
|
|
console.error(`❌ Invalid provider: ${provider}. Must be 'sqlite' or 'postgres'`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const normalizedProvider = provider === 'postgres' ? 'postgresql' : provider;
|
|
updateEnvFile(normalizedProvider);
|
|
|
|
console.log('\nNext steps:');
|
|
console.log('1. Run: npx prisma generate');
|
|
console.log('2. Run: npx prisma migrate deploy');
|
|
console.log('3. Restart your development server');
|
|
}
|
|
|
|
main();
|