Class: NBADW::Util::CopyDatabaseTask

Inherits:
Object
  • Object
show all
Defined in:
lib/nbadw/util/copy_database_task.rb

Constant Summary collapse

STRING_TO_INT_FIXES =
[
  { :table => "auxUserDBSelectedSites", :column => "AquaticSiteUseID" },
  { :table => "auxUserDBSelectedSiteUse", :column => "AquaticSiteUseID" },
  { :table => "cdTranslation - DFO Stock Mating", :column => "Mating Code" },
  { :table => "DEL-Missing Age Class in tblFishMeasurement", :column => "FishSampleID" },
  { :table => "DEL-Missing Age Class in tblFishMeasurement-robin", :column => "FishSampleID" },
  { :table => "Selections", :column => "SelectionID" },
  { :table => "tblElectrofishingMethodDetail", :column => "AquaticActivityDetailID" },
  { :table => "tblOldHabitatSurvey", :column => "HabitatSurveyID" }
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, dest, options = {}) ⇒ CopyDatabaseTask



14
15
16
17
18
19
20
# File 'lib/nbadw/util/copy_database_task.rb', line 14

def initialize(src, dest, options = {})
  @source = Sequel.connect(src, :single_threaded => true)
  @destination = Sequel.connect(dest, :single_threaded => true)
  @page_size = options[:page_size] || :unlimited
  @verify_data = !!options[:verify_data]
  @except = options[:except] || []
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



12
13
14
# File 'lib/nbadw/util/copy_database_task.rb', line 12

def destination
  @destination
end

#exceptObject (readonly)

Returns the value of attribute except.



12
13
14
# File 'lib/nbadw/util/copy_database_task.rb', line 12

def except
  @except
end

#page_sizeObject (readonly)

Returns the value of attribute page_size.



12
13
14
# File 'lib/nbadw/util/copy_database_task.rb', line 12

def page_size
  @page_size
end

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/nbadw/util/copy_database_task.rb', line 12

def source
  @source
end

Class Method Details

.add_callback(type, callback, opts, &block) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/nbadw/util/copy_database_task.rb', line 211

def add_callback(type, callback, opts, &block)
  callback_config = {
    :type     => type,
    :callback => callback,
    :adapter  => opts[:adapter] || :all,
    :for      => opts[:for],
    :logic    => block
  }
  callbacks << callback_config
end

.after(callback, opts = {}, &block) ⇒ Object



207
208
209
# File 'lib/nbadw/util/copy_database_task.rb', line 207

def after(callback, opts = {}, &block)
  add_callback(:after, callback, opts, &block)
end

.before(callback, opts = {}, &block) ⇒ Object



203
204
205
# File 'lib/nbadw/util/copy_database_task.rb', line 203

def before(callback, opts = {}, &block)
  add_callback(:before, callback, opts, &block)
end

.callbacksObject



199
200
201
# File 'lib/nbadw/util/copy_database_task.rb', line 199

def callbacks
  @callbacks ||= []
end

.start(src, dest, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nbadw/util/copy_database_task.rb', line 22

def self.start(src, dest, options = {})
  print "Initializing copy operation"
  task = new(src, dest, options)
  begin
    task.copy
  rescue Exception => e
    puts "...fail!!!"
    puts "Reason: #{e.message}"
    raise e
  end
end

Instance Method Details

#copyObject



34
35
36
37
38
39
40
41
42
# File 'lib/nbadw/util/copy_database_task.rb', line 34

def copy
  puts "..."
  puts "#{source.tables.length} tables, #{format_number(total_records(source))} records"
  copy_schema
  copy_data
  copy_indexes
  verify_data if verify_data?
  puts "...copy completed"
end

#copy_dataObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nbadw/util/copy_database_task.rb', line 67

def copy_data
  run_callback :before_copy_data

  progress = ProgressBar.new("Data copy", source.tables.size)
  begin
    source.tables.each do |table_name|
      next if except.include?(table_name.to_s)
      src_table = source[table_name.to_sym]
      dst_table = destination[table_name.to_sym]
      args = { :table => table_name }
      page_size == :unlimited ? copy_table_without_limit(src_table, dst_table, args) : copy_table_with_limit(src_table, dst_table, args)
      progress.inc(1)
    end
  ensure
    progress.finish
  end

  run_callback :after_copy_data
end

#copy_indexesObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/nbadw/util/copy_database_task.rb', line 111

def copy_indexes
  begin
    run_callback :before_copy_indexes

    tables = source.tables
    progress = ProgressBar.new("Index copy", tables.length)

    tables.each do |t|
      next if except.include?(t.to_s)
      args = { :table => t, :indexes => source.send(:dump_table_indexes, t.to_sym, :add_index) }
      run_callback :before_add_indexes, args
      migration = "Class.new(Sequel::Migration) do \n def up \n #{args[:indexes]} \n end \n end"
      eval(migration).apply(destination, :up)
      run_callback :after_add_indexes, args
      progress.inc(1)
    end

    run_callback :after_copy_indexes
  ensure
    progress.finish if progress
  end
end

#copy_schemaObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nbadw/util/copy_database_task.rb', line 44

def copy_schema
  begin
    run_callback :before_copy_schema
  
    tables = source.tables
    progress = ProgressBar.new("Schema copy", tables.length)

    tables.each do |t|
      next if except.include?(t.to_s)
      args = { :table => t, :schema => source.dump_table_schema(t.to_sym, :indexes => false) }
      run_callback :before_create_table, args
      migration = "Class.new(Sequel::Migration) do \n def up \n #{args[:schema]} \n end \n end"
      eval(migration).apply(destination, :up)
      run_callback :after_create_table, args
      progress.inc(1)
    end

    run_callback :after_copy_schema
  ensure
    progress.finish if progress
  end
end

#copy_table_with_limit(src_table, dst_table, args = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nbadw/util/copy_database_task.rb', line 96

def copy_table_with_limit(src_table, dst_table, args = {})
  count = src_table.count
  offset = 0
  while(offset < count) do
    rows = src_table.limit(page_size, offset).all
    rows.each_with_index do |row, i|
      args.merge!({ :row => row, :index => i, :offset => offset })
      run_callback :before_copy_row, args
      dst_table.insert(row)
      run_callback :after_copy_row, args
    end
    offset += rows.size
  end
end

#copy_table_without_limit(src_table, dst_table, args = {}) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/nbadw/util/copy_database_task.rb', line 87

def copy_table_without_limit(src_table, dst_table, args = {})
  src_table.each do |row|
    args.merge!({ :row => row })
    run_callback :before_copy_row, args
    dst_table.insert(row)
    run_callback :after_copy_row, args
  end
end

#format_number(num) ⇒ Object



192
193
194
# File 'lib/nbadw/util/copy_database_task.rb', line 192

def format_number(num)
  num.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end

#run_callback(full_callback, args = {}) ⇒ Object

determines which callbacks to run (is this needlessly complex?)



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/nbadw/util/copy_database_task.rb', line 288

def run_callback(full_callback, args = {})
  full_callback.to_s.match(/(before|after)_(.*)/)
  type, callback = $1.to_sym, $2.to_sym
  CopyDatabaseTask.callbacks.each do |callback_config|
    if callback_config[:type] == type && callback_config[:callback] == callback # callback matches
      # which adapters should we check against?
      adapters = [:all] # always check for all...
      if callback_config[:for] == :destination # only destination?
        adapters << destination.database_type.to_sym
      elsif callback_config[:for] == :source   # only source?
        adapters << source.database_type.to_sym
      else                                     # or both?
        adapters << destination.database_type.to_sym
        adapters << source.database_type.to_sym
      end
      # if the adapter matches, run the callback
      if adapters.include?(callback_config[:adapter])
        callback_config[:logic].call(source, destination, args)
      end
    end
  end
end

#total_records(db) ⇒ Object



188
189
190
# File 'lib/nbadw/util/copy_database_task.rb', line 188

def total_records(db)
  db.tables.inject(0) { |total, table_name| total += db[table_name.to_sym].count }
end

#verify_dataObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nbadw/util/copy_database_task.rb', line 134

def verify_data
  tables = source.tables
  progress = ProgressBar.new("Verify data", tables.length)
  begin
    tables.each do |table_name|
      next if except.include?(table_name.to_s)
      src_table = source[table_name.to_sym]
      dst_table = destination[table_name.to_sym]
      page_size == :unlimited ? verify_table_without_limit(table_name, src_table, dst_table) : verify_table_with_limit(table_name, src_table, dst_table)
      progress.inc(1)
    end
  ensure
    progress.finish if progress
  end
end

#verify_data?Boolean



184
185
186
# File 'lib/nbadw/util/copy_database_task.rb', line 184

def verify_data?
  @verify_data
end

#verify_row(table_name, row1, row2) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/nbadw/util/copy_database_task.rb', line 172

def verify_row(table_name, row1, row2)
  diff = {}
  row1.each do |col, val|
    eql = case val
    when Time then (val - row1[col]).abs < 1  # time fields are sometimes off by very miniscule fractions
    else           val == row1[col]
    end
    diff[col] = "#{val}, #{row2[col]}" unless eql
  end
  raise "row does not match exactly - expected #{row1.inspect}, but was #{row2.inspect} - in table #{table_name}, diff #{diff.inspect}" unless diff.empty?
end

#verify_table_with_limit(table_name, src_table, dst_table) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/nbadw/util/copy_database_task.rb', line 158

def verify_table_with_limit(table_name, src_table, dst_table)
  count = src_table.count
  offset = 0
  while(offset < count) do
    rows = src_table.limit(page_size, offset).all
    rows.each do |row|
      row_found = dst_table.filter(row).first
      raise "no matching row found in #{table_name} for #{row.inspect}" unless row_found
      verify_row(table_name, row, row_found)
    end
    offset += rows.length
  end
end

#verify_table_without_limit(table_name, src_table, dst_table) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/nbadw/util/copy_database_task.rb', line 150

def verify_table_without_limit(table_name, src_table, dst_table)
  src_table.each do |row|
    row_found = dst_table.filter(row).first
    raise "no matching row found in #{table_name} for #{row.inspect}" unless row_found
    verify_row(table_name, row, row_found)
  end
end