Fix error when deleting project

When deleting a project, an error like:

  delete statement on table change_conflict expected to delete X rows

might occur.  This is because the self-referential many-to-many
relationship of change conflicts was trying to delete entries from
the secondary table twice.  Handle the case of deleting a project
better.

Change-Id: Ib919e36ede14a3a1054fcd6b95ea43ccae046d3e
This commit is contained in:
James E. Blair 2016-10-31 14:15:34 -07:00
parent 70b167ce6a
commit c20ec64ad8
2 changed files with 21 additions and 1 deletions

View File

@ -200,6 +200,25 @@ class Project(object):
self.subscribed = subscribed
self.description = description
def delete(self):
# This unusual delete method is to accomodate the
# self-referential many-to-many relationship from
# change_conflict. With the default cascade configuration,
# the entry from the association table is deleted regardless
# of whether the change being deleted is change1 or change2.
# However, when both changes are deleted at once (only likely
# to happen when the entire project is deleted), SQLAlchemy
# cascades through both relationships and attempts to delete
# the entry twice. Since we rarely delete projects, we add a
# special case here to delete a project's changes one at a
# time to avoid this situation.
session = Session.object_session(self)
for c in self.changes:
session.delete(c)
session.flush()
session.expire_all()
session.delete(self)
def createChange(self, *args, **kw):
session = Session.object_session(self)
args = [self] + list(args)

View File

@ -236,7 +236,8 @@ class SyncProjectListTask(Task):
local_keys = set(local.keys())
for name in local_keys-remote_keys:
session.delete(local[name])
self.log.info("Deleted project %s", name)
local[name].delete()
for name in remote_keys-local_keys:
p = remote[name]