debian-mirror-gitlab/db/post_migrate/20161109150329_fix_project_records_with_invalid_visibility.rb

50 lines
1.5 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration[4.2]
2017-08-17 22:00:37 +05:30
include Gitlab::Database::MigrationHelpers
BATCH_SIZE = 500
DOWNTIME = false
# This migration is idempotent and there's no sense in throwing away the
# partial result if it's interrupted
disable_ddl_transaction!
def up
projects = Arel::Table.new(:projects)
namespaces = Arel::Table.new(:namespaces)
finder_sql =
2017-09-10 17:25:29 +05:30
projects
.join(namespaces, Arel::Nodes::InnerJoin)
.on(projects[:namespace_id].eq(namespaces[:id]))
.where(projects[:visibility_level].gt(namespaces[:visibility_level]))
.project(projects[:id], namespaces[:visibility_level])
.take(BATCH_SIZE)
.to_sql
2017-08-17 22:00:37 +05:30
# Update matching rows in batches. Each batch can cause up to 3 UPDATE
# statements, in addition to the SELECT: one per visibility_level
loop do
to_update = connection.exec_query(finder_sql)
break if to_update.rows.count == 0
# 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|
obj[row[1]] << row[0]
end
updates.each do |visibility_level, project_ids|
2019-02-15 15:39:39 +05:30
updater = Arel::UpdateManager.new
2017-09-10 17:25:29 +05:30
.table(projects)
.set(projects[:visibility_level] => visibility_level)
.where(projects[:id].in(project_ids))
2017-08-17 22:00:37 +05:30
ActiveRecord::Base.connection.exec_update(updater.to_sql, self.class.name, [])
end
end
end
def down
# no-op
end
end