mysql timezones: PWND.

This commit is contained in:
Sarah Mei 2011-01-10 20:40:27 -08:00
parent 6ba587155a
commit 68eedd578a
2 changed files with 40 additions and 14 deletions

View file

@ -5,16 +5,6 @@
module DataConversion module DataConversion
class ImportToMysql < DataConversion::Base class ImportToMysql < DataConversion::Base
def boolean_set(string)
"#{string}= IF(STRCMP(@#{string},'false'), TRUE, FALSE)"
end
def nil_es(string)
"#{string} = NULLIF(@#{string}, '')"
end
def unix_time(string)
"#{string} = FROM_UNIXTIME(@#{string} / 1000)"
end
def import_raw def import_raw
truncate_tables truncate_tables
@ -297,8 +287,10 @@ module DataConversion
end end
log "Done setting target_id" log "Done setting target_id"
end end
def import_raw_users def import_raw_users
log "Loading users file..." log "Loading users file..."
Mongo::User.connection.execute set_time_zone_to_utc
Mongo::User.connection.execute <<-SQL Mongo::User.connection.execute <<-SQL
#{load_string("users")} #{load_string("users")}
#{infile_opts} #{infile_opts}
@ -320,6 +312,7 @@ module DataConversion
def import_raw_aspects def import_raw_aspects
log "Loading aspects file..." log "Loading aspects file..."
Mongo::Aspect.connection.execute set_time_zone_to_utc
Mongo::Aspect.connection.execute <<-SQL Mongo::Aspect.connection.execute <<-SQL
#{load_string("aspects")} #{load_string("aspects")}
#{infile_opts} #{infile_opts}
@ -332,6 +325,7 @@ module DataConversion
def import_raw_aspect_memberships def import_raw_aspect_memberships
log "Loading aspect memberships file..." log "Loading aspect memberships file..."
Mongo::AspectMembership.connection.execute set_time_zone_to_utc
Mongo::AspectMembership.connection.execute <<-SQL Mongo::AspectMembership.connection.execute <<-SQL
#{load_string("aspect_memberships")} #{load_string("aspect_memberships")}
#{infile_opts} #{infile_opts}
@ -342,6 +336,7 @@ module DataConversion
def import_raw_comments def import_raw_comments
log "Loading comments file..." log "Loading comments file..."
Mongo::Comment.connection.execute set_time_zone_to_utc
Mongo::Comment.connection.execute <<-SQL Mongo::Comment.connection.execute <<-SQL
#{load_string("comments")} #{load_string("comments")}
#{infile_opts} #{infile_opts}
@ -351,8 +346,10 @@ module DataConversion
SQL SQL
log "Finished. Imported #{Mongo::Comment.count} comments." log "Finished. Imported #{Mongo::Comment.count} comments."
end end
def import_raw_posts def import_raw_posts
log "Loading posts file..." log "Loading posts file..."
Mongo::Post.connection.execute set_time_zone_to_utc
Mongo::Post.connection.execute <<-SQL Mongo::Post.connection.execute <<-SQL
#{load_string("posts")} #{load_string("posts")}
#{infile_opts} #{infile_opts}
@ -374,8 +371,10 @@ module DataConversion
SQL SQL
log "Finished. Imported #{Mongo::Post.count} posts." log "Finished. Imported #{Mongo::Post.count} posts."
end end
def import_raw_contacts def import_raw_contacts
log "Loading contacts file..." log "Loading contacts file..."
Mongo::Contact.connection.execute set_time_zone_to_utc
Mongo::Contact.connection.execute <<-SQL Mongo::Contact.connection.execute <<-SQL
#{load_string("contacts")} #{load_string("contacts")}
#{infile_opts} #{infile_opts}
@ -387,6 +386,7 @@ module DataConversion
def import_raw_services def import_raw_services
log "Loading services file..." log "Loading services file..."
Mongo::Service.connection.execute set_time_zone_to_utc
Mongo::Service.connection.execute <<-SQL Mongo::Service.connection.execute <<-SQL
#{load_string("services")} #{load_string("services")}
#{infile_opts} #{infile_opts}
@ -402,6 +402,7 @@ module DataConversion
def import_raw_post_visibilities def import_raw_post_visibilities
log "Loading post visibilities file..." log "Loading post visibilities file..."
Mongo::PostVisibility.connection.execute set_time_zone_to_utc
Mongo::PostVisibility.connection.execute <<-SQL Mongo::PostVisibility.connection.execute <<-SQL
#{load_string("post_visibilities")} #{load_string("post_visibilities")}
#{infile_opts} #{infile_opts}
@ -412,6 +413,7 @@ module DataConversion
def import_raw_requests def import_raw_requests
log "Loading requests file..." log "Loading requests file..."
Mongo::Request.connection.execute set_time_zone_to_utc
Mongo::Request.connection.execute <<-SQL Mongo::Request.connection.execute <<-SQL
#{load_string("requests")} #{load_string("requests")}
#{infile_opts} #{infile_opts}
@ -420,8 +422,10 @@ module DataConversion
SQL SQL
log "Finished. Imported #{Mongo::Request.count} requests." log "Finished. Imported #{Mongo::Request.count} requests."
end end
def import_raw_invitations def import_raw_invitations
log "Loading invitations file..." log "Loading invitations file..."
Mongo::Invitation.connection.execute set_time_zone_to_utc
Mongo::Invitation.connection.execute <<-SQL Mongo::Invitation.connection.execute <<-SQL
#{load_string("invitations")} #{load_string("invitations")}
#{infile_opts} #{infile_opts}
@ -429,8 +433,10 @@ module DataConversion
SQL SQL
log "Finished. Imported #{Mongo::Invitation.count} invitations." log "Finished. Imported #{Mongo::Invitation.count} invitations."
end end
def import_raw_notifications def import_raw_notifications
log "Loading notifications file..." log "Loading notifications file..."
Mongo::Notification.connection.execute set_time_zone_to_utc
Mongo::Notification.connection.execute <<-SQL Mongo::Notification.connection.execute <<-SQL
#{load_string("notifications")} #{load_string("notifications")}
#{infile_opts} #{infile_opts}
@ -446,8 +452,10 @@ module DataConversion
end end
log "Notification target types set." log "Notification target types set."
end end
def import_raw_people def import_raw_people
log "Loading people file..." log "Loading people file..."
Mongo::Person.connection.execute set_time_zone_to_utc
Mongo::Person.connection.execute <<-SQL Mongo::Person.connection.execute <<-SQL
#{load_string("people")} #{load_string("people")}
#{infile_opts} #{infile_opts}
@ -459,8 +467,10 @@ module DataConversion
SQL SQL
log "Finished. Imported #{Mongo::Person.count} people." log "Finished. Imported #{Mongo::Person.count} people."
end end
def import_raw_profiles def import_raw_profiles
log "Loading profiles file..." log "Loading profiles file..."
Mongo::Profile.connection.execute set_time_zone_to_utc
Mongo::Profile.connection.execute <<-SQL Mongo::Profile.connection.execute <<-SQL
#{load_string("profiles")} #{load_string("profiles")}
#{infile_opts} #{infile_opts}
@ -480,6 +490,19 @@ module DataConversion
#STRCMP returns 0 if the arguments are the same #STRCMP returns 0 if the arguments are the same
log "Finished. Imported #{Mongo::Profile.count} profiles." log "Finished. Imported #{Mongo::Profile.count} profiles."
end end
def boolean_set(string)
"#{string}= IF(STRCMP(@#{string},'false'), TRUE, FALSE)"
end
def nil_es(string)
"#{string} = NULLIF(@#{string}, '')"
end
def unix_time(string)
"#{string} = FROM_UNIXTIME(@#{string} / 1000)"
end
def infile_opts def infile_opts
<<-OPTS <<-OPTS
FIELDS TERMINATED BY ',' FIELDS TERMINATED BY ','
@ -491,5 +514,9 @@ OPTS
def load_string model_name def load_string model_name
"LOAD DATA INFILE '#{full_path}/#{model_name}.csv' INTO TABLE mongo_#{model_name}" "LOAD DATA INFILE '#{full_path}/#{model_name}.csv' INTO TABLE mongo_#{model_name}"
end end
def set_time_zone_to_utc
"set time_zone = '+0:00'"
end
end end
end end

