· 8 years ago · Feb 10, 2018, 09:22 AM
1begin;
2-- Look for preference set to null, blank or false and remove it.
3
4delete from actor.usr_setting aus
5where
6aus.name='history.circ.retention_start'
7and (aus.value is null or aus.value = 'false' or aus.value='')
8returning aus.*
9;
10
11-- Look for users with circ history but no preference.
12create temp table user_orphan_history
13on commit drop
14as
15select auch.usr, count(auch.id)
16from action.usr_circ_history auch
17left outer join actor.usr_setting aus on aus.usr=auch.usr and aus.name='history.circ.retention_start'
18join actor.usr au on au.id=auch.usr
19where
20aus.value is null
21and not au.deleted
22-- Sanity check, don't do anything if the preference definition has changed or if it isn't used any more,
23 -- Just in case the preference name is changed in the future and automaticlaly updated to a new value
24and exists (select 1 from config.usr_setting_type cust where cust.name='history.circ.retention_start')
25and exists (select 1 from actor.usr_setting aus where name='history.circ.retention_start' limit 1)
26
27group by 1;
28
29select * from user_orphan_history;
30
31delete from action.usr_circ_history auch
32using user_orphan_history uoh
33where
34uoh.usr=auch.usr
35
36returning auch.*
37;
38
39rollback;