Commit 7f6ac436 authored by Brett Walker's avatar Brett Walker Committed by charlie ablett

Work in progress

parent 531cf8d5
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
class BackfillUserNamespace < Gitlab::Database::Migration[1.0] class BackfillUserNamespace < Gitlab::Database::Migration[1.0]
MIGRATION = 'BackfillUserNamespace' MIGRATION = 'BackfillUserNamespace'
INTERVAL = 2.minutes INTERVAL = 2.minutes
BATCH_SIZE = 10_000 BATCH_SIZE = 1_000
SUB_BATCH_SIZE = 200
DOWNTIME = false DOWNTIME = false
def up def up
...@@ -12,7 +13,8 @@ class BackfillUserNamespace < Gitlab::Database::Migration[1.0] ...@@ -12,7 +13,8 @@ class BackfillUserNamespace < Gitlab::Database::Migration[1.0]
:namespaces, :namespaces,
:id, :id,
job_interval: INTERVAL, job_interval: INTERVAL,
batch_size: BATCH_SIZE batch_size: BATCH_SIZE,
sub_batch_size: SUB_BATCH_SIZE
) )
end end
......
...@@ -5,12 +5,41 @@ module Gitlab ...@@ -5,12 +5,41 @@ module Gitlab
# Backfills the `namespaces.type` column, replacing any # Backfills the `namespaces.type` column, replacing any
# instances of `NULL` with `User` # instances of `NULL` with `User`
class BackfillUserNamespace class BackfillUserNamespace
def perform(start_id, stop_id, *args) include Gitlab::Database::DynamicModelHelpers
ActiveRecord::Base.connection.execute(<<~SQL)
UPDATE namespaces SET type = 'User' def perform(start_id, end_id, batch_table, batch_column, sub_batch_size)
WHERE id BETWEEN #{start_id} AND #{stop_id} parent_batch_relation = relation_scoped_to_range(batch_table, batch_column, start_id, end_id)
AND type IS NULL
SQL parent_batch_relation.each_batch(column: batch_column, of: sub_batch_size) do |sub_batch|
batch_metrics.time_operation(:update_all) do
sub_batch.update_all(type: 'User')
end
pause_ms = 0 if pause_ms < 0
sleep(pause_ms * 0.001)
end
# ActiveRecord::Base.connection.execute(<<~SQL)
# UPDATE namespaces SET type = 'User'
# WHERE id BETWEEN #{start_id} AND #{end_id}
# AND type IS NULL
# SQL
end
def batch_metrics
@batch_metrics ||= Gitlab::Database::BackgroundMigration::BatchMetrics.new
end
private
def connection
ActiveRecord::Base.connection
end
def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
define_batchable_model(source_table)
.where(source_key_column => start_id..stop_id)
.where(type: nil)
end 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