From 3d8fb4413b4370181a040f4a3a8f2c094a3bb088 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Mon, 24 Apr 2017 09:31:29 -0400 Subject: [PATCH] Try to trigger an IntegrityError --- gratipay/models/package/__init__.py | 12 +++++++++--- gratipay/models/participant/email.py | 1 + gratipay/models/team/package.py | 5 ++++- sql/branch.sql | 5 ++++- tests/py/test_packages.py | 11 ++++++++++- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/gratipay/models/package/__init__.py b/gratipay/models/package/__init__.py index 4514d15ad1..a0591a1d0f 100644 --- a/gratipay/models/package/__init__.py +++ b/gratipay/models/package/__init__.py @@ -76,10 +76,16 @@ def ensure_team(self, cursor, owner): , owner=owner , _cursor=cursor ) - cursor.run('UPDATE packages SET team_id=%s WHERE id=%s', (team.id, self.id)) - self.set_attributes(team_id=team.id) + cursor.run('INSERT INTO teams_to_packages (team_id, package_id) ' + 'VALUES (%s, %s)', (team.id, self.id)) + from aspen import log + log('inserted into teams') + log(cursor.all('SELECT * FROM teams_to_packages')) return team def _load_team(self, cursor): - return cursor.one('SELECT t.*::teams FROM teams t WHERE t.id=%s', (self.team_id,)) + return cursor.one( 'SELECT t.*::teams FROM teams t WHERE t.id=' + '(SELECT team_id FROM teams_to_packages tp WHERE tp.package_id=%s)' + , (self.id,) + ) diff --git a/gratipay/models/participant/email.py b/gratipay/models/participant/email.py index 42758213e2..e47cafe5b5 100644 --- a/gratipay/models/participant/email.py +++ b/gratipay/models/participant/email.py @@ -245,6 +245,7 @@ def finish_email_verification(self, email, nonce): try: self.finish_package_claims(cursor, nonce, *packages) self.save_email_address(cursor, email) + cursor.connection.commit() except IntegrityError: return VERIFICATION_STYMIED return VERIFICATION_SUCCEEDED diff --git a/gratipay/models/team/package.py b/gratipay/models/team/package.py index dd33f7b76b..50de9a622a 100644 --- a/gratipay/models/team/package.py +++ b/gratipay/models/team/package.py @@ -16,4 +16,7 @@ def package(self): def _load_package(self, cursor): - return cursor.one('SELECT p.*::packages FROM packages p WHERE p.team_id=%s', (self.id,)) + return cursor.one( 'SELECT p.*::packages FROM packages p WHERE p.id=' + '(SELECT package_id FROM teams_to_packages tp WHERE tp.team_id=%s)' + , (self.id,) + ) diff --git a/sql/branch.sql b/sql/branch.sql index 8991d23794..4052aa0ec0 100644 --- a/sql/branch.sql +++ b/sql/branch.sql @@ -9,6 +9,9 @@ BEGIN; , UNIQUE(nonce, package_id) ); - ALTER TABLE packages ADD COLUMN team_id bigint UNIQUE REFERENCES teams(id) ON DELETE RESTRICT; + CREATE TABLE teams_to_packages + ( team_id bigint UNIQUE REFERENCES teams(id) ON DELETE RESTRICT + , package_id bigint UNIQUE REFERENCES packages(id) ON DELETE RESTRICT + ); END; diff --git a/tests/py/test_packages.py b/tests/py/test_packages.py index 23f9f10e46..1d48d5e5d9 100644 --- a/tests/py/test_packages.py +++ b/tests/py/test_packages.py @@ -51,6 +51,15 @@ def test_team_can_only_be_linked_from_one_package(self): bar = self.make_package(name='bar') raises( IntegrityError , self.db.run - , 'UPDATE packages SET team_id=%s WHERE id=%s' + , 'INSERT INTO teams_to_packages (team_id, package_id) VALUES (%s, %s)' , (team.id, bar.id) ) + + def test_package_can_only_be_linked_from_one_team(self): + alice, package, team = self.test_can_link_to_a_new_team() + bar = self.make_team(name='Bar') + raises( IntegrityError + , self.db.run + , 'INSERT INTO teams_to_packages (team_id, package_id) VALUES (%s, %s)' + , (bar.id, package.id) + )