Коммит 64548451 создал по автору Eulyeon Ko's avatar Eulyeon Ko
Просмотр файлов

Merge branch '386474-make-user-user_type-not-nullable-part-3' into 'master'

Data migration: Update user_type value for humans

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/115849



Merged-by: default avatarEulyeon Ko <5961404-euko@users.noreply.gitlab.com>
Approved-by: default avatarCorinna Gogolok <cgogolok@gitlab.com>
Approved-by: default avatarDominic Bauer <dbauer@gitlab.com>
Approved-by: default avatarAhmed Hemdan <ahemdan@gitlab.com>
Approved-by: default avatarEulyeon Ko <5961404-euko@users.noreply.gitlab.com>
Reviewed-by: default avatarPavel Shutsin <pshutsin@gitlab.com>
Reviewed-by: default avatarAhmed Hemdan <ahemdan@gitlab.com>
Co-authored-by: default avatarPavel Shutsin <pshutsin@gitlab.com>
владельцы fc631d5a 1def6c53
---
migration_job_name: MigrateHumanUserType
description: Migrates human user type from old value (nil) to new value (0) for better indexing
feature_category: user_management
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/115849
milestone: 16.0
# frozen_string_literal: true
class QueueMigrateHumanUserType < Gitlab::Database::Migration[2.1]
MIGRATION = "MigrateHumanUserType"
DELAY_INTERVAL = 2.minutes
BATCH_SIZE = 2000
SUB_BATCH_SIZE = 10
restrict_gitlab_migration gitlab_schema: :gitlab_main
def up
queue_batched_background_migration(
MIGRATION,
:users,
:id,
job_interval: DELAY_INTERVAL,
batch_size: BATCH_SIZE,
sub_batch_size: SUB_BATCH_SIZE
)
end
def down
delete_batched_background_migration(MIGRATION, :users, :id, [])
end
end
c5c1627079f081d241031e0ffb205b3088e83e97514e692d3093ab012d727365
\ No newline at end of file
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Migrates all users with user_type = nil to user_type = 0
class MigrateHumanUserType < BatchedMigrationJob
OLD_TYPE_VALUE = nil
NEW_TYPE_VALUE = 0
operation_name :migrate_human_user_type
scope_to ->(relation) { relation.where(user_type: OLD_TYPE_VALUE) }
feature_category :user_management
def perform
cleanup_gin_indexes('users')
each_sub_batch do |sub_batch|
sub_batch.update_all(user_type: NEW_TYPE_VALUE)
end
end
private
def cleanup_gin_indexes(table_name)
sql = <<-SQL
SELECT indexname::text FROM pg_indexes WHERE tablename = '#{table_name}' AND indexdef ILIKE '%using gin%'
SQL
index_names = ApplicationRecord.connection.select_values(sql)
index_names.each do |index_name|
ApplicationRecord.connection.execute("SELECT gin_clean_pending_list('#{index_name}')")
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::MigrateHumanUserType, schema: 20230327103401, feature_category: :user_management do # rubocop:disable Layout/LineLength
let!(:valid_users) do
# 13 is the max value we have at the moment.
(0..13).map do |type|
table(:users).create!(username: "user#{type}", email: "user#{type}@test.com", user_type: type, projects_limit: 0)
end
end
let!(:user_to_update) do
table(:users).create!(username: "user_nil", email: "user_nil@test.com", user_type: nil, projects_limit: 0)
end
let(:starting_id) { table(:users).pluck(:id).min }
let(:end_id) { table(:users).pluck(:id).max }
let(:migration) do
described_class.new(
start_id: starting_id,
end_id: end_id,
batch_table: :users,
batch_column: :id,
sub_batch_size: 100,
pause_ms: 2,
connection: ::ApplicationRecord.connection
)
end
describe 'perform' do
it 'updates user with `nil` user type only' do
expect do
migration.perform
valid_users.map(&:reload)
user_to_update.reload
end.not_to change { valid_users.map(&:user_type) }
expect(user_to_update.user_type).to eq(0)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe QueueMigrateHumanUserType, feature_category: :user_management do
let(:batched_migration) { described_class::MIGRATION }
it 'schedules a new batched migration' do
reversible_migration do |migration|
migration.before -> {
expect(batched_migration).not_to have_scheduled_batched_migration
}
migration.after -> {
expect(batched_migration).to have_scheduled_batched_migration(
table_name: :users,
column_name: :id,
interval: described_class::DELAY_INTERVAL,
batch_size: described_class::BATCH_SIZE,
sub_batch_size: described_class::SUB_BATCH_SIZE
)
}
end
end
end
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать