Commit 754c38bb authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'speed_up_migration' into 'master'

Speed up omniauth AddIdentityTable migration

https://dev.gitlab.org/gitlab/gitlabhq/issues/1810

See merge request !1315
parents 0e32b3c0 7d8dfccf
......@@ -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,11 +22,15 @@ 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
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment