debian-mirror-gitlab/lib/gitlab/background_migration/backfill_environment_tiers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

42 lines
1.5 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# This class backfills the `environments.tier` column by using `guess_tier` logic.
# Environments created after 13.10 already have a value, however, environments created before 13.10 don't.
# See https://gitlab.com/gitlab-org/gitlab/-/issues/300741 for more information.
class BackfillEnvironmentTiers < BatchedMigrationJob
operation_name :backfill_environment_tiers
2023-03-17 16:20:25 +05:30
feature_category :database
2023-03-04 22:38:38 +05:30
# Equivalent to `Environment#guess_tier` pattern matching.
PRODUCTION_TIER = 0
STAGING_TIER = 1
TESTING_TIER = 2
DEVELOPMENT_TIER = 3
OTHER_TIER = 4
TIER_REGEXP_PAIR = [
{ tier: DEVELOPMENT_TIER, regexp: '(dev|review|trunk)' },
{ tier: TESTING_TIER, regexp: '(test|tst|int|ac(ce|)pt|qa|qc|control|quality)' },
{ tier: STAGING_TIER, regexp: '(st(a|)g|mod(e|)l|pre|demo|non)' },
{ tier: PRODUCTION_TIER, regexp: '(pr(o|)d|live)' }
].freeze
def perform
TIER_REGEXP_PAIR.each do |pair|
each_sub_batch(
batching_scope: ->(relation) { relation.where(tier: nil).where("name ~* '#{pair[:regexp]}'") } # rubocop:disable GitlabSecurity/SqlInjection
) do |sub_batch|
sub_batch.update_all(tier: pair[:tier])
end
end
each_sub_batch(batching_scope: ->(relation) { relation.where(tier: nil) }) do |sub_batch|
sub_batch.update_all(tier: OTHER_TIER)
end
end
end
end
end