142 lines
5.3 KiB
JavaScript
Executable File
142 lines
5.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Clean up production database - remove test records only
|
|
*
|
|
* WARNING: This script modifies the production database.
|
|
* It only removes records that match test patterns.
|
|
* It preserves actual player data, matches, and tournaments.
|
|
*/
|
|
|
|
const { execSync } = require('child_process');
|
|
|
|
// Database connection string - use environment variable or default to production
|
|
const DB_URL = process.env.PROD_DATABASE_URL ||
|
|
process.env.DATABASE_URL ||
|
|
'postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp';
|
|
|
|
// Validate that we're not accidentally using dev database
|
|
if (DB_URL.includes('_dev') || DB_URL.includes('_dev_')) {
|
|
console.error('❌ ERROR: This script should not be used with the development database!');
|
|
console.error(' Current DATABASE_URL:', DB_URL);
|
|
console.error(' Please set PROD_DATABASE_URL for production database operations.');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Helper to run SQL and log results
|
|
function runSQL(sql, description) {
|
|
console.log(`\n🔍 ${description}...`);
|
|
try {
|
|
const result = execSync(`psql "${DB_URL}" -c "${sql}"`, { encoding: 'utf8' });
|
|
console.log(result);
|
|
return true;
|
|
} catch (error) {
|
|
console.error(`❌ Error: ${error.message}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Helper to run multi-line SQL
|
|
function runMultiLineSQL(sqlLines, description) {
|
|
console.log(`\n🔍 ${description}...`);
|
|
try {
|
|
const sql = sqlLines.join('\n');
|
|
const result = execSync(`psql "${DB_URL}" -c "${sql}"`, { encoding: 'utf8' });
|
|
console.log(result);
|
|
return true;
|
|
} catch (error) {
|
|
console.error(`❌ Error: ${error.message}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Helper to count records matching pattern
|
|
function countRecords(table, column, pattern) {
|
|
const sql = `SELECT COUNT(*) FROM ${table} WHERE ${column} LIKE '${pattern}';`;
|
|
try {
|
|
const result = execSync(`psql "${DB_URL}" -c "${sql}" -t`, { encoding: 'utf8' }).trim();
|
|
return parseInt(result);
|
|
} catch (error) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
console.log('🧹 Cleaning up test records from production database');
|
|
console.log('=============================================\n');
|
|
|
|
// Confirm with user
|
|
const readline = require('readline');
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
rl.question('⚠️ This will DELETE test records from PRODUCTION database. Continue? (y/N) ', async (answer) => {
|
|
if (answer.toLowerCase() !== 'y' && answer.toLowerCase() !== 'yes') {
|
|
console.log('❌ Cleanup cancelled');
|
|
rl.close();
|
|
process.exit(0);
|
|
}
|
|
|
|
rl.close();
|
|
|
|
console.log('\n📋 Checking test records...\n');
|
|
|
|
// Count test players
|
|
const testPlayerCount = countRecords('players', 'name', '%Test%') +
|
|
countRecords('players', 'name', '%Setup%') +
|
|
countRecords('players', 'name', '%Home Test%') +
|
|
countRecords('players', 'name', '%Home Match Player%') +
|
|
countRecords('players', 'name', '%Admin User%');
|
|
console.log(`Test players found: ${testPlayerCount}`);
|
|
|
|
// Count test tournaments
|
|
const testEventCount = countRecords('events', 'name', '%Test%') +
|
|
countRecords('events', 'name', '%Setup%') +
|
|
countRecords('events', 'name', '%Recent%');
|
|
console.log(`Test tournaments found: ${testEventCount}`);
|
|
|
|
// Count test users
|
|
const testUserCount = countRecords('users', 'email', '%test%') +
|
|
countRecords('users', 'email', '%setup%');
|
|
console.log(`Test users found: ${testUserCount}`);
|
|
|
|
console.log('\n🔄 Starting cleanup...\n');
|
|
|
|
// Step 1: Delete test tournaments (with matches due to cascade delete)
|
|
console.log('1. Deleting test tournaments (matches will be deleted via cascade)...');
|
|
runSQL(
|
|
"DELETE FROM events WHERE (name LIKE '%Test%' OR name LIKE '%Setup%' OR name LIKE '%Recent%');",
|
|
'Deleted test tournaments'
|
|
);
|
|
|
|
// Step 2: Delete test players (all of them, since we deleted their matches)
|
|
console.log('\n2. Deleting test players...');
|
|
runSQL(
|
|
"DELETE FROM players WHERE (name LIKE '%Test%' OR name LIKE '%Setup%' OR name LIKE '%Home Test%' OR name LIKE '%Home Match Player%' OR name LIKE '%Admin User%');",
|
|
'Deleted test players'
|
|
);
|
|
|
|
// Step 3: Delete test users (they shouldn't have player associations)
|
|
console.log('\n3. Deleting test users...');
|
|
runSQL(
|
|
"DELETE FROM users WHERE (email LIKE '%test%' OR email LIKE '%setup%') AND \"playerId\" IS NULL;",
|
|
'Deleted test users'
|
|
);
|
|
|
|
// Summary
|
|
console.log('\n✅ Cleanup complete!');
|
|
console.log('\n📊 Remaining test records:');
|
|
runSQL("SELECT COUNT(*) FROM players WHERE name LIKE '%Test%' OR name LIKE '%Setup%' OR name LIKE '%Home Test%';", 'Remaining test players');
|
|
runSQL("SELECT COUNT(*) FROM events WHERE name LIKE '%Test%' OR name LIKE '%Setup%';", 'Remaining test tournaments');
|
|
runSQL("SELECT COUNT(*) FROM users WHERE email LIKE '%test%' OR email LIKE '%setup%';", 'Remaining test users');
|
|
|
|
console.log('\n💡 Note: All test records deleted. Matches are automatically deleted');
|
|
console.log(' via cascade delete when their parent tournament is deleted.');
|
|
});
|
|
}
|
|
|
|
// Run main function
|
|
main();
|