-
Notifications
You must be signed in to change notification settings - Fork 29
/
recalculate.sh
executable file
·51 lines (46 loc) · 1.96 KB
/
recalculate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
#
# i recalculate counter fields in database to ensure their
# correctness. sometimes stuff like comment karma can get out of sync
# if users are purged from the database or a software bug occurs.
# this script should be run periodically to fix that.
#
# to use me run "crontab -e" and add:
#
# @hourly nice recalculate.sh ows
#
DB=$1
[[ $DB ]] || exit 1
cat <<EOF | psql -q $DB
update occupywallst_comment as C
set ups = coalesce((select count(*)
from occupywallst_commentvote as CV
inner join occupywallst_userinfo as UI
on CV.user_id = UI.user_id
where comment_id = C.id
and is_shadow_banned = false
and vote = 1), 0),
downs = coalesce((select count(*)
from occupywallst_commentvote as CV
inner join occupywallst_userinfo as UI
on CV.user_id = UI.user_id
where comment_id = C.id
and is_shadow_banned = false
and vote = -1), 0),
karma = coalesce((select sum(vote)
from occupywallst_commentvote as CV
inner join occupywallst_userinfo as UI
on CV.user_id = UI.user_id
where comment_id = C.id
and is_shadow_banned = false), 0)
where published > now() - interval '30 day';
update occupywallst_userinfo as U
set karma = coalesce((select sum(karma)
from occupywallst_comment as C
where C.user_id = U.user_id
and not is_removed
and not is_deleted), 0)
where U.user_id in (select distinct(user_id)
from occupywallst_comment
where published > now() - interval '30 day');
EOF