Class: ARDBCopy
- Inherits:
-
Object
show all
- Defined in:
- lib/ar_dbcopy.rb
Defined Under Namespace
Classes: Application, SourceDB, TargetDB
Instance Method Summary
collapse
Constructor Details
#initialize(config_file, opts = {}) ⇒ ARDBCopy
Returns a new instance of ARDBCopy.
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/ar_dbcopy.rb', line 25
def initialize(config_file, opts={})
@copy_schema = opts[:copy_schema]
config = YAML.load_file(config_file)
ActiveRecord::Base.logger ||= Logger.new(nil)
SourceDB.establish_connection(config["source"])
ActiveRecord::Base.establish_connection(config["target"])
@tables = SourceDB.connection.tables.reject { |m| m == "schema_migrations" }
end
|
Instance Method Details
#copy_data ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/ar_dbcopy.rb', line 52
def copy_data
@tables.each do |table_name|
source_model = Class.new(SourceDB) do
set_inheritance_column(:not_sti)
set_table_name table_name
end
dest_model = Class.new(TargetDB) do
set_inheritance_column(:not_sti)
set_table_name table_name
end
dest_model.delete_all
puts "Copying #{table_name} (#{source_model.count} lines)..."
i = 0
source_model.find_in_batches(:batch_size => 10_000) do |src_batch|
dest_model.transaction do
src_batch.each do |src_inst|
dst_inst = dest_model.new(src_inst.attributes)
dst_inst.id = src_inst.id
dst_inst.save!
i += 1
end
puts i
end
end
end
end
|
#copy_schema ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/ar_dbcopy.rb', line 43
def copy_schema
io = StringIO.new
ActiveRecord::SchemaDumper.dump(SourceDB.connection, io) io.rewind
eval(io.read)
end
|
#run! ⇒ Object
38
39
40
41
|
# File 'lib/ar_dbcopy.rb', line 38
def run!
copy_schema if @copy_schema
copy_data
end
|