-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete_relationship_fix.sql
More file actions
250 lines (210 loc) · 8.06 KB
/
complete_relationship_fix.sql
File metadata and controls
250 lines (210 loc) · 8.06 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
-- ===================================================================
-- Complete Database Relationship Fix and Constraint Validation
-- This script ensures proper foreign key relationships and data integrity
-- ===================================================================
-- PHASE 1: Analysis of Current State
-- ===================================================================
SELECT '=== CURRENT DATABASE ANALYSIS ===' as phase;
-- Show current users
SELECT 'Current Users:' as info;
SELECT id, username, email, role, created_at
FROM users
WHERE archived = 0
ORDER BY id;
-- Show adopters and their user links
SELECT 'Current Adopters:' as info;
SELECT adopter_id, username, adopter_name, adopter_email, adopter_username
FROM tbl_adopter
WHERE archived = 0
ORDER BY adopter_id;
-- Show pet owners and their user links
SELECT 'Current Pet Owners:' as info;
SELECT pet_owner_id, username, pet_owner_name, pet_owner_email, pet_owner_username
FROM tbl_pet_owner
WHERE archived = 0
ORDER BY pet_owner_id;
-- PHASE 2: Fix Data Inconsistencies
-- ===================================================================
SELECT '=== PHASE 2: FIXING DATA INCONSISTENCIES ===' as phase;
-- First, handle duplicate emails by making them unique
UPDATE tbl_adopter
SET adopter_email = CASE
WHEN adopter_id = 1 AND adopter_email = 'adopter1@example.com' THEN 'alice.johnson@example.com'
WHEN adopter_id = 2 THEN 'mark.cruz@example.com'
WHEN adopter_id = 3 AND adopter_email = 'adopter1@example.com' THEN 'john.adopter@example.com'
WHEN adopter_id = 4 THEN 'theresa.gina@example.com'
ELSE adopter_email
END
WHERE adopter_id IN (1, 2, 3, 4);
-- Create missing user accounts for existing adopters
INSERT IGNORE INTO users (username, email, password, role, created_at)
VALUES
('alicej', 'alice.johnson@example.com', 'password123', 'adopter', NOW()),
('markc', 'mark.cruz@example.com', 'password123', 'adopter', NOW()),
('adopter1', 'john.adopter@example.com', 'iwantmydaddy', 'adopter', NOW()),
('ginathis', 'theresa.gina@example.com', 'Sdfg123', 'adopter', NOW());
-- Link existing adopters to their user accounts
UPDATE tbl_adopter SET username = 'alicej' WHERE adopter_id = 1 AND username IS NULL;
UPDATE tbl_adopter SET username = 'markc' WHERE adopter_id = 2 AND username IS NULL;
UPDATE tbl_adopter SET username = 'adopter1' WHERE adopter_id = 3 AND username IS NULL;
UPDATE tbl_adopter SET username = 'ginathis' WHERE adopter_id = 4 AND username IS NULL;
-- Handle the case where "Lara Croft" user might exist but not have a profile
-- First check if user exists, if not create it
INSERT IGNORE INTO users (username, email, password, role, created_at)
VALUES ('Lara Croft', 'lara.croft@example.com', 'dsfgerty', 'adopter', NOW());
-- Create adopter profile for Lara Croft if it doesn't exist
INSERT IGNORE INTO tbl_adopter (username, adopter_name, adopter_contact, adopter_email, adopter_address, adopter_username, adopter_password)
VALUES (
'Lara Croft',
'Lara Croft',
'09000000000',
'lara.croft@example.com',
'Tomb Raider Mansion',
'Lara Croft',
'dsfgerty'
);
-- PHASE 3: Create Missing Profiles for Existing Users
-- ===================================================================
SELECT '=== PHASE 3: CREATING MISSING PROFILES ===' as phase;
-- Create adopter profiles for users who don't have them
INSERT IGNORE INTO tbl_adopter (username, adopter_name, adopter_contact, adopter_email, adopter_address, adopter_username, adopter_password)
SELECT
u.username,
u.username,
'09000000000',
u.email,
'Address not provided',
u.username,
u.password
FROM users u
LEFT JOIN tbl_adopter a ON u.username = a.username
WHERE u.role = 'adopter'
AND a.adopter_id IS NULL
AND u.archived = 0;
-- Create pet owner profiles for users who don't have them
INSERT IGNORE INTO tbl_pet_owner (username, pet_owner_name, pet_owner_contact, pet_owner_email, pet_owner_address, pet_owner_username, pet_owner_password)
SELECT
u.username,
u.username,
'09000000000',
u.email,
'Address not provided',
u.username,
u.password
FROM users u
LEFT JOIN tbl_pet_owner po ON u.username = po.username
WHERE u.role = 'pet_owner'
AND po.pet_owner_id IS NULL
AND u.archived = 0;
-- PHASE 4: Validate Foreign Key Constraints
-- ===================================================================
SELECT '=== PHASE 4: FOREIGN KEY CONSTRAINT VALIDATION ===' as phase;
-- Check if all adopter username references are valid
SELECT 'Invalid adopter-user references:' as check_result;
SELECT a.adopter_id, a.username as adopter_username, 'User not found' as issue
FROM tbl_adopter a
LEFT JOIN users u ON a.username = u.username
WHERE a.username IS NOT NULL
AND u.username IS NULL
AND a.archived = 0;
-- Check if all pet owner username references are valid
SELECT 'Invalid pet_owner-user references:' as check_result;
SELECT po.pet_owner_id, po.username as pet_owner_username, 'User not found' as issue
FROM tbl_pet_owner po
LEFT JOIN users u ON po.username = u.username
WHERE po.username IS NOT NULL
AND u.username IS NULL
AND po.archived = 0;
-- PHASE 5: Final Verification and Summary
-- ===================================================================
SELECT '=== FINAL VERIFICATION RESULTS ===' as phase;
-- Complete user-profile mapping
SELECT 'Complete User-Profile Mapping:' as summary;
SELECT
u.id,
u.username,
u.email,
u.role,
u.created_at,
CASE
WHEN u.role = 'admin' THEN 'Admin - No profile needed'
WHEN u.role = 'adopter' THEN
CASE
WHEN a.adopter_id IS NOT NULL THEN CONCAT('✓ Adopter Profile ID: ', a.adopter_id, ' (', a.adopter_name, ')')
ELSE '❌ MISSING ADOPTER PROFILE'
END
WHEN u.role = 'pet_owner' THEN
CASE
WHEN po.pet_owner_id IS NOT NULL THEN CONCAT('✓ Pet Owner Profile ID: ', po.pet_owner_id, ' (', po.pet_owner_name, ')')
ELSE '❌ MISSING PET OWNER PROFILE'
END
ELSE '? Unknown role'
END as profile_status
FROM users u
LEFT JOIN tbl_adopter a ON u.username = a.username AND a.archived = 0
LEFT JOIN tbl_pet_owner po ON u.username = po.username AND po.archived = 0
WHERE u.archived = 0
ORDER BY u.id;
-- Summary statistics
SELECT 'Summary Statistics:' as summary;
SELECT
'Total Users' as metric,
COUNT(*) as count
FROM users
WHERE archived = 0
UNION ALL
SELECT
'Admin Users' as metric,
COUNT(*) as count
FROM users
WHERE role = 'admin' AND archived = 0
UNION ALL
SELECT
'Adopter Users' as metric,
COUNT(*) as count
FROM users
WHERE role = 'adopter' AND archived = 0
UNION ALL
SELECT
'Pet Owner Users' as metric,
COUNT(*) as count
FROM users
WHERE role = 'pet_owner' AND archived = 0
UNION ALL
SELECT
'Adopter Profiles' as metric,
COUNT(*) as count
FROM tbl_adopter
WHERE archived = 0 AND username IS NOT NULL
UNION ALL
SELECT
'Pet Owner Profiles' as metric,
COUNT(*) as count
FROM tbl_pet_owner
WHERE archived = 0 AND username IS NOT NULL
UNION ALL
SELECT
'Orphaned Adopter Records' as metric,
COUNT(*) as count
FROM tbl_adopter
WHERE archived = 0 AND username IS NULL
UNION ALL
SELECT
'Orphaned Pet Owner Records' as metric,
COUNT(*) as count
FROM tbl_pet_owner
WHERE archived = 0 AND username IS NULL;
-- Final constraint validation
SELECT 'Constraint Validation Results:' as summary;
SELECT
CASE
WHEN (SELECT COUNT(*) FROM tbl_adopter a LEFT JOIN users u ON a.username = u.username WHERE a.username IS NOT NULL AND u.username IS NULL AND a.archived = 0) = 0
THEN '✓ All adopter foreign key constraints are valid'
ELSE '❌ Some adopter foreign key constraints are invalid'
END as adopter_constraints,
CASE
WHEN (SELECT COUNT(*) FROM tbl_pet_owner po LEFT JOIN users u ON po.username = u.username WHERE po.username IS NOT NULL AND u.username IS NULL AND po.archived = 0) = 0
THEN '✓ All pet owner foreign key constraints are valid'
ELSE '❌ Some pet owner foreign key constraints are invalid'
END as pet_owner_constraints;
SELECT '=== RELATIONSHIP FIX COMPLETED ===' as phase;