Commit ed70f29f authored by Yorick Peterse's avatar Yorick Peterse

Merge branch '24506-fix-timing-out-migration' into 'master'

Fix a badly-performing migration

See merge request !7483
parents 6bf87395 16674d9b
class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
BATCH_SIZE = 1000 BATCH_SIZE = 500
DOWNTIME = false DOWNTIME = false
# This migration is idempotent and there's no sense in throwing away the # This migration is idempotent and there's no sense in throwing away the
...@@ -12,34 +12,34 @@ class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration ...@@ -12,34 +12,34 @@ class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration
projects = Arel::Table.new(:projects) projects = Arel::Table.new(:projects)
namespaces = Arel::Table.new(:namespaces) namespaces = Arel::Table.new(:namespaces)
finder = finder_sql =
projects. projects.
join(namespaces, Arel::Nodes::InnerJoin). join(namespaces, Arel::Nodes::InnerJoin).
on(projects[:namespace_id].eq(namespaces[:id])). on(projects[:namespace_id].eq(namespaces[:id])).
where(projects[:visibility_level].gt(namespaces[:visibility_level])). where(projects[:visibility_level].gt(namespaces[:visibility_level])).
project(projects[:id]). project(projects[:id], namespaces[:visibility_level]).
take(BATCH_SIZE) take(BATCH_SIZE).
to_sql
# MySQL requires a derived table to perform this query # Update matching rows in batches. Each batch can cause up to 3 UPDATE
nested_finder = # statements, in addition to the SELECT: one per visibility_level
projects.
from(finder.as("AS projects_inner")).
project(projects[:id])
valuer =
namespaces.
where(namespaces[:id].eq(projects[:namespace_id])).
project(namespaces[:visibility_level])
# Update matching rows until none remain. The finder contains a limit.
loop do loop do
updater = Arel::UpdateManager.new(ActiveRecord::Base). to_update = connection.exec_query(finder_sql)
table(projects). break if to_update.rows.count == 0
set(projects[:visibility_level] => Arel::Nodes::SqlLiteral.new("(#{valuer.to_sql})")).
where(projects[:id].in(nested_finder)) # row[0] is projects.id, row[1] is namespaces.visibility_level
updates = to_update.rows.each_with_object(Hash.new {|h, k| h[k] = [] }) do |row, obj|
num_updated = connection.exec_update(updater.to_sql, self.class.name, []) obj[row[1]] << row[0]
break if num_updated == 0 end
updates.each do |visibility_level, project_ids|
updater = Arel::UpdateManager.new(ActiveRecord::Base).
table(projects).
set(projects[:visibility_level] => visibility_level).
where(projects[:id].in(project_ids))
ActiveRecord::Base.connection.exec_update(updater.to_sql, self.class.name, [])
end
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