def edit_history_event(self, event: HistoryBaseEntry) -> Tuple[bool, str]: """Edit a history entry to the DB. Returns the edited entry""" cursor = self.db.conn.cursor() try: cursor.execute( 'UPDATE history_events SET event_identifier=?, sequence_index=?, timestamp=?, ' 'location=?, location_label=?, asset=?, amount=?, usd_value=?, notes=?, ' 'type=?, subtype=?, counterparty=?, extra_data=? WHERE identifier=?', (*event.serialize_for_db(), event.identifier), ) except sqlcipher.IntegrityError: # pylint: disable=no-member msg = ( f'Tried to edit event to have event_identifier {event.event_identifier} and ' f'sequence_index {event.sequence_index} but it already exists' ) return False, msg if cursor.rowcount != 1: msg = f'Tried to edit event with id {event.identifier} but could not find it in the DB' return False, msg cursor.execute( 'INSERT OR IGNORE INTO history_events_mappings(parent_identifier, value) ' 'VALUES(?, ?)', (event.identifier, HISTORY_MAPPING_CUSTOMIZED), ) self.db.update_last_write() return True, ''
def add_history_event( self, event: HistoryBaseEntry, mapping_value: Optional[str] = None, ) -> int: """Insert a single history entry to the DB. Returns its identifier. Optionally map it to a specific value used to map attributes to some events May raise: - DeserializationError if the event could not be serialized for the DB - sqlcipher.DatabaseError: If anything went wrong at insertion """ cursor = self.db.conn.cursor() cursor.execute(HISTORY_INSERT, event.serialize_for_db()) identifier = cursor.lastrowid if mapping_value is not None: cursor.execute( 'INSERT OR IGNORE INTO history_events_mappings(parent_identifier, value) ' 'VALUES(?, ?)', (identifier, mapping_value), ) self.db.update_last_write() return identifier