Commit f88d5132 authored by Andreas Brandl's avatar Andreas Brandl

Simpler migration strategy for MySQL.

MySQL does not return the number of inserted rows (or at least not
expose them through cmd_tuples). Let's do one-shot INSERT instead.
parent f579a087
...@@ -6,7 +6,24 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration ...@@ -6,7 +6,24 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration
disable_ddl_transaction! disable_ddl_transaction!
BATCH_SIZE = 100000 def up
if Gitlab::Database.postgresql?
PostgresStrategy.new
else
MysqlStrategy.new
end.up
end
def down
execute "TRUNCATE user_contributed_projects"
end
private
class PostgresStrategy < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
BATCH_SIZE = 100_000
SLEEP_TIME = 30 SLEEP_TIME = 30
def up def up
...@@ -30,11 +47,7 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration ...@@ -30,11 +47,7 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration
end while result.cmd_tuples > 0 end while result.cmd_tuples > 0
end end
execute "ANALYZE user_contributed_projects" if Gitlab::Database.postgresql? execute "ANALYZE user_contributed_projects"
end
def down
execute "TRUNCATE user_contributed_projects"
end end
private private
...@@ -45,4 +58,20 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration ...@@ -45,4 +58,20 @@ class BuildUserContributedProjectsTable < ActiveRecord::Migration
ensure ensure
remove_concurrent_index(*args) if index_exists?(*args) remove_concurrent_index(*args) if index_exists?(*args)
end end
end
class MysqlStrategy < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
def up
execute <<~SQL
INSERT INTO user_contributed_projects (user_id, project_id)
SELECT e.user_id, e.project_id
FROM (SELECT DISTINCT author_id AS user_id, project_id FROM events WHERE project_id IS NOT NULL) AS e
LEFT JOIN user_contributed_projects ucp USING (user_id, project_id)
WHERE ucp.user_id IS NULL
SQL
end
end
end end
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