/// Revert the block with the given `block_ptr` which must be the hash
/// of the block to revert. The returned `StoreEvent` reflects the changes
/// that were made during reversion
Revert the block with the given `block_ptr` which must be the hash
of the block to revert. The returned `StoreEvent` reflects the changes
that were made during reversion
Revert the block with the given ` block_ptr ` which must be the hash of the block to revert . The returned ` StoreEvent ` reflects the changes that were made during reversion
fn revert_block(
        &self,
        conn: &PgConnection,
        block_ptr: String,
    ) -> Result<StoreEvent, StoreError> {
        let entries: Vec<RawHistory> = match self {
            Table::Public(subgraph) => {
                use self::public::entity_history::dsl as h;
                use self::public::event_meta_data as m;
                h::entity_history
                    .inner_join(m::table)
                    .select((h::id, h::entity, h::entity_id, h::data_before, h::op_id))
                    .filter(h::subgraph.eq(subgraph.to_string()))
                    .filter(m::source.eq(&block_ptr))
                    .order(h::event_id.desc())
                    .load(conn)?
            }
            Table::Split(entities) => {
                // We can't use Diesel's JoinOnDsl here because DynamicTable
                // does not implement AppearsInFromClause, so we have to run
                // a raw SQL query
                let query = format!(
                    "SELECT h.id, h.entity, h.entity_id, h.data_before, h.op_id
                    FROM {}.entity_history h, event_meta_data m
                    WHERE m.id = h.event_id
                      AND m.source = $1
                    ORDER BY h.event_id desc",
                    entities.schema
                );
                let query = diesel::sql_query(query).bind::<Text, _>(&block_ptr);
                query.get_results(conn)?
            }
        };
        let subgraph_id = match self {
            Table::Public(subgraph) => subgraph.clone(),
            Table::Split(entities) => entities.subgraph.clone(),
        };
        let mut changes = Vec::with_capacity(entries.len());
        for history in entries.into_iter() {
            // Perform the actual reversion
            let key = self.entity_key(history.entity.clone(), history.entity_id.clone());
            match history.op {
                0 => {
                    // Reverse an insert
                    self.delete(conn, &key, EventSource::None)?;
                }
                1 | 2 => {
                    // Reverse an update or delete
                    if let Some(data) = history.data {
                        self.upsert(conn, &key, &data, EventSource::None)?;
                    } else {
                        return Err(StoreError::Unknown(format_err!(
                            "History entry for update/delete has NULL data_before. id={}, op={}",
                            history.id,
                            history.op
                        )));
                    }
                }
                _ => {
                    return Err(StoreError::Unknown(format_err!(
                        "bad operation {}",
                        history.op
                    )))
                }
            }
            // Record the change that was just made
            let change = EntityChange {
                subgraph_id: subgraph_id.clone(),
                entity_type: history.entity,
                entity_id: history.entity_id,
                operation: match history.op {
                    0 => EntityChangeOperation::Removed,
                    1 | 2 => EntityChangeOperation::Set,
                    _ => {
                        return Err(StoreError::Unknown(format_err!(
                            "bad operation {}",
                            history.op
                        )))
                    }
                },
            };
            changes.push(change);
        }
        Ok(StoreEvent::new(changes))
    }
fn revert_block ( & self , conn : & PgConnection , block_ptr : String , ) -> Result < StoreEvent , StoreError > { let entries : Vec < RawHistory > = match self { Table :: Public ( subgraph ) => { use self :: public :: entity_history :: dsl as h ; use self :: public :: event_meta_data as m ; h :: entity_history . inner_join ( m :: table ) . select ( ( h :: id , h :: entity , h :: entity_id , h :: data_before , h :: op_id ) ) . filter ( h :: subgraph . eq ( subgraph . to_string ( ) ) ) . filter ( m :: source . eq ( & block_ptr ) ) . order ( h :: event_id . desc ( ) ) . load ( conn ) ? } Table :: Split ( entities ) => { // We can't use Diesel's JoinOnDsl here because DynamicTable // does not implement AppearsInFromClause, so we have to run // a raw SQL query let query = format ! ( " " entities schema ) ; let query = diesel :: sql_query ( query ) . bind :: < Text , _ > ( & block_ptr ) ; query . get_results ( conn ) ? } } ; let subgraph_id = match self { Table :: Public ( subgraph ) => subgraph . clone ( ) , Table :: Split ( entities ) => entities . subgraph . clone ( ) , } ; let mut changes = Vec :: with_capacity ( entries . len ( ) ) ; for history in entries . into_iter ( ) { // Perform the actual reversion let key = self . entity_key ( history . entity . clone ( ) , history . entity_id . clone ( ) ) ; match history . op { 0 => { // Reverse an insert self . delete ( conn , & key , EventSource :: None ) ? ; } 1 | 2 => { // Reverse an update or delete if let Some ( data ) = history . data { self . upsert ( conn , & key , & data , EventSource :: None ) ? ; } else { return Err ( StoreError :: Unknown ( format_err ! ( " " history id history op ) ) ) ; } } _ => { return Err ( StoreError :: Unknown ( format_err ! ( " " history op ) ) ) } } // Record the change that was just made let change = EntityChange { subgraph_id : subgraph_id . clone ( ) , entity_type : history . entity , entity_id : history . entity_id , operation : match history . op { 0 => EntityChangeOperation :: Removed , 1 | 2 => EntityChangeOperation :: Set , _ => { return Err ( StoreError :: Unknown ( format_err ! ( " " history op ) ) ) } } , } ; changes . push ( change ) ; } Ok ( StoreEvent :: new ( changes ) ) }
Revert the block with the given `block_ptr` which must be the hash
of the block to revert.
Revert the block with the given ` block_ptr ` which must be the hash of the block to revert .
&self
conn
&PgConnection
block_ptr
String