View file

@ -363,9 +363,8 @@ describe DataConversion::ImportToMysql do
post.person_id.should == Person.where(:mongo_id => mongo_post.person_mongo_id).first.id post.person_id.should == Person.where(:mongo_id => mongo_post.person_mongo_id).first.id
post.diaspora_handle.should == post.person.diaspora_handle post.diaspora_handle.should == post.person.diaspora_handle
post.message.should be_nil post.message.should be_nil
# puts post.created_at.utc? # == true post.created_at.should == mongo_post.created_at
post.created_at.utc.to_i.should == 1294692032 # got 1294663230- minus 8 hours post.updated_at.should == mongo_post.updated_at
post.updated_at.to_i.should == 1294692033
end end
end end
describe "notifications" do describe "notifications" do
@ -608,7 +607,7 @@ describe DataConversion::ImportToMysql do
person.url.should == "http://google-1b05052.com/" person.url.should == "http://google-1b05052.com/"
person.diaspora_handle.should == "bob-person-1fe12fb@aol.com" person.diaspora_handle.should == "bob-person-1fe12fb@aol.com"
person.serialized_public_key.should_not be_nil person.serialized_public_key.should_not be_nil
person.created_at.to_i.should == 1294358505 person.created_at.to_i.should == 1294692022
end end
end end
describe "post_visibilities" do describe "post_visibilities" do