Class: Sq::Dbsync::RefreshRecentLoadAction

Inherits:
LoadAction
  • Object
show all
Defined in:
lib/sq/dbsync/refresh_recent_load_action.rb

Overview

This is a terribly named class that will delete the last X days of data from a table and reload it. Useful for tables that are nearly append only but sometimes will update recent data (for instance, a failed import). The tables are too big to regularly reload in their entirety, but reloading only recent data fixes the main issues.

Constant Summary collapse

WINDOW =

2 days

60 * 60 * 24 * 2

Constants inherited from LoadAction

LoadAction::EPOCH

Instance Method Summary collapse

Methods inherited from LoadAction

#call, #do_prepare, #initialize, stages, #tag

Constructor Details

This class inherits a constructor from Sq::Dbsync::LoadAction

Instance Method Details

#extract_dataObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/sq/dbsync/refresh_recent_load_action.rb', line 24

def extract_data
  @metadata   = registry.get(plan.table_name)
  @start_time = now.call
  @since      = (
    @metadata[:last_row_at] ||
    @metadata[:last_synced_at]
  ) - WINDOW
  @file, @last_row_at = measure(:extract) { extract_to_file(@since) }
  self
end

#load_dataObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sq/dbsync/refresh_recent_load_action.rb', line 35

def load_data
  measure(:load) do
    tname   = plan.table_name
    columns = plan.columns
    db.transaction do
      db.delete_recent(plan, @since)
      db.load_from_file(tname, columns, @file.path)
    end
  end
  @file.close!
  self
end

#operationObject



13
# File 'lib/sq/dbsync/refresh_recent_load_action.rb', line 13

def operation; 'refresh_recent'; end

#post_loadObject



21
22
# File 'lib/sq/dbsync/refresh_recent_load_action.rb', line 21

def post_load
end

#prepareObject



15
16
17
18
19
# File 'lib/sq/dbsync/refresh_recent_load_action.rb', line 15

def prepare
  return false unless plan.refresh_recent

  super
end