class RailsRedshiftReplicator::Deleter

Attributes

replicable[R]

Public Class Methods

new(replicable) click to toggle source
# File lib/rails_redshift_replicator/deleter.rb, line 4
def initialize(replicable)
  @replicable = replicable
end

Public Instance Methods

delete_on_target() click to toggle source

Deletes records flagged for deletion on redshift @return [true, false] if deletion succeded

# File lib/rails_redshift_replicator/deleter.rb, line 15
def delete_on_target
  RailsRedshiftReplicator.connection.exec(delete_on_target_statement).result_status == 1
end
delete_on_target_statement() click to toggle source
# File lib/rails_redshift_replicator/deleter.rb, line 60
    def delete_on_target_statement
      sql = <<-DD.squish
        DELETE FROM #{replicable.target_table}
        WHERE id IN(#{deleted_ids.join(",")})
      DD
    end
deleted_ids() click to toggle source

Retrives ids of objects enqueued for deletion for the replication source table @example deleted_ids #=> “1,2,3,4,5” @return [String] list of ids enqueued to delete on target

# File lib/rails_redshift_replicator/deleter.rb, line 35
    def deleted_ids
      sql = <<-DR.squish
        SELECT object_id
        FROM rails_redshift_replicator_deleted_ids
        WHERE source_table = '#{replicable.source_table}'
      DR
      ActiveRecord::Base.connection.execute(sql).map{ |r| r['object_id'] }
    end
discard_deleted_statement() click to toggle source

Builds the statement to perform a deletion on source @return [String] SQL statement

# File lib/rails_redshift_replicator/deleter.rb, line 52
    def discard_deleted_statement
      sql = <<-DD.squish
        DELETE FROM rails_redshift_replicator_deleted_ids
        WHERE source_table = '#{replicable.source_table}'
        AND object_id IN(#{deleted_ids.join(",")})
      DD
    end
handle_delete_propagation() click to toggle source

Deletes records flagged for deletion on target and then delete the queue from source

# File lib/rails_redshift_replicator/deleter.rb, line 20
def handle_delete_propagation
  if replicable.tracking_deleted && has_deleted_ids?
    RailsRedshiftReplicator.logger.info propagation_message(:propagating_deletes)
    delete_on_target ? purge_deleted : RailsRedshiftReplicator.logger.error(propagation_message(:delete_propagation_error))
  end
end
has_deleted_ids?() click to toggle source

If has objects to delete on target @return [true, false]

# File lib/rails_redshift_replicator/deleter.rb, line 46
def has_deleted_ids?
  deleted_ids.present?
end
propagation_message(key) click to toggle source
# File lib/rails_redshift_replicator/deleter.rb, line 27
def propagation_message(key)
  I18n.t(key, table_name: replicable.source_table, count: deleted_ids.count, scope: :rails_redshift_replicator)
end
purge_deleted() click to toggle source

Deletes ids on source database. This ensures that only the records deleted on target will be discarded on source

# File lib/rails_redshift_replicator/deleter.rb, line 9
def purge_deleted
  ActiveRecord::Base.connection.execute discard_deleted_statement
end