BEGIN; -- Do not silently discard ownership when rolling back. DO $$ BEGIN IF EXISTS(SELECT 1 FROM device_account_binding) THEN RAISE EXCEPTION 'Account device bindings exist; restore the matching backup instead of dropping ownership'; END IF; END $$; DROP TRIGGER device_account_binding_reset ON device; DROP FUNCTION reset_device_account_binding(); DROP TRIGGER device_account_assignment_finish ON device_binding; DROP FUNCTION finish_device_account_assignment(); DROP TRIGGER device_account_assignment_guard ON device_binding; DROP FUNCTION guard_device_account_assignment(); DELETE FROM device_ble_binding WHERE subject_id IS NULL; ALTER TABLE device_ble_binding DROP CONSTRAINT device_ble_binding_profile_pair, DROP COLUMN account_binding_id; ALTER TABLE device_ble_binding ALTER COLUMN household_id SET NOT NULL, ALTER COLUMN subject_id SET NOT NULL; DROP TABLE device_account_binding; COMMIT;