From bb18844c52202f79952fc7bf7ab7c6c9e5bf4873 Mon Sep 17 00:00:00 2001 From: Rohit Paul Kuruvilla Date: Fri, 15 May 2015 00:01:18 +0530 Subject: [PATCH] Make sure that migrations don't run twice --- gratipay/models/team.py | 7 +++++++ tests/py/test_teams.py | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/gratipay/models/team.py b/gratipay/models/team.py index 24f9906d5d..300187982b 100644 --- a/gratipay/models/team.py +++ b/gratipay/models/team.py @@ -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 @@ -66,3 +72,4 @@ def migrate_tips(self): """, {'slug': self.slug, 'owner': self.owner}) +class AlreadyMigrated(Exception): pass diff --git a/tests/py/test_teams.py b/tests/py/test_teams.py index 23626c3d07..7f6cf75f46 100644 --- a/tests/py/test_teams.py +++ b/tests/py/test_teams.py @@ -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): @@ -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):