fix id handling and remove command

Ensure that anything we are going to need a foreign key for is assigned
an id before we set up the relationship.

Fix the remove command to delete data correctly after the add code was
refactored earlier.
This commit is contained in:
Doug Hellmann 2014-11-26 23:14:14 +00:00
parent 1e5dd80a5f
commit 3d0a501dd3
1 changed files with 15 additions and 7 deletions

View File

@ -81,6 +81,8 @@ class ProjectManager(object):
proj_obj = Project(name=name, path=path)
LOG.info('adding project %s from %s', name, path)
self.session.add(proj_obj)
self.session.flush()
assert proj_obj.id, 'No id for new project'
self.update(proj_obj, force=force)
return proj_obj
@ -96,15 +98,11 @@ class ProjectManager(object):
LOG.info('removing project %s', name)
except NoResultFound:
return
self._delete_filehandler_data_from_project(proj_obj)
for file_obj in proj_obj.files:
self._remove_plugin_data_for_file(file_obj)
self.session.delete(proj_obj)
def _remove_file_data(self, file_obj, reason='file has changed'):
"""Delete the data associated with the file, including plugin data and
file contents.
"""
LOG.debug('removing cached contents of %s: %s', file_obj.name, reason)
def _remove_plugin_data_for_file(self, file_obj):
# We have to explicitly have the handlers delete their data
# because the parent-child relationship of the tables is reversed
# because the plugins define the relationships.
@ -112,6 +110,14 @@ class ProjectManager(object):
if fh.obj.supports_file(file_obj):
LOG.debug('removing %s plugin data for %s', fh.name, file_obj.name)
fh.obj.delete_data_for_file(self.session, file_obj)
def _remove_file_data(self, file_obj, reason='file has changed'):
"""Delete the data associated with the file, including plugin data and
file contents.
"""
LOG.debug('removing cached contents of %s: %s', file_obj.name, reason)
self._remove_plugin_data_for_file(file_obj)
self.session.query(Line).filter(Line.file_id == file_obj.id).delete()
self.session.delete(file_obj)
@ -147,6 +153,8 @@ class ProjectManager(object):
pass
new_file = File(project=proj_obj, name=filename, path=fullname, sha=sha)
self.session.add(new_file)
self.session.flush() # make sure new_file gets an id
assert new_file.id, 'No id for new file'
if any(fnmatch.fnmatch(filename, dnr) for dnr in self._DO_NOT_READ):
LOG.debug('ignoring contents of %s', fullname)
else: