Fix SQL reset script to respect foreign key constraints

- Fix deletion order: child tables before parent tables
- Add bet_details deletion before bets deletion
- All 4 options updated with correct deletion order
- Prevents foreign key constraint errors

Deletion order:
1. report_sync_logs (no dependencies)
2. match_reports (child of report_syncs)
3. extraction_stats (child of report_syncs)
4. bet_details (child of bets)
5. bets (parent of bet_details, child of report_syncs)
6. report_syncs (parent of bets, extraction_stats, match_reports)
parent b719f53f
...@@ -13,39 +13,54 @@ ...@@ -13,39 +13,54 @@
-- Option 1: Reset for a specific client -- Option 1: Reset for a specific client
-- ============================================ -- ============================================
-- IMPORTANT: Delete in correct order to respect foreign key constraints
-- Child tables first, then parent tables
-- Delete all sync logs for specific client -- Delete all sync logs for specific client
-- DELETE FROM report_sync_logs WHERE client_id = 'YOUR_CLIENT_ID_HERE'; DELETE FROM report_sync_logs WHERE client_id = 'YOUR_CLIENT_ID_HERE';
-- Delete all match reports for specific client (child of report_syncs)
DELETE FROM match_reports WHERE client_id = 'YOUR_CLIENT_ID_HERE';
-- Delete all match reports for specific client -- Delete all extraction stats for specific client (child of report_syncs)
-- DELETE FROM match_reports WHERE client_id = 'YOUR_CLIENT_ID_HERE'; DELETE FROM extraction_stats WHERE client_id = 'YOUR_CLIENT_ID_HERE';
-- Delete all extraction stats for specific client -- Delete all bet details for specific client (child of bets)
-- DELETE FROM extraction_stats WHERE client_id = 'YOUR_CLIENT_ID_HERE'; DELETE FROM bet_details
WHERE bet_id IN (
SELECT id FROM bets WHERE client_id = 'YOUR_CLIENT_ID_HERE'
);
-- Delete all bets for specific client -- Delete all bets for specific client (parent of bet_details, child of report_syncs)
-- DELETE FROM bets WHERE client_id = 'YOUR_CLIENT_ID_HERE'; DELETE FROM bets WHERE client_id = 'YOUR_CLIENT_ID_HERE';
-- Delete all report syncs for specific client -- Delete all report syncs for specific client (parent of bets, extraction_stats, match_reports)
-- DELETE FROM report_syncs WHERE client_id = 'YOUR_CLIENT_ID_HERE'; DELETE FROM report_syncs WHERE client_id = 'YOUR_CLIENT_ID_HERE';
-- ============================================ -- ============================================
-- Option 2: Reset for ALL clients (Full Reset) -- Option 2: Reset for ALL clients (Full Reset)
-- ============================================ -- ============================================
-- IMPORTANT: Delete in correct order to respect foreign key constraints
-- Child tables first, then parent tables
-- Delete all sync logs -- Delete all sync logs
DELETE FROM report_sync_logs; DELETE FROM report_sync_logs;
-- Delete all match reports -- Delete all match reports (child of report_syncs)
DELETE FROM match_reports; DELETE FROM match_reports;
-- Delete all extraction stats -- Delete all extraction stats (child of report_syncs)
DELETE FROM extraction_stats; DELETE FROM extraction_stats;
-- Delete all bets (bet_details will be cascade deleted) -- Delete all bet details (child of bets)
DELETE FROM bet_details;
-- Delete all bets (parent of bet_details, child of report_syncs)
DELETE FROM bets; DELETE FROM bets;
-- Delete all report syncs -- Delete all report syncs (parent of bets, extraction_stats, match_reports)
DELETE FROM report_syncs; DELETE FROM report_syncs;
...@@ -53,40 +68,54 @@ DELETE FROM report_syncs; ...@@ -53,40 +68,54 @@ DELETE FROM report_syncs;
-- Option 3: Reset for multiple specific clients -- Option 3: Reset for multiple specific clients
-- ============================================ -- ============================================
-- IMPORTANT: Delete in correct order to respect foreign key constraints
-- Child tables first, then parent tables
-- Delete sync logs for multiple clients -- Delete sync logs for multiple clients
DELETE FROM report_sync_logs DELETE FROM report_sync_logs
WHERE client_id IN ( WHERE client_id IN (
'client_id_1', 'client_id_1',
'client_id_2', 'client_id_2',
'client_id_3' 'client_id_3'
); );
-- Delete match reports for multiple clients -- Delete match reports for multiple clients (child of report_syncs)
DELETE FROM match_reports DELETE FROM match_reports
WHERE client_id IN ( WHERE client_id IN (
'client_id_1', 'client_id_1',
'client_id_2', 'client_id_2',
'client_id_3' 'client_id_3'
); );
-- Delete extraction stats for multiple clients -- Delete extraction stats for multiple clients (child of report_syncs)
DELETE FROM extraction_stats DELETE FROM extraction_stats
WHERE client_id IN ( WHERE client_id IN (
'client_id_1', 'client_id_1',
'client_id_2', 'client_id_2',
'client_id_3' 'client_id_3'
); );
-- Delete bets for multiple clients -- Delete bet details for multiple clients (child of bets)
DELETE FROM bets DELETE FROM bet_details
WHERE bet_id IN (
SELECT id FROM bets
WHERE client_id IN (
'client_id_1',
'client_id_2',
'client_id_3'
)
);
-- Delete bets for multiple clients (parent of bet_details, child of report_syncs)
DELETE FROM bets
WHERE client_id IN ( WHERE client_id IN (
'client_id_1', 'client_id_1',
'client_id_2', 'client_id_2',
'client_id_3' 'client_id_3'
); );
-- Delete report syncs for multiple clients -- Delete report syncs for multiple clients (parent of bets, extraction_stats, match_reports)
DELETE FROM report_syncs DELETE FROM report_syncs
WHERE client_id IN ( WHERE client_id IN (
'client_id_1', 'client_id_1',
'client_id_2', 'client_id_2',
...@@ -98,24 +127,34 @@ WHERE client_id IN ( ...@@ -98,24 +127,34 @@ WHERE client_id IN (
-- Option 4: Reset syncs after a specific date -- Option 4: Reset syncs after a specific date
-- ============================================ -- ============================================
-- IMPORTANT: Delete in correct order to respect foreign key constraints
-- Child tables first, then parent tables
-- Delete sync logs after specific date -- Delete sync logs after specific date
DELETE FROM report_sync_logs DELETE FROM report_sync_logs
WHERE created_at >= '2026-01-01 00:00:00'; WHERE created_at >= '2026-01-01 00:00:00';
-- Delete match reports after specific date -- Delete match reports after specific date (child of report_syncs)
DELETE FROM match_reports DELETE FROM match_reports
WHERE created_at >= '2026-01-01 00:00:00'; WHERE created_at >= '2026-01-01 00:00:00';
-- Delete extraction stats after specific date -- Delete extraction stats after specific date (child of report_syncs)
DELETE FROM extraction_stats DELETE FROM extraction_stats
WHERE created_at >= '2026-01-01 00:00:00'; WHERE created_at >= '2026-01-01 00:00:00';
-- Delete bets after specific date -- Delete bet details after specific date (child of bets)
DELETE FROM bets DELETE FROM bet_details
WHERE bet_id IN (
SELECT id FROM bets
WHERE created_at >= '2026-01-01 00:00:00'
);
-- Delete bets after specific date (parent of bet_details, child of report_syncs)
DELETE FROM bets
WHERE created_at >= '2026-01-01 00:00:00'; WHERE created_at >= '2026-01-01 00:00:00';
-- Delete report syncs after specific date -- Delete report syncs after specific date (parent of bets, extraction_stats, match_reports)
DELETE FROM report_syncs DELETE FROM report_syncs
WHERE created_at >= '2026-01-01 00:00:00'; WHERE created_at >= '2026-01-01 00:00:00';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment