def _copy_entity_fields_to_historical_snapshot( self, entity: DatabaseEntity, historical_snapshot: DatabaseEntity) -> None: """ Copies all column values present on |entity| to |historical_snapshot|. NOTE: This method *only* copies values for properties which are present on both the master and historical tables. Any property that is only present on one table will be ignored. The only exception is the master key column, which is copied over regardless of property name (only based on *column* name), following module assumption #2. """ for column_property_name in self._get_shared_column_property_names( type(entity), type(historical_snapshot)): entity_value = getattr(entity, column_property_name) setattr(historical_snapshot, column_property_name, entity_value) # See module assumption #2 key_column_name = entity.get_primary_key_column_name() # type: ignore historical_master_key_property_name = type( historical_snapshot).get_property_name_by_column_name( key_column_name) setattr( historical_snapshot, historical_master_key_property_name, entity.get_primary_key(), ) # type: ignore
def register_entity(self, schema_object: DatabaseEntity) -> None: """Creates (_SnapshotContext) for |entity| and adds it to registry Raises (ValueError) if |entity| has already been registered """ type_name = type(schema_object).__name__ if type_name not in self.snapshot_contexts: self.snapshot_contexts[type_name] = {} entity_id = schema_object.get_primary_key() if entity_id in self.snapshot_contexts[type_name]: raise ValueError( "Entity already registered with type {type} and primary key " "{primary_key}".format(type=type_name, primary_key=entity_id)) self.snapshot_contexts[type_name][entity_id] = _SnapshotContext( schema_object=schema_object)
def snapshot_context(self, entity: DatabaseEntity) -> _SnapshotContext: """Returns (_SnapshotContext) for |entity|""" context_map = self.snapshot_contexts[type(entity).__name__] return context_map[entity.get_primary_key()]