Example #1
0
    def flush(self):
        """Flush all pending changes of this session to a database
        (doesn't commit)"""
        from eplasty.object.const import UNCHANGED, DELETED
        cursor = self.cursor()
        queue = self.objects[:]
        for object_ in queue_iterator(queue):
            if object_._status != UNCHANGED and not object_._flushed:
                if object_._has_unflushed_dependencies():
                    queue.append(object_)
                    continue
                try:
                    object_.flush(self, cursor)
                    object_._flushed = True
                except ProgrammingError as err:
                    if err.pgcode == UNDEFINED_TABLE:
                        self._rollback(True)
                        type(object_).create_table(cursor)
                        self.flush()
                        return
                    else:
                        self._rollback(False)
                        raise

        self.connection.commit()
        for object_ in self.objects:
            if object_._flushed and object_._status != DELETED:
                object_._status = UNCHANGED
                object_._flushed = False

        for tname, collection in self.nopk_objects.items():
            self.pk_objects.setdefault(tname, {})
            for object_ in collection:
                self.pk_objects[tname][object_.get_pk_value()] = object_
            del collection[:]
Example #2
0
 def test_queue(self):
     """Test the queue_iterator"""
     input = ['aa', 'ba', 'ab', 'bb', 'ac', 'bc']
     result = []
     for el in util.queue_iterator(input):
         if el[0] == 'a':
             input.append('x' + el)
         else:
             result.append(el)
             
     self.assertEqual(
         result, ['ba', 'bb', 'bc', 'xaa', 'xab', 'xac']
     )