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

stop Python-interpolating payday.sql #3785

Merged
merged 1 commit into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gratipay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def payin(self):
money internally between participants.
"""
with self.db.get_cursor() as cursor:
self.prepare(cursor, self.ts_start)
self.prepare(cursor)
holds = self.create_card_holds(cursor)
self.process_payment_instructions(cursor)
self.transfer_takes(cursor, self.ts_start)
Expand All @@ -172,10 +172,10 @@ def payin(self):


@staticmethod
def prepare(cursor, ts_start):
def prepare(cursor):
"""Prepare the DB: we need temporary tables with indexes and triggers.
"""
cursor.run(PAYDAY, dict(ts_start=ts_start))
cursor.run(PAYDAY)
log('Prepared the DB.')


Expand Down
14 changes: 14 additions & 0 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BEGIN;

CREATE FUNCTION current_payday() RETURNS paydays AS $$
SELECT *
FROM paydays
WHERE ts_end='1970-01-01T00:00:00+00'::timestamptz;
$$ LANGUAGE sql;

CREATE FUNCTION current_payday_id() RETURNS int AS $$
-- This is a function so we can use it in DEFAULTS for a column.
SELECT id FROM current_payday();
$$ LANGUAGE sql;

END;
6 changes: 3 additions & 3 deletions sql/payday.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CREATE TABLE payday_participants AS
, braintree_customer_id
FROM participants p
WHERE is_suspicious IS NOT true
AND claimed_time < %(ts_start)s
AND claimed_time < (SELECT ts_start FROM current_payday())
ORDER BY claimed_time;

CREATE UNIQUE INDEX ON payday_participants (id);
Expand Down Expand Up @@ -51,14 +51,14 @@ DROP TABLE IF EXISTS payday_payments_done;
CREATE TABLE payday_payments_done AS
SELECT *
FROM payments p
WHERE p.timestamp > %(ts_start)s;
WHERE p.timestamp > (SELECT ts_start FROM current_payday());

DROP TABLE IF EXISTS payday_payment_instructions;
CREATE TABLE payday_payment_instructions AS
SELECT s.id, participant, team, amount, due
FROM ( SELECT DISTINCT ON (participant, team) *
FROM payment_instructions
WHERE mtime < %(ts_start)s
WHERE mtime < (SELECT ts_start FROM current_payday())
ORDER BY participant, team, mtime DESC
) s
JOIN payday_participants p ON p.username = s.participant
Expand Down
26 changes: 12 additions & 14 deletions tests/py/test_billing_payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,16 @@ def test_start_prepare(self, log):
self.make_participant('carl', balance=10, claimed_time='now')

payday = Payday.start()
ts_start = payday.ts_start

get_participants = lambda c: c.all("SELECT * FROM payday_participants")

with self.db.get_cursor() as cursor:
payday.prepare(cursor, ts_start)
payday.prepare(cursor)
participants = get_participants(cursor)

expected_logging_call_args = [
('Starting a new payday.'),
('Payday started at {}.'.format(ts_start)),
('Payday started at {}.'.format(payday.ts_start)),
('Prepared the DB.'),
]
expected_logging_call_args.reverse()
Expand All @@ -191,13 +190,12 @@ def test_start_prepare(self, log):
log.reset_mock()

# run a second time, we should see it pick up the existing payday
payday = Payday.start()
second_ts_start = payday.ts_start
second_payday = Payday.start()
with self.db.get_cursor() as cursor:
payday.prepare(cursor, second_ts_start)
payday.prepare(cursor)
second_participants = get_participants(cursor)

assert ts_start == second_ts_start
assert payday.ts_start == second_payday.ts_start
participants = list(participants)
second_participants = list(second_participants)

Expand All @@ -207,7 +205,7 @@ def test_start_prepare(self, log):

expected_logging_call_args = [
('Picking up with an existing payday.'),
('Payday started at {}.'.format(second_ts_start)),
('Payday started at {}.'.format(second_payday.ts_start)),
('Prepared the DB.'),
]
expected_logging_call_args.reverse()
Expand Down Expand Up @@ -238,7 +236,7 @@ class TestPayin(BillingHarness):
def create_card_holds(self):
payday = Payday.start()
with self.db.get_cursor() as cursor:
payday.prepare(cursor, payday.ts_start)
payday.prepare(cursor)
return payday.create_card_holds(cursor)

@mock.patch.object(Payday, 'fetch_card_holds')
Expand Down Expand Up @@ -369,7 +367,7 @@ def test_payin_cant_make_balances_more_negative(self):
""")
payday = Payday.start()
with self.db.get_cursor() as cursor:
payday.prepare(cursor, payday.ts_start)
payday.prepare(cursor)
cursor.run("""
UPDATE payday_participants
SET new_balance = -50
Expand Down Expand Up @@ -398,7 +396,7 @@ def test_process_payment_instructions(self):

payday = Payday.start()
with self.db.get_cursor() as cursor:
payday.prepare(cursor, payday.ts_start)
payday.prepare(cursor)
payday.process_payment_instructions(cursor)
assert cursor.one("select balance from payday_teams where slug='TheEnterprise'") == D('0.51')
assert cursor.one("select balance from payday_teams where slug='TheTrident'") == 0
Expand Down Expand Up @@ -429,7 +427,7 @@ def test_transfer_takes(self):
# have already been processed
for i in range(3):
with self.db.get_cursor() as cursor:
payday.prepare(cursor, payday.ts_start)
payday.prepare(cursor)
payday.transfer_takes(cursor, payday.ts_start)
payday.update_balances(cursor)

Expand All @@ -453,7 +451,7 @@ def test_process_draws(self):

payday = Payday.start()
with self.db.get_cursor() as cursor:
payday.prepare(cursor, payday.ts_start)
payday.prepare(cursor)
payday.process_payment_instructions(cursor)
payday.transfer_takes(cursor, payday.ts_start)
payday.process_draws(cursor)
Expand Down Expand Up @@ -491,7 +489,7 @@ def test_take_over_during_payin(self):
alice.set_tip_to(bob, 18)
payday = Payday.start()
with self.db.get_cursor() as cursor:
payday.prepare(cursor, payday.ts_start)
payday.prepare(cursor)
bruce = self.make_participant('bruce', claimed_time='now')
bruce.take_over(('twitter', str(bob.id)), have_confirmation=True)
payday.process_payment_instructions(cursor)
Expand Down