diff options
author | Valery Sizov <vsv2711@gmail.com> | 2014-12-08 16:15:11 +0300 |
---|---|---|
committer | Valery Sizov <vsv2711@gmail.com> | 2014-12-08 16:15:11 +0300 |
commit | 7d8dfccf70da047b68f974b7e63610f5c7bf1a43 (patch) | |
tree | 06bf26d2f555297fa6b77d0f2840451393399c24 /db/migrate/20141121161704_add_identity_table.rb | |
parent | bbf9953b99d59801c72dd7b9550ee149ca77bfcf (diff) |
speed up migration to identities
Diffstat (limited to 'db/migrate/20141121161704_add_identity_table.rb')
-rw-r--r-- | db/migrate/20141121161704_add_identity_table.rb | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/db/migrate/20141121161704_add_identity_table.rb b/db/migrate/20141121161704_add_identity_table.rb index 243958039af..6fe63637dfe 100644 --- a/db/migrate/20141121161704_add_identity_table.rb +++ b/db/migrate/20141121161704_add_identity_table.rb @@ -8,9 +8,11 @@ class AddIdentityTable < ActiveRecord::Migration add_index :identities, :user_id - User.where("provider IS NOT NULL").find_each do |user| - execute "INSERT INTO identities(provider, extern_uid, user_id) VALUES('#{user.provider}', '#{user.extern_uid}', '#{user.id}')" - end + execute <<eos +INSERT INTO identities (provider, extern_uid, user_id) +SELECT provider, extern_uid, id FROM users +WHERE provider IS NOT NULL +eos remove_column :users, :extern_uid remove_column :users, :provider @@ -20,12 +22,16 @@ class AddIdentityTable < ActiveRecord::Migration add_column :users, :extern_uid, :string add_column :users, :provider, :string - User.where("id IN(SELECT user_id FROM identities)").find_each do |user| - identity = user.identities.last - user.extern_uid = identity.extern_uid - user.provider = identity.provider - user.save - end + if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' + execute <<eos +UPDATE users u +SET provider = i.provider, extern_uid = i.extern_uid +FROM identities i +WHERE i.user_id = u.id +eos + else + execute "UPDATE users u, identities i SET u.provider = i.provider, u.extern_uid = i.extern_uid WHERE u.id = i.user_id" + end drop_table :identities end |