diff --git a/scripts/check-test-records.js b/scripts/check-test-records.js index e57ecfd..2d8185b 100755 --- a/scripts/check-test-records.js +++ b/scripts/check-test-records.js @@ -40,12 +40,15 @@ 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 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', '%Setup%') + + countRecords('events', 'name', '%Recent%'); console.log(`Test tournaments found: ${testEventCount}`); // Count test users @@ -57,7 +60,7 @@ console.log(`Test users found: ${testUserCount}`); runSQL(` SELECT id, name FROM players - WHERE name LIKE '%Test%' OR name LIKE '%Setup%' OR name LIKE '%Home Test%' + 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'); @@ -65,7 +68,7 @@ runSQL(` runSQL(` SELECT id, name FROM events - WHERE name LIKE '%Test%' OR name LIKE '%Setup%' + WHERE name LIKE '%Test%' OR name LIKE '%Setup%' OR name LIKE '%Recent%' ORDER BY name; `, 'Test events list'); @@ -77,35 +80,24 @@ runSQL(` ORDER BY email; `, 'Test users list'); -// Check which test players have matches +// 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%') - AND EXISTS ( - SELECT 1 FROM matches m - WHERE m."team1P1Id" = p.id - OR m."team1P2Id" = p.id - OR m."team2P1Id" = p.id - OR m."team2P2Id" = p.id - ) + 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 with matches (will be preserved)'); +`, 'Test players (will be deleted)'); -// Check which test events have matches +// Check which test events exist runSQL(` SELECT e.id, e.name FROM events e - WHERE (e.name LIKE '%Test%' OR e.name LIKE '%Setup%') - AND EXISTS ( - SELECT 1 FROM matches m WHERE m."eventId" = e.id - ) + WHERE (e.name LIKE '%Test%' OR e.name LIKE '%Setup%' OR e.name LIKE '%Recent%') ORDER BY e.name; -`, 'Test events with matches (will be preserved)'); +`, 'Test events (will be deleted with matches via cascade)'); console.log('\nāœ… Check complete!'); console.log('\nšŸ’” Summary:'); -console.log(' - Test players without matches can be deleted'); -console.log(' - Test tournaments without matches can be deleted'); +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'); -console.log(' - Records with matches/associations are preserved'); diff --git a/scripts/cleanup-prod-db.js b/scripts/cleanup-prod-db.js index b6b126e..aa0afdb 100755 --- a/scripts/cleanup-prod-db.js +++ b/scripts/cleanup-prod-db.js @@ -76,12 +76,15 @@ async function main() { // Count test players const testPlayerCount = countRecords('players', 'name', '%Test%') + countRecords('players', 'name', '%Setup%') + - countRecords('players', 'name', '%Home Test%'); + 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', '%Setup%') + + countRecords('events', 'name', '%Recent%'); console.log(`Test tournaments found: ${testEventCount}`); // Count test users @@ -91,18 +94,18 @@ async function main() { console.log('\nšŸ”„ Starting cleanup...\n'); - // Step 1: Delete test events (tournaments) that have no matches - console.log('1. Deleting test tournaments without matches...'); + // 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%') AND NOT EXISTS (SELECT 1 FROM matches WHERE matches.\"eventId\" = events.id);", + "DELETE FROM events WHERE (name LIKE '%Test%' OR name LIKE '%Setup%' OR name LIKE '%Recent%');", 'Deleted test tournaments' ); - // Step 2: Delete test players that have no matches - console.log('\n2. Deleting test players without matches...'); + // 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%') AND NOT EXISTS (SELECT 1 FROM matches WHERE matches.\"team1P1Id\" = players.id) AND NOT EXISTS (SELECT 1 FROM matches WHERE matches.\"team1P2Id\" = players.id) AND NOT EXISTS (SELECT 1 FROM matches WHERE matches.\"team2P1Id\" = players.id) AND NOT EXISTS (SELECT 1 FROM matches WHERE matches.\"team2P2Id\" = players.id);", - 'Deleted test players without matches' + "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) @@ -119,10 +122,8 @@ async function main() { 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: Some test records may remain if they have:'); - console.log(' - Associated matches'); - console.log(' - Associated users'); - console.log(' These were preserved to maintain data integrity.'); + console.log('\nšŸ’” Note: All test records deleted. Matches are automatically deleted'); + console.log(' via cascade delete when their parent tournament is deleted.'); }); }