120 lines
4.1 KiB
JavaScript
Executable File
120 lines
4.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Check for test records in production database (read-only)
|
|
* Shows what would be deleted without making any changes
|
|
*/
|
|
|
|
const { execSync } = require('child_process');
|
|
|
|
// Database connection string - must be explicitly set via environment variable
|
|
const DB_URL = process.env.PROD_DATABASE_URL || process.env.DATABASE_URL;
|
|
|
|
// Check if database URL is set
|
|
if (!DB_URL) {
|
|
console.error('❌ No database URL set');
|
|
console.error('Please set PROD_DATABASE_URL or DATABASE_URL environment variable');
|
|
console.error('For production: PROD_DATABASE_URL="postgresql://user:pass@host:port/dbname"');
|
|
process.exit(1);
|
|
}
|
|
|
|
// 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 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;
|
|
}
|
|
}
|
|
|
|
console.log('🔍 Checking test records in production database');
|
|
console.log('=============================================\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}`);
|
|
|
|
// Show test players
|
|
runSQL(`
|
|
SELECT id, name
|
|
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%'
|
|
ORDER BY name;
|
|
`, 'Test players list');
|
|
|
|
// Show test events
|
|
runSQL(`
|
|
SELECT id, name
|
|
FROM events
|
|
WHERE name LIKE '%Test%' OR name LIKE '%Setup%' OR name LIKE '%Recent%'
|
|
ORDER BY name;
|
|
`, 'Test events list');
|
|
|
|
// Show test users
|
|
runSQL(`
|
|
SELECT id, email, name
|
|
FROM users
|
|
WHERE email LIKE '%test%' OR email LIKE '%setup%'
|
|
ORDER BY email;
|
|
`, 'Test users list');
|
|
|
|
// Check which test players have matches (simplified query)
|
|
runSQL(`
|
|
SELECT p.id, p.name
|
|
FROM players p
|
|
WHERE (p.name LIKE '%Test%' OR p.name LIKE '%Setup%' OR p.name LIKE '%Home Test%' OR p.name LIKE '%Home Match Player%' OR p.name LIKE '%Admin User%')
|
|
ORDER BY p.name;
|
|
`, 'Test players (will be deleted)');
|
|
|
|
// Check which test events exist
|
|
runSQL(`
|
|
SELECT e.id, e.name
|
|
FROM events e
|
|
WHERE (e.name LIKE '%Test%' OR e.name LIKE '%Setup%' OR e.name LIKE '%Recent%')
|
|
ORDER BY e.name;
|
|
`, 'Test events (will be deleted with matches via cascade)');
|
|
|
|
console.log('\n✅ Check complete!');
|
|
console.log('\n💡 Summary:');
|
|
console.log(' - Test tournaments (with matches) can be deleted');
|
|
console.log(' - Test players can be deleted');
|
|
console.log(' - Test users without player associations can be deleted');
|