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_table, destination_table) ⇒ Copier

Returns a new instance of Copier.



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

def initialize(source_table, destination_table)
  @source_table      = source_table
  @destination_table = destination_table
end

Instance Attribute Details

#destination_tableObject (readonly)

Returns the value of attribute destination_table.



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

def destination_table
  @destination_table
end

#source_tableObject (readonly)

Returns the value of attribute source_table.



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

def source_table
  @source_table
end

Instance Method Details

#diffyObject



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

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

#droppyObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/table_copy/copier.rb', line 29

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

#find_deletesObject



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

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

#updateObject



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

def update
  if destination_table.none?
    droppy
  elsif (max_sequence = destination_table.max_sequence)
    update_data(max_sequence)
  else
    diffy_update
  end
rescue ::PG::UndefinedTable => e
  ([e.inspect] + e.backtrace).each { |l| logger.warn(l) }
  create_table
  retry
rescue ::PG::UndefinedColumn => e
  ([e.inspect] + e.backtrace).each { |l| logger.warn(l) }
  droppy
end