IT61/it61-rails

View on GitHub
lib/tasks/importer/social_accounts_importer.rb

Summary

Maintainability
A
55 mins
Test Coverage
module SocialAccountsImporter
  def import_social_accounts
    puts "Importing social accounts"

    use_old_db
    social_accounts = ActiveRecord::Base.connection.execute("SELECT * FROM authentications")
    use_new_db

    for i in 0...social_accounts.count do
      row = social_accounts.get_row(i)

      # for social accounts we save all fields as is
      social_account = SocialAccount.new
      row.fields.each do |f|
        value = row.get(f)
        # rename vk => vkontakte
        value = "vkontakte" if value == "vk"
        value = "google_oauth2" if value == "google"

        social_account.write_attribute(f, value)
      end

      begin
        social_account.save!
      rescue => e
        puts "Failed to save #{social_account.uid} for #{social_account.user_id}: #{e.message}"
      end
    end
  end
end