Esempio n. 1
0
    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 ...)
        '''
        logger.debug('Purging revision: %s' % revision.id)
        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()).limit(2).all()
                    if len(trevobjs) == 0:
                        raise Exception('Should have at least one revision.')
                    if len(trevobjs) == 1:
                        to_purge.append(continuity)
                    else:
                        new_correct_revobj = trevobjs[1]  # older one
                        self.revert(continuity, new_correct_revobj)
                # 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 UTC' % datetime.datetime.utcnow()
        else:
            self.session.delete(revision)
        self.commit_and_remove()
Esempio n. 2
0
 def new_revision(self):                                   
     '''Convenience method to create new revision and set it on session.
     
     NB: if in transactional mode do *not* need to call `begin` as we are
     automatically within a transaction at all times if session was set up
     as transactional (every commit is paired with a begin)
     <http://groups.google.com/group/sqlalchemy/browse_thread/thread/a54ce150b33517db/17587ca675ab3674>
     '''
     rev = Revision()
     self.session.add(rev)
     SQLAlchemySession.set_revision(self.session, rev)             
     return rev
Esempio n. 3
0
 def new_revision(self):
     '''Convenience method to create new revision and set it on session.
     
     NB: if in transactional mode do *not* need to call `begin` as we are
     automatically within a transaction at all times if session was set up
     as transactional (every commit is paired with a begin)
     <http://groups.google.com/group/sqlalchemy/browse_thread/thread/a54ce150b33517db/17587ca675ab3674>
     '''
     rev = Revision()
     self.session.add(rev)
     SQLAlchemySession.set_revision(self.session, rev)
     return rev
Esempio n. 4
0
    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 ...)
        '''
        logger.debug('Purging revision: %s' % revision.id)
        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()).limit(2).all()
                    if len(trevobjs) == 0:
                        raise Exception('Should have at least one revision.')
                    if len(trevobjs) == 1:
                        to_purge.append(continuity)
                    else:
                        new_correct_revobj = trevobjs[1] # older one
                        self.revert(continuity, new_correct_revobj)
                # 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 UTC' % datetime.datetime.utcnow()
        else:
            self.session.delete(revision)
        self.commit_and_remove()