Class: DbCopier::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/db-copier/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Worker

Returns a new instance of Worker.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
# File 'lib/db-copier/worker.rb', line 3

def initialize(options = {})
  @src_db_conn, @target_db_conn, @table_name, @rows_per_copy, @copy_columns = options[:src_db_conn], options[:target_db_conn],
          options[:table_name], (options[:rows_per_copy] || 1000), (options[:copy_columns] || [])
  raise ArgumentError unless @src_db_conn && @target_db_conn && @table_name
  @copy_columns ||= []
end

Instance Method Details

#copy_tableObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/db-copier/worker.rb', line 10

def copy_table
  start = Time.now
  $stdout.print green, bold, "Thread: #{Thread.current.object_id} is copying table: #{@table_name}", reset, "\n"
  tab_to_copy = @table_name.to_sym
  num_rows = @src_db_conn[tab_to_copy].count
  i = 0
  #Create the table if it does not already exist
  unless @target_db_conn.table_exists?(tab_to_copy)
    table_creation_ddl = DbCopier.generate_create_table_ddl(@src_db_conn, tab_to_copy, tab_to_copy, @copy_columns)
    table_creation_ddl = ("@target_db_conn" + table_creation_ddl)
    eval table_creation_ddl
  end

  #This is the intersection of columns specified via the +copy_columns+ argumnent in the +for_table+ method
  #and those that actually exist in the target table.
  columns_in_target_db = @target_db_conn.schema(tab_to_copy).map {|cols| cols.first}
  columns_to_copy =
          if @copy_columns.count > 0
            @copy_columns & columns_in_target_db
          else
            columns_in_target_db
          end

  while i < num_rows
    rows_to_copy = @src_db_conn[tab_to_copy].select(*columns_to_copy).limit(@rows_per_copy, i).all
    #Special handling of datetime columns
    rows_to_copy.each { |col_name, col_val| rows_to_copy[col_name] = DateTime.parse(col_val) if col_val.class == Time }
    i += rows_to_copy.count
    @target_db_conn[tab_to_copy].multi_insert(rows_to_copy)
  end

  #copy indexes now
  @src_db_conn.indexes(tab_to_copy).each do |index_name, index_info|
    #Make sure we are adding an index to a column that is going to be there
    next unless (columns_to_copy & index_info[:columns] == index_info[:columns])
    @target_db_conn.add_index(tab_to_copy, index_info[:columns])
  end
  $stdout.print green,bold, "Thread: #{Thread.current.object_id} has COMPLETED copying table: #{@table_name} in #{Time.now - start} seconds", reset, "\n"
end