Commit f9d95243 authored by Brett Walker's avatar Brett Walker Committed by charlie ablett

Backfill default namespace as User namespace

Changelog: other
parent d30d4658
# frozen_string_literal: true
class BackfillUserNamespace < Gitlab::Database::Migration[1.0]
MIGRATION = 'BackfillUserNamespace'
INTERVAL = 2.minutes
BATCH_SIZE = 10_000
DOWNTIME = false
def up
queue_batched_background_migration(
MIGRATION,
:namespaces,
:id,
job_interval: INTERVAL,
batch_size: BATCH_SIZE
)
end
def down
Gitlab::Database::BackgroundMigration::BatchedMigration
.for_configuration(MIGRATION, :namespaces, :id, [])
.delete_all
end
end
3aaf2a4fa834331768e2acc10f67b8d456e70aca9784787e40b55dade7b6f64c
\ No newline at end of file
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Backfills the `namespaces.type` column, replacing any
# instances of `NULL` with `User`
class BackfillUserNamespace
def perform(start_id, stop_id, *args)
ActiveRecord::Base.connection.execute(<<~SQL)
UPDATE namespaces SET type = 'User'
WHERE id BETWEEN #{start_id} AND #{stop_id}
AND type IS NULL
SQL
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillUserNamespace do
let(:migration) { described_class.new }
let(:namespaces) { table(:namespaces) }
before do
namespaces.create!(id: 1, name: 'test1', path: 'test1', type: nil)
namespaces.create!(id: 2, name: 'test2', path: 'test2', type: 'User')
namespaces.create!(id: 3, name: 'test3', path: 'test3', type: 'Group')
namespaces.create!(id: 4, name: 'test4', path: 'test4', type: nil)
namespaces.create!(id: 11, name: 'test11', path: 'test11', type: nil)
end
it 'backfills `type_new` for the selected records' do
queries = ActiveRecord::QueryRecorder.new do
migration.perform(1, 10)
end
expect(queries.count).to be(1)
expect(Namespace.where(type: 'User').count).to eq 3
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe BackfillUserNamespace do
let_it_be(:migration) { described_class::MIGRATION }
describe '#up' do
it 'schedules background jobs for each batch of namespaces' do
migrate!
expect(migration).to have_scheduled_batched_migration(
table_name: :namespaces,
column_name: :id,
interval: described_class::INTERVAL
)
end
end
describe '#down' do
it 'deletes all batched migration records' do
migrate!
schema_migrate_down!
expect(migration).not_to have_scheduled_batched_migration
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