diff --git a/service/src/main/resources/db/migration/V477__delete_disallowed_personal_mobile_pairings.sql b/service/src/main/resources/db/migration/V477__delete_disallowed_personal_mobile_pairings.sql new file mode 100644 index 0000000000..f8900b21e9 --- /dev/null +++ b/service/src/main/resources/db/migration/V477__delete_disallowed_personal_mobile_pairings.sql @@ -0,0 +1,25 @@ +WITH disallowed_devices AS ( + SELECT id + FROM mobile_device + WHERE + employee_id IS NOT NULL AND + NOT EXISTS ( + SELECT FROM daycare_acl a + JOIN daycare d ON d.id = a.daycare_id + WHERE + (d.provider_type = 'MUNICIPAL' OR d.provider_type = 'MUNICIPAL_SCHOOL') AND + a.employee_id = mobile_device.employee_id AND + a.role = 'UNIT_SUPERVISOR' + ) +), push_subscriptions AS ( + DELETE FROM mobile_device_push_subscription + WHERE device IN (SELECT id FROM disallowed_devices) +), push_groups AS ( + DELETE FROM mobile_device_push_group + WHERE device IN (SELECT id FROM disallowed_devices) +), pairings AS ( + DELETE FROM pairing + WHERE mobile_device_id IN (SELECT id FROM disallowed_devices) +) +DELETE FROM mobile_device +WHERE id IN (SELECT id FROM disallowed_devices); diff --git a/service/src/main/resources/migrations.txt b/service/src/main/resources/migrations.txt index 08e47f4563..967228163c 100644 --- a/service/src/main/resources/migrations.txt +++ b/service/src/main/resources/migrations.txt @@ -472,3 +472,4 @@ V473__person_municipality_of_residence.sql V474__holiday_questionnaire_open_ranges.sql V475__application_modified_metadata.sql V476__finance_metadata_process.sql +V477__delete_disallowed_personal_mobile_pairings.sql