Class: TableCopy::Copier

Inherits:
Object
  • Object
show all
Defined in:
lib/table_copy/copier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination) ⇒ Copier

Returns a new instance of Copier.



7
8
9
10
# File 'lib/table_copy/copier.rb', line 7

def initialize(source, destination)
  @source      = source
  @destination = destination
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



5
6
7
# File 'lib/table_copy/copier.rb', line 5

def destination
  @destination
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/table_copy/copier.rb', line 5

def source
  @source
end

Instance Method Details

#diffyObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/table_copy/copier.rb', line 53

def diffy
  logger.info { "Diffy #{destination.table_name}" }
  destination.transaction do
    destination.create_temp(source.fields_ddl)
    moved_count = destination.copy_data_from(source, temp: true)
    logger.info { "#{moved_count} rows moved to temp_#{destination.table_name}" }
    destination.copy_from_temp
    logger.info { "Upsert to #{destination.table_name} complete" }
    destination.delete_not_in_temp
    logger.info { "Deletetions from #{destination.table_name} complete." }
  end
end

#droppyObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/table_copy/copier.rb', line 24

def droppy
  logger.info { "Droppy #{destination.table_name}" }
  views = destination.query_views

  destination.transaction do
    destination.drop(cascade: true)
    create_table
    moved_count = destination.copy_data_from(source)
    logger.info { "#{moved_count} rows moved to #{destination.table_name}" }
    destination.create_indexes
    logger.info { "Completed #{source.indexes.count} indexes on #{destination.table_name}." }
  end

  destination.create_views(views).each do |view_name, view_status|
    logger.info { "#{view_status ? 'Created' : 'Failed to create'} view #{view_name} for #{destination.table_name}" }
  end
end

#find_deletesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/table_copy/copier.rb', line 42

def find_deletes
  logger.info { "Find deletes #{destination.table_name}" }
  destination.transaction do
    destination.create_temp(source.fields_ddl)
    moved_count = destination.copy_data_from(source, temp: true, pk_only: true)
    logger.info { "#{moved_count} rows moved to temp_#{destination.table_name}" }
    destination.delete_not_in_temp
    logger.info { "Deletetions from #{destination.table_name} complete." }
  end
end

#updateObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/table_copy/copier.rb', line 12

def update
  with_rescue do
    if destination.none?
      droppy
    elsif (max_sequence = destination.max_sequence)
      update_data(max_sequence)
    else
      diffy_update
    end
  end
end