Skip to content
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

Commit 20e51b7

Browse files
author
v.promzelev
committed
specify max values of types as hash
1 parent 8b58c07 commit 20e51b7

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

lib/activerecord/overflow_signalizer.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def initialize(type = nil)
1313

1414
DAY = 24 * 60 * 60
1515

16+
MAX_VALUE = {
17+
'integer' => 2_147_483_647,
18+
'bigint' => 9_223_372_036_854_775_807
19+
}.freeze
20+
1621
def initialize(logger: nil, models: nil, days_count: 60, signalizer: nil)
1722
@logger = logger || ActiveRecord::Base.logger
1823
@models = models || ActiveRecord::Base.descendants
@@ -25,16 +30,17 @@ def analyse!
2530
model = models.first
2631
pk = model.columns.select { |c| c.name == model.primary_key }.first
2732
next if model.last.nil?
28-
if overflow_soon?(pk, model)
29-
signalize(table, model.last.public_send(pk.name), max_value(pk.sql_type))
33+
max = MAX_VALUE.fetch(pk.sql_type) { |type| raise UnsupportedType, type }
34+
if overflow_soon?(max, model)
35+
signalize(table, model.last.public_send(pk.name), max)
3036
end
3137
end
3238
end
3339

3440
private
3541

36-
def overflow_soon?(pk, model)
37-
(max_value(pk.sql_type) - model.last.id) / avg(model) <= @days_count
42+
def overflow_soon?(max, model)
43+
(max - model.last.id) / avg(model) <= @days_count
3844
end
3945

4046
def avg(model)
@@ -47,17 +53,6 @@ def avg(model)
4753
week_records.reduce(:+) / week_records.keep_if { |v| v > 0 }.size
4854
end
4955

50-
def max_value(type)
51-
case type
52-
when 'integer'
53-
2_147_483_647
54-
when 'bigint'
55-
9_223_372_036_854_775_807
56-
else
57-
raise UnsupportedType, type
58-
end
59-
end
60-
6156
def signalize(table, current_value, max_value)
6257
if current_value == max_value
6358
msg = "Primary key in table #{table} overflowed! #{current_value} from #{max_value}"

0 commit comments

Comments
 (0)