def version_obj(session, parent_obj): manager = get_versioning_manager(parent_obj) uow = manager.unit_of_work(session) for version_obj in uow.version_session: if (parent_class(version_obj.__class__) == parent_obj.__class__ and identity(version_obj)[:-1] == identity(parent_obj)): return version_obj
async def deepcopy_recursive( model: Model, object_id: str, new_fk_link_id: Optional[str] = None, fk_column: Optional[Column] = None, new_id: Optional[str] = None, ): logger.debug( f"Making a deepcopy of {model} with id {object_id} linking to foreign key" f" {fk_column} with id {new_fk_link_id}" ) new_obj_key = await create_object_copy( model.__tablename__, object_id, fk_column, new_fk_link_id, new_id=new_id ) primary_key_col = identity(model)[0] dependent_models = {} # TODO(ehborisov): check how it works in the case of composite key for m_id, data in cfg.models.items(): for column in cfg.app.db.tables[m_id].columns: if column.references(primary_key_col): dependent_models[data["model"]] = column for dep_model in dependent_models: fk_column = dependent_models[dep_model] all_referencing_instance_ids = ( await dep_model.select(identity(dep_model)[0].name) .where(fk_column == object_id) .gino.all() ) # TODO(ehborisov): can gather be used there? Only if we have a connection pool? for inst_id in all_referencing_instance_ids: await deepcopy_recursive(dep_model, inst_id[0], new_obj_key, fk_column) logger.debug(f"Finished copying, returning newly created object's id {new_obj_key}") return new_obj_key
def version_obj(session, parent_obj): manager = get_versioning_manager(parent_obj) uow = manager.unit_of_work(session) for version_obj in uow.version_session: if ( parent_class(version_obj.__class__) == parent_obj.__class__ and identity(version_obj)[:-1] == identity(parent_obj) ): return version_obj
async def deepcopy_recursive( model: Model, object_id: str, model_data: Dict, *, new_fk_link_id: Optional[str] = None, fk_column: Optional[Column] = None, new_id: Optional[str] = None, ): logger.debug( f"Making a deepcopy of {model} with id {object_id} linking to foreign key" f" {fk_column} with id {new_fk_link_id}" ) new_obj_key = await create_object_copy( model.__tablename__, object_id, model_data, fk_column=fk_column, new_fk_link_id=new_fk_link_id, new_id=new_id, ) if len(identity(model)) == 0: primary_key_col = object_id return ( "Deepcopy does not available for tables without primary keys right now", "error", ) else: primary_key_col = identity(model)[0] dependent_models = {} # TODO(ehborisov): check how it works in the case of composite key for m_id, data in cfg.models.items(): table_name = get_table_name(m_id) for column in cfg.app.db.tables[table_name].columns: if column.references(primary_key_col): dependent_models[data["model"]] = column for dep_model in dependent_models: fk_column = dependent_models[dep_model] all_referencing_instance_ids = ( await dep_model.select(identity(dep_model)[0].name) .where(fk_column == object_id[primary_key_col.name]) .gino.all() ) # TODO(ehborisov): can gather be used there? Only if we have a connection pool? for inst_id in all_referencing_instance_ids: result = await deepcopy_recursive( dep_model, {identity(dep_model)[0].name: inst_id[0]}, model_data, new_fk_link_id=new_obj_key[identity(model)[0].name], fk_column=fk_column, ) if isinstance(result, tuple): return result logger.debug(f"Finished copying, returning newly created object's id {new_obj_key}") return new_obj_key
def __eq__(self, other): discriminator = six.text_type(type(other).__name__) q = self.property._discriminator_col == discriminator other_id = identity(other) for index, id in enumerate(self.property._id_cols): q &= id == other_id[index] return q
def wrapper(self, mapper, connection, target): if not self.manager.options['versioning']: return if not is_versioned(target): return # we cannot use target._sa_instance_state.identity here since object's # is not yet updated at this phase key = (target.__class__, identity(target)) return func(self, key, target)
def test_identity_for_class(self): assert identity(self.Building) == (self.Building.id, )
def test_for_transient_class_with_id(self): building = self.Building(name=u'Some building') self.session.add(building) self.session.flush() assert identity(building) == (building.id, )
def test_for_transient_class_without_id(self): assert identity(self.Building()) == (None, )
def test_for_transient_class_without_id(self): assert identity(self.Building()) is None
def test_identity_for_class(self, Building): assert identity(Building) == (Building.id, )
def test_for_transient_class_without_id(self, Building): assert identity(Building()) == (None, )