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

Create a migrate tips function #3424

Merged
merged 4 commits into from
May 28, 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
28 changes: 28 additions & 0 deletions gratipay/models/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,31 @@ def status(self):
, False: 'rejected'
, True: 'approved'
}[self.is_approved]

def migrate_tips(self):
subscriptions = self.db.all("SELECT * FROM subscriptions WHERE team=%s", (self.slug,))

# Make sure the migration hasn't been done already
if subscriptions:
raise AlreadyMigrated

self.db.run("""

INSERT INTO subscriptions
(ctime, mtime, subscriber, team, amount, is_funded)
SELECT ct.ctime
, ct.mtime
, ct.tipper
, %(slug)s
, ct.amount
, ct.is_funded
FROM current_tips ct
JOIN participants p ON p.username = tipper
WHERE ct.tippee=%(owner)s
AND p.claimed_time IS NOT NULL
AND p.is_suspicious IS NOT TRUE
AND p.is_closed IS NOT TRUE

""", {'slug': self.slug, 'owner': self.owner})

class AlreadyMigrated(Exception): pass
43 changes: 41 additions & 2 deletions tests/py/test_teams.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import unicode_literals
from gratipay.models._mixin_team import StubParticipantAdded

import pytest

from gratipay.models._mixin_team import StubParticipantAdded
from gratipay.testing import Harness
from gratipay.security.user import User
from gratipay.models.team import Team
from gratipay.models.team import Team, AlreadyMigrated


class TestNewTeams(Harness):
Expand Down Expand Up @@ -116,6 +118,43 @@ def test_stripping_required_inputs(self):
assert self.db.one("SELECT COUNT(*) FROM teams") == 0
assert "Please fill out the 'Team Name' field." in r.body

def test_migrate_tips_to_subscriptions(self):
alice = self.make_participant('alice', claimed_time='now')
bob = self.make_participant('bob', claimed_time='now')
self.make_participant('old_team')
alice.set_tip_to('old_team', '1.00')
bob.set_tip_to('old_team', '2.00')
new_team = self.make_team('new_team', owner='old_team')

new_team.migrate_tips()

subscriptions = self.db.all("SELECT * FROM subscriptions")
assert len(subscriptions) == 2
assert subscriptions[0].subscriber == 'alice'
assert subscriptions[0].team == 'new_team'
assert subscriptions[0].amount == 1
assert subscriptions[1].subscriber == 'bob'
assert subscriptions[1].team == 'new_team'
assert subscriptions[1].amount == 2

def test_migrate_tips_only_runs_once(self):
alice = self.make_participant('alice', claimed_time='now')
self.make_participant('old_team')
alice.set_tip_to('old_team', '1.00')
new_team = self.make_team('new_team', owner='old_team')

self.db.run("""
INSERT INTO subscriptions
(ctime, subscriber, team, amount)
VALUES (CURRENT_TIMESTAMP, 'alice', 'new_team', 1)
""")

with pytest.raises(AlreadyMigrated):
new_team.migrate_tips()

subscriptions = self.db.all("SELECT * FROM subscriptions")
assert len(subscriptions) == 1

class TestOldTeams(Harness):

def setUp(self):
Expand Down