Class: RailsRedshiftReplicator::Deleter
- Inherits:
-
Object
- Object
- RailsRedshiftReplicator::Deleter
- Defined in:
- lib/rails_redshift_replicator/deleter.rb
Instance Attribute Summary collapse
-
#replicable ⇒ Object
readonly
Returns the value of attribute replicable.
Instance Method Summary collapse
-
#delete_on_target ⇒ true, false
Deletes records flagged for deletion on redshift.
- #delete_on_target_statement ⇒ Object
-
#deleted_ids ⇒ String
Retrives ids of objects enqueued for deletion for the replication source table deleted_ids #=> “1,2,3,4,5”.
-
#discard_deleted_statement ⇒ String
Builds the statement to perform a deletion on source.
-
#handle_delete_propagation ⇒ Object
Deletes records flagged for deletion on target and then delete the queue from source.
-
#has_deleted_ids? ⇒ true, false
If has objects to delete on target.
-
#initialize(replicable) ⇒ Deleter
constructor
A new instance of Deleter.
- #propagation_message(key) ⇒ Object
-
#purge_deleted ⇒ Object
Deletes ids on source database.
Constructor Details
#initialize(replicable) ⇒ Deleter
Returns a new instance of Deleter.
4 5 6 |
# File 'lib/rails_redshift_replicator/deleter.rb', line 4 def initialize(replicable) @replicable = replicable end |
Instance Attribute Details
#replicable ⇒ Object (readonly)
Returns the value of attribute replicable.
3 4 5 |
# File 'lib/rails_redshift_replicator/deleter.rb', line 3 def replicable @replicable end |
Instance Method Details
#delete_on_target ⇒ true, false
Deletes records flagged for deletion on redshift
15 16 17 |
# 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 ⇒ Object
60 61 62 63 64 65 |
# 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 ⇒ String
Retrives ids of objects enqueued for deletion for the replication source table deleted_ids #=> “1,2,3,4,5”
35 36 37 38 39 40 41 42 |
# 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 ⇒ String
Builds the statement to perform a deletion on source
52 53 54 55 56 57 58 |
# 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 ⇒ Object
Deletes records flagged for deletion on target and then delete the queue from source
20 21 22 23 24 25 |
# File 'lib/rails_redshift_replicator/deleter.rb', line 20 def handle_delete_propagation if replicable.tracking_deleted && has_deleted_ids? RailsRedshiftReplicator.logger.info (:propagating_deletes) delete_on_target ? purge_deleted : RailsRedshiftReplicator.logger.error((:delete_propagation_error)) end end |
#has_deleted_ids? ⇒ true, false
If has objects to delete on target
46 47 48 |
# File 'lib/rails_redshift_replicator/deleter.rb', line 46 def has_deleted_ids? deleted_ids.present? end |
#propagation_message(key) ⇒ Object
27 28 29 |
# File 'lib/rails_redshift_replicator/deleter.rb', line 27 def (key) I18n.t(key, table_name: replicable.source_table, count: deleted_ids.count, scope: :rails_redshift_replicator) end |
#purge_deleted ⇒ Object
Deletes ids on source database. This ensures that only the records deleted on target will be discarded on source
9 10 11 |
# File 'lib/rails_redshift_replicator/deleter.rb', line 9 def purge_deleted ActiveRecord::Base.connection.execute discard_deleted_statement end |