def purge_revision(self, revision, leave_record=False): """Purge all changes associated with a revision. @param leave_record: if True leave revision in existence but change message to "PURGED: {date-time-of-purge}". If false delete revision object as well. Summary of the Algorithm ------------------------ 1. list all RevisionObjects affected by this revision 2. check continuity objects and cascade on everything else ? 3. crudely get all object revisions associated with this 4. then check whether this is the only revision and delete the continuity object 5. ALTERNATIVELY delete all associated object revisions then do a select on continutity to check which have zero associated revisions (should only be these ...) """ to_purge = [] SQLAlchemySession.setattr(self.session, "revisioning_disabled", True) self.session.autoflush = False for o in self.versioned_objects: revobj = o.__revision_class__ items = self.session.query(revobj).filter_by(revision=revision).all() for item in items: continuity = item.continuity if continuity.revision == revision: # must change continuity trevobjs = ( self.session.query(revobj) .join("revision") .filter(revobj.continuity == continuity) .order_by(Revision.timestamp.desc()) .all() ) if len(trevobjs) == 0: raise Exception("Should have at least one revision.") if len(trevobjs) == 1: to_purge.append(continuity) else: self.revert(continuity, trevobjs[1]) for num, obj in enumerate(trevobjs): if num == 0: continue if "pending" not in obj.state: obj.current = True obj.expired_timestamp = datetime(9999, 12, 31) self.session.add(obj) break # now delete revision object self.session.delete(item) for cont in to_purge: self.session.delete(cont) if leave_record: revision.message = u"PURGED: %s" % datetime.now() else: self.session.delete(revision) self.commit_and_remove()
def purge_revision(self, revision, leave_record=False): '''Purge all changes associated with a revision. @param leave_record: if True leave revision in existence but change message to "PURGED: {date-time-of-purge}". If false delete revision object as well. Summary of the Algorithm ------------------------ 1. list all RevisionObjects affected by this revision 2. check continuity objects and cascade on everything else ? 1. crudely get all object revisions associated with this 2. then check whether this is the only revision and delete the continuity object 3. ALTERNATIVELY delete all associated object revisions then do a select on continutity to check which have zero associated revisions (should only be these ...) ''' to_purge = [] SQLAlchemySession.setattr(self.session, 'revisioning_disabled', True) self.session.autoflush = False for o in self.versioned_objects: revobj = o.__revision_class__ items = self.session.query(revobj).filter_by(revision=revision).all() for item in items: continuity = item.continuity if continuity.revision == revision: # need to change continuity trevobjs = self.session.query(revobj).join('revision'). filter( revobj.continuity==continuity ).order_by(Revision.timestamp.desc()).all() if len(trevobjs) == 0: raise Exception('Should have at least one revision.') if len(trevobjs) == 1: to_purge.append(continuity) else: self.revert(continuity, trevobjs[1]) for num, obj in enumerate(trevobjs): if num == 0: continue if 'pending' not in obj.state: obj.current = True import datetime obj.expired_timestamp = datetime.datetime(9999, 12, 31) self.session.add(obj) break # now delete revision object self.session.delete(item) for cont in to_purge: self.session.delete(cont) if leave_record: import datetime revision.message = u'PURGED: %s' % datetime.datetime.now() else: self.session.delete(revision) self.commit_and_remove()
def __create_sparl_resource(self, packagedb, endpoint_url): self.__delete_sparl_resource(packagedb) # Little hack to create a Revision for the SQLAlchemy Session provided by CKAN. Otherwise resource insertion fails because "No revision is currently set for this Session". SQLAlchemySession.set_revision(model.Session, Revision()) packagedb.add_resource(url=endpoint_url, format=u'api/sparql', description=u'SPARQL endpoint', name=u'SPARQL endpoint', resource_type=u'api', extras={'generated_by_ckanextsparql': True}) model.Session.commit()
def make_latest_pending_package_active(context, data_dict): ''' .. todo:: What does this function do? You must be authorized to update the dataset. :param id: the name or id of the dataset, e.g. ``'warandpeace'`` :type id: string ''' model = context['model'] session = model.Session SQLAlchemySession.setattr(session, 'revisioning_disabled', True) id = _get_or_bust(data_dict, "id") pkg = model.Package.get(id) _check_access('make_latest_pending_package_active', context, data_dict) #packages q = session.query(model.PackageRevision).filter_by(id=pkg.id) _make_latest_rev_active(context, q) #resources for resource in pkg.resource_groups_all[0].resources_all: q = session.query(model.ResourceRevision).filter_by(id=resource.id) _make_latest_rev_active(context, q) #tags for tag in pkg.package_tag_all: q = session.query(model.PackageTagRevision).filter_by(id=tag.id) _make_latest_rev_active(context, q) #extras for extra in pkg.extras_list: q = session.query(model.PackageExtraRevision).filter_by(id=extra.id) _make_latest_rev_active(context, q) latest_revision = context.get('latest_revision') if not latest_revision: return q = session.query(model.Revision).filter_by(id=latest_revision) revision = q.first() revision.approved_timestamp = datetime.datetime.now() session.add(revision) if not context.get('defer_commit'): session.commit()
def purge_revision(self, revision, leave_record=False): '''Purge revisions.''' SQLAlchemySession.setattr(self.session, 'revisioning_disabled', True) self.session.autoflush = False for o in self.versioned_objects: revobj = o.__revision_class__ items = self.session.query(revobj). \ filter_by(revision=revision).all() for item in items: self.session.delete(item) if leave_record: revision.message = u'PURGED: %s' % datetime.now() else: self.session.delete(revision) self.commit_and_remove()
def make_latest_pending_package_active(context, data_dict): model = context['model'] session = model.Session SQLAlchemySession.setattr(session, 'revisioning_disabled', True) id = data_dict["id"] pkg = model.Package.get(id) check_access('make_latest_pending_package_active', context, data_dict) #packages q = session.query(model.PackageRevision).filter_by(id=pkg.id) _make_latest_rev_active(context, q) #resources for resource in pkg.resource_groups_all[0].resources_all: q = session.query(model.ResourceRevision).filter_by(id=resource.id) _make_latest_rev_active(context, q) #tags for tag in pkg.package_tag_all: q = session.query(model.PackageTagRevision).filter_by(id=tag.id) _make_latest_rev_active(context, q) #extras for extra in pkg.extras_list: q = session.query(model.PackageExtraRevision).filter_by(id=extra.id) _make_latest_rev_active(context, q) latest_revision = context.get('latest_revision') if not latest_revision: return q = session.query(model.Revision).filter_by(id=latest_revision) revision = q.first() revision.approved_timestamp = datetime.datetime.now() session.add(revision) if not context.get('defer_commit'): session.commit() session.remove()