Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1902 from gittip/repair-paydays
Browse files Browse the repository at this point in the history
Repair paydays and add self checks (fixes #204).
  • Loading branch information
chadwhitacre committed Jan 17, 2014
2 parents 95a9aa4 + 2b9ae1d commit dcd1621
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
69 changes: 69 additions & 0 deletions gittip/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def self_check(self):
self._check_tips()
self._check_orphans()
self._check_orphans_no_tips()
self._check_paydays_volumes()

def _check_tips(self):
"""
Expand Down Expand Up @@ -138,4 +139,72 @@ def _check_orphans_no_tips(self):
known = set([25206]) # '4c074000c7bc', 'naderman', '3.00'
real = set(tips_with_orphans) - known
assert len(real) == 0, real

def _check_paydays_volumes(self):
"""
Recalculate *_volume fields in paydays table using exchanges table.
"""
charge_volume = self.all("""
select * from (
select id, ts_start, charge_volume, (
select sum(amount+fee)
from exchanges
where timestamp > ts_start
and timestamp < ts_end
and amount > 0
) as ref
from paydays
order by id
) as foo
where charge_volume != ref
""")
assert len(charge_volume) == 0

charge_fees_volume = self.all("""
select * from (
select id, ts_start, charge_fees_volume, (
select sum(fee)
from exchanges
where timestamp > ts_start
and timestamp < ts_end
and amount > 0
) as ref
from paydays
order by id
) as foo
where charge_fees_volume != ref
""")
assert len(charge_fees_volume) == 0

ach_volume = self.all("""
select * from (
select id, ts_start, ach_volume, (
select sum(amount)
from exchanges
where timestamp > ts_start
and timestamp < ts_end
and amount < 0
) as ref
from paydays
order by id
) as foo
where ach_volume != ref
""")
assert len(ach_volume) == 0

ach_fees_volume = self.all("""
select * from (
select id, ts_start, ach_fees_volume, (
select sum(fee)
from exchanges
where timestamp > ts_start
and timestamp < ts_end
and amount < 0
) as ref
from paydays
order by id
) as foo
where ach_fees_volume != ref
""")
assert len(ach_fees_volume) == 0
#
17 changes: 17 additions & 0 deletions repair.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
update paydays
set charge_fees_volume = (
select sum(fee)
from exchanges
where timestamp > ts_start
and timestamp < ts_end
and amount > 0
);

update paydays
set ach_volume = (
select sum(amount)
from exchanges
where timestamp > ts_start
and timestamp < ts_end
and amount < 0
);

0 comments on commit dcd1621

Please sign in to comment.