Advertisement
chrissharp123

Untitled

Aug 9th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. root@db01:~# cat /var/lib/postgresql/eg/set_users_inactive_nightly.sql
  2. begin;
  3. update actor.usr au
  4. set
  5. active = false,
  6. --alert_message = 'automatically set to inactive status via PINES policy / ' || alert_message,
  7. last_update_time = now()
  8. -- no unfinished circulations and no circulations within the last 3 years
  9. where not exists (
  10. select 1
  11. from action.circulation ac
  12. where ac.usr = au.id
  13. and (
  14. xact_finish is null or (
  15. now() - ac.xact_start < '3 years'::interval
  16. )
  17. )
  18. )
  19. -- no hold requests placed in the last 3 years
  20. and not exists (
  21. select 1
  22. from action.hold_request ahr
  23. where ahr.usr = au.id
  24. and (now() - request_time) < '3 years'::interval
  25. )
  26. -- no owed money in either direction and no payment within the last 3 years
  27. and not exists (
  28. select 1
  29. from money.materialized_billable_xact_summary mmbxs
  30. where mmbxs.usr = au.id
  31. and (
  32. balance_owed <> '0.00' or (now() - last_payment_ts) < '3 years'::interval)
  33. )
  34. -- no activity entries within the last 3 years
  35. and not exists (
  36. select 1
  37. from actor.usr_activity aua
  38. where aua.usr = au.id
  39. and (now() - event_time) < '3 years'::interval
  40. )
  41. -- we only care about active users
  42. and au.active
  43. -- we don't care about deleted users
  44. and not au.deleted
  45. -- don't include non-expired users that don't otherwise meet the "inactive" criteria
  46. and expire_date < now()
  47. -- we don't want users that have been created within the last 3 years
  48. and (now() - au.create_date) > '3 years'
  49. -- restrict to patron profiles ('Patrons' = 45)
  50. and profile in (
  51. select id
  52. from permission.grp_descendants(45)
  53. )
  54. ;
  55. commit;
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement