Class: DbCopier::Application

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Application

Returns a new instance of Application.



7
8
9
10
11
12
13
# File 'lib/db-copier/db-copier.rb', line 7

def initialize &block
  @tables_to_copy        = []
  @index_to_copy         = []
  @only_tables_to_copy   = []
  @except_tables_to_copy = []
  instance_eval &block
end

Instance Attribute Details

#tables_to_copyObject (readonly)

Returns the value of attribute tables_to_copy.



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

def tables_to_copy
  @tables_to_copy
end

Instance Method Details

#copy(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/db-copier/db-copier.rb', line 15

def copy options = {}
  begin
    from, to, @rows_per_copy, max_connections = options[:from], options[:to], (options[:rows_per_copy] || 50), (options[:max_connections] || 5)
    raise ArgumentError unless from && to && from.is_a?(Hash) && to.is_a?(Hash) && from.size > 0 && to.size > 0
    @source_db, @target_db = Sequel.connect(from.merge(:max_connections => max_connections, :single_threaded => false)),
            Sequel.connect(to.merge(:max_connections => max_connections, :single_threaded => false))
    @source_db.test_connection && @target_db.test_connection #test connections
    @tables_to_copy = @source_db.tables
    @target_db.tables
    instance_eval { yield } if block_given?
    copy_tables
  ensure
    self.close_connections
  end
end

#except(*tabs) ⇒ Object



53
54
55
# File 'lib/db-copier/db-copier.rb', line 53

def except(*tabs)
  @tables_to_copy -= Array(tabs).map { |tb| tb.to_sym }
end

#for_table(table, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/db-copier/db-copier.rb', line 38

def for_table(table, options = {})
  raise ArgumentError, "missing required copy_cols attribute" unless (copy_columns = options[:copy_columns])
  table, copy_columns = table.to_sym, copy_columns.map {|col| col.to_sym}
  raise ArgumentError, "columns do not exist" unless (@source_db.schema(table).map {|cols| cols.first} & copy_columns) == copy_columns
  self.copy_columns[table] = copy_columns
end

#index(ind) ⇒ Object



45
46
47
# File 'lib/db-copier/db-copier.rb', line 45

def index(ind)
  @index_to_copy << ind
end

#only(*tabs) ⇒ Object



49
50
51
# File 'lib/db-copier/db-copier.rb', line 49

def only(*tabs)
  @tables_to_copy = Array(tabs)
end