-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear-all-data-except-users.sql
More file actions
86 lines (67 loc) · 3.31 KB
/
clear-all-data-except-users.sql
File metadata and controls
86 lines (67 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- ============================================================================
-- Clear All Data Except Users
-- ============================================================================
-- This script deletes all data from all databases EXCEPT user data
-- Run this script to reset the system for testing while keeping user accounts
--
-- Usage:
-- PowerShell: docker exec -i library-postgres psql -U postgres -d <database> -f /tmp/clear-all-data-except-users.sql
-- Or use the PowerShell script: clear-all-data-except-users.ps1
-- ============================================================================
-- ============================================================================
-- SECTION 1: BOOKING_DB - Delete all bookings
-- ============================================================================
-- Database: booking_db
-- Tables: bookings
-- Delete all bookings
TRUNCATE TABLE bookings CASCADE;
-- ============================================================================
-- SECTION 2: CATALOG_DB - Delete all resources
-- ============================================================================
-- Database: catalog_db
-- Tables: resources, resource_amenities
-- Delete resource amenities first (foreign key constraint)
DELETE FROM resource_amenities;
-- Delete all resources
TRUNCATE TABLE resources CASCADE;
-- ============================================================================
-- SECTION 3: POLICY_DB - Delete all policies
-- ============================================================================
-- Database: policy_db
-- Tables: booking_policies
-- Delete all booking policies
TRUNCATE TABLE booking_policies CASCADE;
-- ============================================================================
-- SECTION 4: NOTIFICATION_DB - Delete all notifications
-- ============================================================================
-- Database: notification_db
-- Tables: notifications
-- Delete all notifications
TRUNCATE TABLE notifications CASCADE;
-- ============================================================================
-- SECTION 5: ANALYTICS_DB - Delete all analytics data
-- ============================================================================
-- Database: analytics_db
-- Tables: usage_statistics, analytics_events (if they exist)
-- Delete analytics data (tables may not exist, so we use IF EXISTS pattern)
DO $$
BEGIN
-- Delete usage statistics if table exists
IF EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'usage_statistics') THEN
TRUNCATE TABLE usage_statistics CASCADE;
END IF;
-- Delete analytics events if table exists
IF EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'analytics_events') THEN
TRUNCATE TABLE analytics_events CASCADE;
END IF;
END $$;
-- ============================================================================
-- VERIFICATION QUERIES (uncomment to verify deletion)
-- ============================================================================
-- SELECT 'bookings' as table_name, COUNT(*) as count FROM bookings
-- UNION ALL
-- SELECT 'resources', COUNT(*) FROM resources
-- UNION ALL
-- SELECT 'booking_policies', COUNT(*) FROM booking_policies
-- UNION ALL
-- SELECT 'notifications', COUNT(*) FROM notifications;