Refactor ImportToMysql; add import_raw. Add rake task for import.
Remove unique constraint on index on mongo_requests.
This commit is contained in:
parent
ff1cad6e63
commit
75ac7e91c8
4 changed files with 75 additions and 11 deletions
|
|
@ -57,7 +57,7 @@ class CreateImportTables < ActiveRecord::Migration
|
||||||
end
|
end
|
||||||
add_index :mongo_requests, :sender_mongo_id
|
add_index :mongo_requests, :sender_mongo_id
|
||||||
add_index :mongo_requests, :recipient_mongo_id
|
add_index :mongo_requests, :recipient_mongo_id
|
||||||
add_index :mongo_requests, [:sender_mongo_id, :recipient_mongo_id], :unique => true
|
add_index :mongo_requests, [:sender_mongo_id, :recipient_mongo_id]
|
||||||
|
|
||||||
create_table :mongo_users do |t|
|
create_table :mongo_users do |t|
|
||||||
t.string :mongo_id
|
t.string :mongo_id
|
||||||
|
|
@ -79,5 +79,11 @@ class CreateImportTables < ActiveRecord::Migration
|
||||||
|
|
||||||
def self.down
|
def self.down
|
||||||
drop_table :mongo_users
|
drop_table :mongo_users
|
||||||
|
drop_table :mongo_requests
|
||||||
|
drop_table :mongo_post_visibilities
|
||||||
|
drop_table :mongo_contacts
|
||||||
|
drop_table :mongo_comments
|
||||||
|
drop_table :mongo_aspect_memberships
|
||||||
|
drop_table :mongo_aspects
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -138,7 +138,7 @@ ActiveRecord::Schema.define(:version => 20110105051803) do
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "mongo_requests", ["recipient_mongo_id"], :name => "index_mongo_requests_on_recipient_mongo_id"
|
add_index "mongo_requests", ["recipient_mongo_id"], :name => "index_mongo_requests_on_recipient_mongo_id"
|
||||||
add_index "mongo_requests", ["sender_mongo_id", "recipient_mongo_id"], :name => "index_mongo_requests_on_sender_mongo_id_and_recipient_mongo_id", :unique => true
|
add_index "mongo_requests", ["sender_mongo_id", "recipient_mongo_id"], :name => "index_mongo_requests_on_sender_mongo_id_and_recipient_mongo_id"
|
||||||
add_index "mongo_requests", ["sender_mongo_id"], :name => "index_mongo_requests_on_sender_mongo_id"
|
add_index "mongo_requests", ["sender_mongo_id"], :name => "index_mongo_requests_on_sender_mongo_id"
|
||||||
|
|
||||||
create_table "mongo_users", :force => true do |t|
|
create_table "mongo_users", :force => true do |t|
|
||||||
|
|
|
||||||
|
|
@ -4,17 +4,34 @@
|
||||||
|
|
||||||
module DataConversion
|
module DataConversion
|
||||||
class ImportToMysql < DataConversion::Base
|
class ImportToMysql < DataConversion::Base
|
||||||
def infile_opts
|
|
||||||
<<-OPTS
|
def import_raw
|
||||||
FIELDS TERMINATED BY ','
|
truncate_tables
|
||||||
ENCLOSED BY '"'
|
import_raw_users
|
||||||
IGNORE 1 LINES
|
import_raw_aspects
|
||||||
OPTS
|
import_raw_aspect_memberships
|
||||||
|
import_raw_comments
|
||||||
|
import_raw_contacts
|
||||||
|
import_raw_post_visibilities
|
||||||
|
import_raw_requests
|
||||||
end
|
end
|
||||||
def load_string model_name
|
|
||||||
"LOAD DATA INFILE '#{full_path}/#{model_name}.csv' INTO TABLE mongo_#{model_name}"
|
def process_raw_tables
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def truncate_tables
|
||||||
|
Mongo::User.connection.execute "TRUNCATE TABLE mongo_users"
|
||||||
|
Mongo::Aspect.connection.execute "TRUNCATE TABLE mongo_aspects"
|
||||||
|
Mongo::AspectMembership.connection.execute "TRUNCATE TABLE mongo_aspect_memberships"
|
||||||
|
Mongo::Comment.connection.execute "TRUNCATE TABLE mongo_comments"
|
||||||
|
Mongo::Contact.connection.execute "TRUNCATE TABLE mongo_contacts"
|
||||||
|
Mongo::PostVisibility.connection.execute "TRUNCATE TABLE mongo_post_visibilities"
|
||||||
|
Mongo::Request.connection.execute "TRUNCATE TABLE mongo_requests"
|
||||||
|
end
|
||||||
|
|
||||||
def import_raw_users
|
def import_raw_users
|
||||||
|
log "Loading users file..."
|
||||||
Mongo::User.connection.execute <<-SQL
|
Mongo::User.connection.execute <<-SQL
|
||||||
#{load_string("users")}
|
#{load_string("users")}
|
||||||
#{infile_opts}
|
#{infile_opts}
|
||||||
|
|
@ -24,51 +41,80 @@ OPTS
|
||||||
reset_password_token, password_salt)
|
reset_password_token, password_salt)
|
||||||
SET last_sign_in_at = FROM_UNIXTIME(LEFT(@last_sign_in_at_var, LENGTH(@last_sign_in_at_var)-3));
|
SET last_sign_in_at = FROM_UNIXTIME(LEFT(@last_sign_in_at_var, LENGTH(@last_sign_in_at_var)-3));
|
||||||
SQL
|
SQL
|
||||||
|
log "Finished. Imported #{Mongo::User.count} users."
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_raw_aspects
|
def import_raw_aspects
|
||||||
|
log "Loading aspects file..."
|
||||||
Mongo::Aspect.connection.execute <<-SQL
|
Mongo::Aspect.connection.execute <<-SQL
|
||||||
#{load_string("aspects")}
|
#{load_string("aspects")}
|
||||||
#{infile_opts}
|
#{infile_opts}
|
||||||
(mongo_id, name, user_mongo_id, @created_at, @updated_at)
|
(mongo_id, name, user_mongo_id, @created_at, @updated_at)
|
||||||
SQL
|
SQL
|
||||||
|
log "Finished. Imported #{Mongo::Aspect.count} aspects."
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_raw_aspect_memberships
|
def import_raw_aspect_memberships
|
||||||
|
log "Loading aspect memberships file..."
|
||||||
Mongo::Aspect.connection.execute <<-SQL
|
Mongo::Aspect.connection.execute <<-SQL
|
||||||
#{load_string("aspect_memberships")}
|
#{load_string("aspect_memberships")}
|
||||||
#{infile_opts}
|
#{infile_opts}
|
||||||
(contact_mongo_id, aspect_mongo_id)
|
(contact_mongo_id, aspect_mongo_id)
|
||||||
SQL
|
SQL
|
||||||
|
log "Finished. Imported #{Mongo::AspectMembership.count} aspect memberships."
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_raw_comments
|
def import_raw_comments
|
||||||
|
log "Loading comments file..."
|
||||||
Mongo::Aspect.connection.execute <<-SQL
|
Mongo::Aspect.connection.execute <<-SQL
|
||||||
#{load_string("comments")}
|
#{load_string("comments")}
|
||||||
#{infile_opts}
|
#{infile_opts}
|
||||||
(mongo_id, post_mongo_id, person_mongo_id, @diaspora_handle, text, youtube_titles)
|
(mongo_id, post_mongo_id, person_mongo_id, @diaspora_handle, text, youtube_titles)
|
||||||
SET guid = mongo_id;
|
SET guid = mongo_id;
|
||||||
SQL
|
SQL
|
||||||
|
log "Finished. Imported #{Mongo::Comment.count} comments."
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_raw_contacts
|
def import_raw_contacts
|
||||||
|
log "Loading contacts file..."
|
||||||
Mongo::Aspect.connection.execute <<-SQL
|
Mongo::Aspect.connection.execute <<-SQL
|
||||||
#{load_string("contacts")}
|
#{load_string("contacts")}
|
||||||
#{infile_opts}
|
#{infile_opts}
|
||||||
(mongo_id, user_mongo_id, person_mongo_id, pending, created_at, updated_at)
|
(mongo_id, user_mongo_id, person_mongo_id, pending, created_at, updated_at)
|
||||||
SQL
|
SQL
|
||||||
|
log "Finished. Imported #{Mongo::Contact.count} contacts."
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_raw_post_visibilities
|
def import_raw_post_visibilities
|
||||||
|
log "Loading post visibilities file..."
|
||||||
Mongo::Aspect.connection.execute <<-SQL
|
Mongo::Aspect.connection.execute <<-SQL
|
||||||
#{load_string("post_visibilities")}
|
#{load_string("post_visibilities")}
|
||||||
#{infile_opts}
|
#{infile_opts}
|
||||||
(aspect_mongo_id, post_mongo_id)
|
(aspect_mongo_id, post_mongo_id)
|
||||||
SQL
|
SQL
|
||||||
|
log "Finished. Imported #{Mongo::PostVisibility.count} post visibilities."
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_raw_requests
|
def import_raw_requests
|
||||||
|
log "Loading requests file..."
|
||||||
Mongo::Aspect.connection.execute <<-SQL
|
Mongo::Aspect.connection.execute <<-SQL
|
||||||
#{load_string("requests")}
|
#{load_string("requests")}
|
||||||
#{infile_opts}
|
#{infile_opts}
|
||||||
(mongo_id, recipient_mongo_id, sender_mongo_id, aspect_mongo_id)
|
(mongo_id, recipient_mongo_id, sender_mongo_id, aspect_mongo_id)
|
||||||
SQL
|
SQL
|
||||||
|
log "Finished. Imported #{Mongo::Request.count} requests."
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
def infile_opts
|
||||||
|
<<-OPTS
|
||||||
|
FIELDS TERMINATED BY ','
|
||||||
|
ENCLOSED BY '"'
|
||||||
|
IGNORE 1 LINES
|
||||||
|
OPTS
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_string model_name
|
||||||
|
"LOAD DATA INFILE '#{full_path}/#{model_name}.csv' INTO TABLE mongo_#{model_name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ namespace :migrations do
|
||||||
desc 'export data for mysql import'
|
desc 'export data for mysql import'
|
||||||
task :export_for_mysql do
|
task :export_for_mysql do
|
||||||
migrator = DataConversion::ExportFromMongo.new
|
migrator = DataConversion::ExportFromMongo.new
|
||||||
|
migrator.full_path = "/tmp/data_conversion"
|
||||||
migrator.log("**** Starting export for MySQL ****")
|
migrator.log("**** Starting export for MySQL ****")
|
||||||
migrator.clear_dir
|
migrator.clear_dir
|
||||||
migrator.write_json_export
|
migrator.write_json_export
|
||||||
|
|
@ -15,4 +16,15 @@ namespace :migrations do
|
||||||
migrator.log("**** Export finished! ****")
|
migrator.log("**** Export finished! ****")
|
||||||
migrator.log("total elapsed time")
|
migrator.log("total elapsed time")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc 'import data to mysql'
|
||||||
|
task :import_to_mysql => :environment do
|
||||||
|
migrator = DataConversion::ImportToMysql.new
|
||||||
|
migrator.full_path = "/tmp/data_conversion/csv"
|
||||||
|
migrator.log("**** Starting import to MySQL database #{ActiveRecord::Base.connection.current_database} ****")
|
||||||
|
migrator.import_raw
|
||||||
|
migrator.process_raw_tables
|
||||||
|
migrator.log("**** Import finished! ****")
|
||||||
|
migrator.log("total elapsed time")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue