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

Commit

Permalink
Make sure that migrations don't run twice
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed May 14, 2015
1 parent 60da70d commit bb18844
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions gratipay/models/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def create_new(cls, owner, fields):
owner.username))

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
Expand All @@ -66,3 +72,4 @@ def migrate_tips(self):
""", {'slug': self.slug, 'owner': self.owner})

class AlreadyMigrated(Exception): pass
23 changes: 21 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 @@ -103,6 +105,23 @@ def test_migrate_tips_to_subscriptions(self):
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):

Expand Down

0 comments on commit bb18844

Please sign in to comment.