def test_lifecycle(self): memento_changes = [ memento_change(self.model, [self.id_counter], None, 'create'), memento_change(self.model, [self.id_counter], {'name': 'foo'}, 'before_update'), memento_change(self.model, [self.id_counter], {'name': 'bar'}, 'before_delete'), ] self.memento.register_changes(memento_changes) changes = list( self.memento.get_changes(self.model, [self.id_counter], {})) self.assertEqual(len(changes), 3)
def flush(self, entity_instance): """Flush the pending changes of this entity instance to the backend""" from sqlalchemy.orm.session import Session session = Session.object_session( entity_instance ) if session: objects_to_flush = set([entity_instance]) self._expand_compounding_objects( objects_to_flush ) # # Create a list of changes # changes = [] for obj_to_flush in objects_to_flush: if obj_to_flush in session.dirty: modifications = {} try: modifications = self.get_modifications( obj_to_flush ) except Exception, e: # todo : there seems to be a bug in sqlalchemy that causes the # get history to fail in some cases logger.error( 'could not get modifications from object', exc_info = e ) primary_key = self.primary_key( obj_to_flush ) if modifications and (None not in primary_key): change = memento_change( model = unicode( self.entity.__name__ ), memento_type = 'before_update', primary_key = primary_key, previous_attributes = modifications ) changes.append( change ) session.flush( objects_to_flush ) # # If needed, track the changes # memento = self.get_memento() if changes and memento != None: memento.register_changes( changes )
def delete(self, entity_instance): """Delete an entity instance""" session = Session.object_session( entity_instance ) # # new and deleted instances cannot be deleted # if session: if entity_instance in session.new: session.expunge(entity_instance) elif entity_instance not in session.deleted: # # only if we know the primary key, we can keep track of its history # primary_key = self.primary_key( entity_instance ) if not None in primary_key: # save the state before the update memento = self.get_memento() if memento != None: modifications = entity_to_dict( entity_instance ) change = memento_change( model = unicode( self.entity.__name__ ), memento_type = 'before_delete', primary_key = primary_key, previous_attributes = modifications ) memento.register_changes( [change] ) session.delete( entity_instance ) self.flush( entity_instance )
def test_no_error( self ): memento_changes = [ memento_change( None, [self.id_counter], None, None ), ] self.memento.register_changes( memento_changes )
def flush(self, entity_instance): """Flush the pending changes of this entity instance to the backend""" from sqlalchemy.orm.session import Session session = Session.object_session( entity_instance ) if session: modifications = {} try: modifications = self.get_modifications( entity_instance ) except Exception, e: # todo : there seems to be a bug in sqlalchemy that causes the # get history to fail in some cases logger.error( 'could not get modifications from object', exc_info = e ) session.flush( [entity_instance] ) # # If needed, track the changes # primary_key = self.primary_key( entity_instance ) if modifications and (None not in primary_key): memento = self.get_memento() if memento != None: change = memento_change( model = unicode( self.entity.__name__ ), memento_type = 'before_update', primary_key = primary_key, previous_attributes = modifications ) memento.register_changes( [change] )
def delete(self, entity_instance): """Delete an entity instance""" from sqlalchemy.orm.session import Session session = Session.object_session( entity_instance ) # # new and deleted instances cannot be deleted # if session: if entity_instance in session.new: session.expunge(entity_instance) elif (entity_instance not in session.deleted) and \ (entity_instance in session): # if the object is not in the session, it might already be deleted # # only if we know the primary key, we can keep track of its history # primary_key = self.primary_key( entity_instance ) if not None in primary_key: # save the state before the update memento = self.get_memento() if memento != None: modifications = dict() change = memento_change( model = unicode( self.entity.__name__ ), memento_type = 'before_delete', primary_key = primary_key, previous_attributes = modifications ) memento.register_changes( [change] ) session.delete( entity_instance ) session.flush( [entity_instance] )
def delete(self, entity_instance): """Delete an entity instance""" session = Session.object_session(entity_instance) # # new and deleted instances cannot be deleted # if session: if entity_instance in session.new: session.expunge(entity_instance) elif entity_instance not in session.deleted: # # only if we know the primary key, we can keep track of its history # primary_key = self.primary_key(entity_instance) if not None in primary_key: # save the state before the update memento = self.get_memento() if memento != None: modifications = entity_to_dict(entity_instance) change = memento_change( model=six.text_type(self.entity.__name__), memento_type='before_delete', primary_key=primary_key, previous_attributes=modifications) memento.register_changes([change]) session.delete(entity_instance) session.flush()
def test_custom_memento_type(self): memento_changes = [ memento_change(self.model, [self.id_counter], {}, 'custom'), ] self.memento.register_changes(memento_changes) changes = list( self.memento.get_changes(self.model, [self.id_counter], {})) self.assertEqual(len(changes), 1)
def test_lifecycle( self ): memento_changes = [ memento_change( self.model, [self.id_counter], None, 'create' ), memento_change( self.model, [self.id_counter], {'name':'foo'}, 'before_update' ), memento_change( self.model, [self.id_counter], {'name':'bar'}, 'before_delete' ), ] self.memento.register_changes( memento_changes ) changes = list( self.memento.get_changes( self.model, [self.id_counter], {} ) ) self.assertEqual( len(changes), 3 )
def test_custom_memento_type( self ): memento_changes = [ memento_change( self.model, [self.id_counter], {}, 'custom' ), ] self.memento.register_changes( memento_changes ) changes = list( self.memento.get_changes( self.model, [self.id_counter], {} ) ) self.assertEqual( len(changes), 1 )
def flush(self, entity_instance): """Flush the pending changes of this entity instance to the backend""" from sqlalchemy.orm.session import Session session = Session.object_session(entity_instance) if session: objects_to_flush = set([entity_instance]) self._expand_compounding_objects(objects_to_flush) # # Create a list of changes # changes = [] for obj_to_flush in objects_to_flush: if obj_to_flush in session.dirty: modifications = {} try: modifications = self.get_modifications(obj_to_flush) except Exception as e: # todo : there seems to be a bug in sqlalchemy that causes the # get history to fail in some cases logger.error('could not get modifications from object', exc_info=e) primary_key = self.primary_key(obj_to_flush) if modifications and (None not in primary_key): change = memento_change( model=six.text_type(type(obj_to_flush).__name__), memento_type='before_update', primary_key=primary_key, previous_attributes=modifications) changes.append(change) session.flush(objects_to_flush) # # If needed, track the changes # memento = self.get_memento() if changes and memento != None: memento.register_changes(changes)
def test_no_error(self): memento_changes = [ memento_change(None, [self.id_counter], None, None), ] self.memento.register_changes(memento_changes)