summaryrefslogtreecommitdiff
path: root/db/migrate/20220523030805_add_web_hook_calls_to_plan_limits_paid_tiers.rb
blob: 842bb297803cfe0bd7e192232e12028dc66054c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

class AddWebHookCallsToPlanLimitsPaidTiers < Gitlab::Database::Migration[2.0]
  restrict_gitlab_migration gitlab_schema: :gitlab_main

  MAX_RATE_LIMIT_NAME = 'web_hook_calls'
  MID_RATE_LIMIT_NAME = 'web_hook_calls_mid'
  MIN_RATE_LIMIT_NAME = 'web_hook_calls_low'

  UP_FREE_LIMITS = {
    MAX_RATE_LIMIT_NAME => 500,
    MID_RATE_LIMIT_NAME => 500,
    MIN_RATE_LIMIT_NAME => 500
  }.freeze

  UP_PREMIUM_LIMITS = {
    MAX_RATE_LIMIT_NAME => 4_000,
    MID_RATE_LIMIT_NAME => 2_800,
    MIN_RATE_LIMIT_NAME => 1_600
  }.freeze

  UP_ULTIMATE_LIMITS = {
    MAX_RATE_LIMIT_NAME => 13_000,
    MID_RATE_LIMIT_NAME => 9_000,
    MIN_RATE_LIMIT_NAME => 6_000
  }.freeze

  DOWN_FREE_LIMITS = {
    # 120 is the value for 'free' migrated in `db/migrate/20210601131742_update_web_hook_calls_limit.rb`
    MAX_RATE_LIMIT_NAME => 120,
    MID_RATE_LIMIT_NAME => 0,
    MIN_RATE_LIMIT_NAME => 0
  }.freeze

  DOWN_PAID_LIMITS = {
    MAX_RATE_LIMIT_NAME => 0,
    MID_RATE_LIMIT_NAME => 0,
    MIN_RATE_LIMIT_NAME => 0
  }.freeze

  def up
    return unless Gitlab.com?

    apply_limits('free', UP_FREE_LIMITS)

    # Apply Premium limits
    apply_limits('bronze', UP_PREMIUM_LIMITS)
    apply_limits('silver', UP_PREMIUM_LIMITS)
    apply_limits('premium', UP_PREMIUM_LIMITS)
    apply_limits('premium_trial', UP_PREMIUM_LIMITS)

    # Apply Ultimate limits
    apply_limits('gold', UP_ULTIMATE_LIMITS)
    apply_limits('ultimate', UP_ULTIMATE_LIMITS)
    apply_limits('ultimate_trial', UP_ULTIMATE_LIMITS)
    apply_limits('opensource', UP_ULTIMATE_LIMITS)
  end

  def down
    return unless Gitlab.com?

    apply_limits('free', DOWN_FREE_LIMITS)

    apply_limits('bronze', DOWN_PAID_LIMITS)
    apply_limits('silver', DOWN_PAID_LIMITS)
    apply_limits('premium', DOWN_PAID_LIMITS)
    apply_limits('premium_trial', DOWN_PAID_LIMITS)
    apply_limits('gold', DOWN_PAID_LIMITS)
    apply_limits('ultimate', DOWN_PAID_LIMITS)
    apply_limits('ultimate_trial', DOWN_PAID_LIMITS)
    apply_limits('opensource', DOWN_PAID_LIMITS)
  end

  private

  def apply_limits(plan_name, limits)
    limits.each_pair do |limit_name, limit|
      create_or_update_plan_limit(limit_name, plan_name, limit)
    end
  end
end