Class: RailsRedshiftReplicator::Deleter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_redshift_replicator/deleter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#replicableObject (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_targettrue, false

Deletes records flagged for deletion on redshift

Returns:

  • (true, false)

    if deletion succeded



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_statementObject



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_idsString

Retrives ids of objects enqueued for deletion for the replication source table deleted_ids #=> “1,2,3,4,5”

Returns:

  • (String)

    list of ids enqueued to delete on target



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_statementString

Builds the statement to perform a deletion on source

Returns:

  • (String)

    SQL statement



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_propagationObject

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 propagation_message(:propagating_deletes)
    delete_on_target ? purge_deleted : RailsRedshiftReplicator.logger.error(propagation_message(:delete_propagation_error))
  end
end

#has_deleted_ids?true, false

If has objects to delete on target

Returns:

  • (true, false)


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 propagation_message(key)
  I18n.t(key, table_name: replicable.source_table, count: deleted_ids.count, scope: :rails_redshift_replicator)
end

#purge_deletedObject

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