2018-11-13 07:27:31 +00:00
|
|
|
class FixProjectRecordsWithInvalidVisibility < ActiveRecord::Migration[4.2]
|
2016-11-09 22:54:58 +00:00
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
2016-11-15 17:07:58 +00:00
|
|
|
BATCH_SIZE = 500
|
2016-11-09 22:54:58 +00:00
|
|
|
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)
|
|
|
|
|
2016-11-15 17:07:58 +00:00
|
|
|
finder_sql =
|
2017-06-21 13:48:12 +00:00
|
|
|
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
|
2016-11-09 22:54:58 +00:00
|
|
|
|
2016-11-15 17:07:58 +00:00
|
|
|
# Update matching rows in batches. Each batch can cause up to 3 UPDATE
|
2017-02-22 22:34:45 +00:00
|
|
|
# statements, in addition to the SELECT: one per visibility_level
|
2016-11-09 22:54:58 +00:00
|
|
|
loop do
|
2016-11-15 17:07:58 +00:00
|
|
|
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|
|
2018-12-15 09:06:56 +00:00
|
|
|
updater = Arel::UpdateManager.new
|
2017-06-21 13:48:12 +00:00
|
|
|
.table(projects)
|
|
|
|
.set(projects[:visibility_level] => visibility_level)
|
|
|
|
.where(projects[:id].in(project_ids))
|
2016-11-15 17:07:58 +00:00
|
|
|
|
|
|
|
ActiveRecord::Base.connection.exec_update(updater.to_sql, self.class.name, [])
|
|
|
|
end
|
2016-11-09 22:54:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
end
|