assert on type before typecasting, do not tolerate empty strings where NULL should be.

This commit is contained in:
Raphael 2011-01-07 10:49:07 -08:00
parent 0f8b8bbd5b
commit 686713dc4b
2 changed files with 45 additions and 23 deletions

View file

@ -7,8 +7,11 @@ module DataConversion
def boolean_set(string)
"#{string}= IF(STRCMP(@#{string},'false'), TRUE, FALSE)"
end
def nil_es(string)
"#{string} = NULLIF(@#{string}, '')"
end
def import_raw
truncate_tables
import_raw_users
@ -40,10 +43,13 @@ module DataConversion
#{load_string("users")}
#{infile_opts}
(mongo_id, username, serialized_private_key, encrypted_password,
invites, invitation_token, invitation_sent_at, @getting_started,
@disable_mail, language, last_sign_in_ip, @last_sign_in_at_var,
reset_password_token, password_salt)
invites, @invitation_token, invitation_sent_at, @getting_started,
@disable_mail, language, @last_sign_in_ip, @last_sign_in_at_var,
@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)),
#{nil_es("invitation_token")},
#{nil_es("last_sign_in_ip")},
#{nil_es("reset_password_token")},
#{boolean_set("getting_started")},
#{boolean_set("disable_mail")};
SQL
@ -77,7 +83,7 @@ module DataConversion
#{infile_opts}
(mongo_id, post_mongo_id, person_mongo_id, @diaspora_handle, text, @youtube_titles)
SET guid = mongo_id,
youtube_titles = NULLIF(@youtube_titles, '');
#{nil_es("youtube_titles")};
SQL
log "Finished. Imported #{Mongo::Comment.count} comments."
end
@ -88,7 +94,7 @@ module DataConversion
#{infile_opts}
(@youtube_titles,@pending,created_at,@public,updated_at,status_message_mongo_id,caption,remote_photo_path,remote_photo_name,random_string,image,mongo_id,type,diaspora_handle,person_mongo_id,message)
SET guid = mongo_id,
youtube_titles = NULLIF(@youtube_titles, ''),
#{nil_es("youtube_titles")},
#{boolean_set("pending")},
#{boolean_set("public")};
SQL
@ -110,7 +116,12 @@ module DataConversion
Mongo::Service.connection.execute <<-SQL
#{load_string("services")}
#{infile_opts}
(type,user_mongo_id,provider,uid,access_token,access_secret,nickname)
(type,user_mongo_id,@provider,@uid,@access_token,@access_secret,@nickname)
SET #{nil_es("provider")},
#{nil_es("uid")},
#{nil_es("access_token")},
#{nil_es("access_secret")},
#{nil_es("nickname")};
SQL
log "Finished. Imported #{Mongo::Service.count} services."
end
@ -130,7 +141,8 @@ module DataConversion
Mongo::Request.connection.execute <<-SQL
#{load_string("requests")}
#{infile_opts}
(mongo_id, recipient_mongo_id, sender_mongo_id, aspect_mongo_id)
(mongo_id, recipient_mongo_id, sender_mongo_id, @aspect_mongo_id)
SET #{nil_es("aspect_mongo_id")};
SQL
log "Finished. Imported #{Mongo::Request.count} requests."
end
@ -158,9 +170,9 @@ module DataConversion
Mongo::Person.connection.execute <<-SQL
#{load_string("people")}
#{infile_opts}
(created_at,updated_at,serialized_public_key,url,mongo_id,@owner_mongo_id_var,diaspora_handle)
(created_at,updated_at,serialized_public_key,url,mongo_id,@owner_mongo_id,diaspora_handle)
SET guid = mongo_id,
owner_mongo_id = NULLIF(@owner_mongo_id_var, '');
#{nil_es("owner_mongo_id")};
SQL
log "Finished. Imported #{Mongo::Person.count} people."
end
@ -169,8 +181,18 @@ module DataConversion
Mongo::Profile.connection.execute <<-SQL
#{load_string("profiles")}
#{infile_opts}
(image_url_medium,@searchable,image_url,person_mongo_id,gender,diaspora_handle,birthday,last_name,bio,image_url_small,first_name)
SET #{boolean_set("searchable")};
(@image_url_medium,@searchable,@image_url,person_mongo_id,
@gender,@diaspora_handle,birthday,@last_name,@bio,
@image_url_small,@first_name)
SET #{boolean_set("searchable")},
#{nil_es("image_url_medium")},
#{nil_es("image_url")},
#{nil_es("gender")},
#{nil_es("diaspora_handle")},
#{nil_es("last_name")},
#{nil_es("bio")},
#{nil_es("image_url_small")},
#{nil_es("first_name")};
SQL
#STRCMP returns 0 if the arguments are the same
log "Finished. Imported #{Mongo::Profile.count} profiles."

View file

@ -232,16 +232,16 @@ describe DataConversion::ImportToMysql do
it "imports all the columns" do
@migrator.import_raw_profiles
profile = Mongo::Profile.first
profile.image_url_medium.should == ''
profile.image_url_medium.should be_nil
profile.searchable.should == true
profile.image_url.should == ''
profile.image_url.should be_nil
profile.person_mongo_id.should == "4d262129cc8cb44df2000001"
profile.gender.should == ''
profile.diaspora_handle.should == ''
profile.gender.should be_nil
profile.diaspora_handle.should be_nil
profile.birthday.should be_nil
profile.last_name.should == 'weinstien'
profile.bio.should == ''
profile.image_url_small.should == ''
profile.bio.should be_nil
profile.image_url_small.should be_nil
profile.first_name.should == 'eugene'
end
end
@ -263,7 +263,7 @@ describe DataConversion::ImportToMysql do
request.mongo_id.should == "4d26212ccc8cb44df200001b"
request.recipient_mongo_id.should =="4d26212bcc8cb44df2000018"
request.sender_mongo_id.should == "4d26212bcc8cb44df2000014"
request.aspect_mongo_id.should == ''
request.aspect_mongo_id.should be_nil
end
end
describe "services" do
@ -280,7 +280,7 @@ describe DataConversion::ImportToMysql do
it "imports all the columns" do
@migrator.import_raw_services
service = Mongo::Service.first
service.type.should == "Services::Facebook"
service.type_before_type_cast.should == "Services::Facebook"
service.user_mongo_id.should == "4d2657eacc8cb46033000011"
service.provider.should be_nil
service.uid.should be_nil
@ -306,14 +306,14 @@ describe DataConversion::ImportToMysql do
bob.serialized_private_key.should_not be_nil
bob.encrypted_password.should_not be_nil
bob.invites.should == 4
bob.invitation_token.should == ""
bob.invitation_token.should be_nil
bob.invitation_sent_at.should be_nil
bob.getting_started.should be_true
bob.disable_mail.should be_false
bob.language.should == 'en'
bob.last_sign_in_ip.should == ''
bob.last_sign_in_ip.should be_nil
bob.last_sign_in_at.to_i.should_not be_nil
bob.reset_password_token.should == ""
bob.reset_password_token.should be_nil
bob.password_salt.should_not be_nil
end
end