require 'rubygems'
require 'active_record'
require 'active_support'
require 'sqlite3'
require 'active_record/schema_dumper'
require "yaml"
require "create_class.rb"
t1 = Time.now
$config = YAML.load_file("config/config.yml")
name_of_schema_file = ($config["schema_file"])
class SourceDB < ActiveRecord::Base
end
SourceDB.establish_connection($config["database_mysql"])
class TargetDB < ActiveRecord::Base
end
TargetDB.establish_connection($config["database_sqlite"])
puts "dumping the schema"
File.open(name_of_schema_file,"w") do |file|
ActiveRecord::SchemaDumper.dump(SourceDB.connection, file)
end
puts "discarding index(es)"
line_array = Array.new
File.open(name_of_schema_file,"r") do |file|
file.each { |line| line_array << line unless line.include?("add_index")}
end
File.rename(name_of_schema_file, "original_#{name_of_schema_file}")
File.open(name_of_schema_file,"w") do |file|
file.puts(line_array)
end
puts "loading the schema"
ActiveRecord::Base.connection = TargetDB.connection
load(name_of_schema_file)
SourceDB.connection.tables.each do |tbl|
puts "Table_initiated: #{tbl.inspect}"
SourceDB.set_table_name tbl
SourceDB.set_inheritance_column ""
create_class('TargetModel', TargetDB) do
set_table_name tbl
set_inheritance_column ""
end
puts "=========for table: #{tbl}=========="
total_record_in_table = SourceDB.count_by_sql("SELECT COUNT(*) from #{tbl}")
tub_size = 1000
no_of_iteration = ((total_record_in_table % tub_size) == 0) ? (total_record_in_table / tub_size) : ((total_record_in_table / tub_size) + 1)
for j in 0..(no_of_iteration-1)
current_record_set = SourceDB.find(:all,:offset => (j*tub_size), :limit => (tub_size - 1))
current_record_set.each_with_index do |record,ind|
record_copy = TargetModel.new
record.attributes.each do |key,value|
record_copy.send("#{key}=",value)
end
record_copy.save
puts "Completed: #{((j*tub_size) + ind)} of #{total_record_in_table} with id: #{record_copy.id} ."
end
end
end
t2 = Time.now
puts "Process initiated at: #{t1}"
puts "Process completed at: #{t2}"
puts "Time elapsed : #{t2-t1} seconds"