Fix employee time conflicts

This commit is contained in:
2026-04-06 00:18:49 -06:00
parent 706cd94d14
commit 6d4c9a5e65
4 changed files with 73 additions and 44 deletions

View File

@@ -0,0 +1,28 @@
-- V17: Normalize legacy appointmentPet data into customer_pet and appointment_customer_pet
-- Step 1: Ensure a customer_pet exists for every pet linked in appointmentPet
-- Note: pet species and breed might be null in pet table, but we copy them over if present
INSERT INTO customer_pet (customer_id, pet_name, species, breed)
SELECT DISTINCT a.customerId, p.petName, p.petSpecies, p.petBreed
FROM appointmentPet ap
JOIN appointment a ON a.appointmentId = ap.appointmentId
JOIN pet p ON p.petId = ap.petId
WHERE NOT EXISTS (
SELECT 1 FROM customer_pet cp
WHERE cp.customer_id = a.customerId AND cp.pet_name = p.petName
);
-- Step 2: Link the appointment to the customer_pet
INSERT INTO appointment_customer_pet (appointment_id, customer_pet_id)
SELECT ap.appointmentId, cp.customer_pet_id
FROM appointmentPet ap
JOIN appointment a ON a.appointmentId = ap.appointmentId
JOIN pet p ON p.petId = ap.petId
JOIN customer_pet cp ON cp.customer_id = a.customerId AND cp.pet_name = p.petName
WHERE NOT EXISTS (
SELECT 1 FROM appointment_customer_pet acp
WHERE acp.appointment_id = ap.appointmentId AND acp.customer_pet_id = cp.customer_pet_id
);
-- Step 3: Remove the old legacy relationships so it strictly uses the new ones
DELETE FROM appointmentPet;