 UPDATE order_type SET retired_by = @buendia_admin_id WHERE
     retired_by IS NOT NULL AND retired_by != @admin_id;
 
-UPDATE orders SET creator = @buendia_admin_id WHERE
-    creator IS NOT NULL AND creator != @admin_id;
-UPDATE orders SET voided_by = @buendia_admin_id WHERE
-    voided_by IS NOT NULL AND voided_by != @admin_id;
-
-UPDATE patient SET changed_by = @buendia_admin_id WHERE
-    changed_by IS NOT NULL AND changed_by != @admin_id;
-UPDATE patient SET creator = @buendia_admin_id WHERE
-    creator IS NOT NULL AND creator != @admin_id;
-UPDATE patient SET voided_by = @buendia_admin_id WHERE
-    voided_by IS NOT NULL AND voided_by != @admin_id;
-
-UPDATE patient_identifier SET creator = @buendia_admin_id WHERE
-    creator IS NOT NULL AND creator != @admin_id;
-UPDATE patient_identifier SET voided_by = @buendia_admin_id WHERE
-    voided_by IS NOT NULL AND voided_by != @admin_id;
-UPDATE patient_identifier SET changed_by = @buendia_admin_id WHERE
-    changed_by IS NOT NULL AND changed_by != @admin_id;
-
 UPDATE patient_identifier_type SET creator = @buendia_admin_id WHERE
     creator IS NOT NULL AND creator != @admin_id;
 UPDATE patient_identifier_type SET retired_by = @buendia_admin_id WHERE
     retired_by IS NOT NULL AND retired_by != @admin_id;
 
-UPDATE patient_program SET creator = @buendia_admin_id WHERE
-    creator IS NOT NULL AND creator != @admin_id;
-UPDATE patient_program SET changed_by = @buendia_admin_id WHERE
-    changed_by IS NOT NULL AND changed_by != @admin_id;
-UPDATE patient_program SET voided_by = @buendia_admin_id WHERE
-    voided_by IS NOT NULL AND voided_by != @admin_id;
-
-UPDATE patient_state SET changed_by = @buendia_admin_id WHERE
-    changed_by IS NOT NULL AND changed_by != @admin_id;
-UPDATE patient_state SET creator = @buendia_admin_id WHERE
-    creator IS NOT NULL AND creator != @admin_id;
-UPDATE patient_state SET voided_by = @buendia_admin_id WHERE
-    voided_by IS NOT NULL AND voided_by != @admin_id;
-
 UPDATE person SET changed_by = @buendia_admin_id WHERE
     changed_by IS NOT NULL AND changed_by != @admin_id;
 UPDATE person SET creator = @buendia_admin_id WHERE


DELETE FROM person WHERE person_id NOT IN (SELECT person_id FROM keep_users);

-- Create a temporary table for the locations to keep.
CREATE TEMPORARY TABLE keep_locations SELECT * FROM location WHERE uuid IN (
    "3449f5fe-8e6b-4250-bcaa-fca5df28ddbf", -- EMC
    "3f75ca61-ec1a-4739-af09-25a84e3dd237", -- Triage Zone
    "2f1e2418-ede6-481a-ad80-b9939a7fde8e", -- Suspected Zone
    "3b11e7c8-a68a-4a5f-afb3-a4a053592d0e", -- Probable Zone
    "b9038895-9c9d-4908-9e0d-51fd535ddd3c", -- Confirmed Zone
    "4ef642b9-9843-4d0d-9b2b-84fe1984801f", -- Morgue
    "d7ca63c3-6ea0-4357-82fd-0910cc17a2cb" -- Discharged
);

-- Remove other locations, first clearing out foreign keys that refer to them.
UPDATE location SET parent_location = NULL WHERE
    location_id NOT IN (SELECT location_id FROM keep_locations);
DELETE FROM location_attribute WHERE
    location_id NOT IN (SELECT location_id FROM keep_locations);
DELETE FROM location WHERE
    location_id NOT IN (SELECT location_id FROM keep_locations);

-- Clear the huge concept word and concept reference tables; we don't need them.
DELETE FROM concept_word;
DELETE FROM concept_reference_term_map;
DELETE FROM concept_reference_term;
DELETE FROM drug_reference_map;
DELETE FROM concept_reference_term;
DELETE FROM concept_reference_map;



-- HL7 forms are used for xform submission
DELETE FROM hl7_in_archive;
DELETE FROM hl7_in_error;
DELETE FROM hl7_in_queue;

-- Remove all encounters/observations
DELETE FROM active_list_allergy; -- refers to concept
DELETE FROM active_list_problem;
DELETE FROM active_list; -- refers to person, obs, and users
DELETE FROM concept_proposal_tag_map; -- refers to concept_proposal
DELETE FROM concept_proposal; -- refers to obs and encounter
DELETE FROM note; -- refers to obs, encounter, and patient
DELETE FROM obs; -- refers to order and encounter

-- Remove all orders
DELETE FROM drug_order; -- refers to orders and concepts
DELETE FROM order_type WHERE parent IS NOT NULL; -- refers to orders
DELETE FROM test_order; -- refers to orders
DELETE FROM orders; -- refers to encounter and patient

-- Remove all encounters
DELETE FROM encounter_provider; -- refers to encounter
DELETE FROM encounter; -- refers to patient
DELETE FROM visit; -- refers to patient, location, concept

-- Start with no providers at all
DELETE FROM provider_attribute; -- refers to provider
DELETE FROM provider; -- refers to person

-- Delete cohorts
DELETE FROM cohort_member; -- refers to patient

-- Delete visits
DELETE FROM visit_attribute; -- refers to visit
DELETE FROM visit; -- refers to location

-- Delete forms, first clearing out foreign keys that refer to them.
DELETE FROM xforms_xform;
DELETE FROM form_resource;
UPDATE form_field SET parent_form_field = NULL;
DELETE FROM form_field; -- refers to field, form, and form_field
DELETE FROM form;
DELETE FROM field_answer; -- refers to field and concept
DELETE FROM field;

-- Delete patients.
DELETE FROM patient_identifier; -- refers to patient
DELETE FROM patient_state; -- refers to patient_program
DELETE FROM patient_program; -- refers to patient
DELETE FROM patient;

-- Delete programs
DELETE FROM program_workflow_state; -- referes to program_workflow
DELETE FROM program_workflow; -- referes to program
DELETE FROM program;

-- Remove the locations, first clearing out foreign keys that refer to them.
UPDATE location SET parent_location = NULL;
DELETE FROM location_attribute; -- refers to location, location_attribute_type
DELETE FROM location_attribute_type;
DELETE FROM location_tag_map; -- refers to location and location_tag
DELETE FROM location_tag;
DELETE FROM location;

-- Delete metadata sharing information.
DELETE FROM metadatasharing_exported_package;
DELETE FROM metadatasharing_imported_package;
DELETE FROM metadatasharing_imported_item;

-- Delete notification alerts.
DELETE FROM notification_alert_recipient; -- refers to notification_alert
DELETE FROM notification_alert;

-- Delete serialized objects.
DELETE FROM serialized_object;

-- Keep the OpenMRS identifier type; delete all others.
DELETE FROM patient_identifier_type WHERE name NOT LIKE 'OpenMRS%';

-- Create a temporary table to hold the users that are not to be deleted.
-- For information about the daemon user, see https://wiki.openmrs.org/display/docs/Daemon+User
CREATE TEMPORARY TABLE keep_users SELECT * FROM users WHERE system_id IN
    ("admin", "daemon");

