FIX: clamp integers to prevent 'PG::NumericValueOutOfRange' errors

This commit is contained in:
Régis Hanol 2018-12-04 22:51:56 +01:00
parent 28ddaf80d2
commit f1050350ed
1 changed files with 6 additions and 4 deletions

View File

@ -9,6 +9,8 @@ class MigratePollsData < ActiveRecord::Migration[5.2]
"number" => 2, "number" => 2,
} }
PG_INTEGER_MAX ||= 2_147_483_647
def up def up
# Ensure we don't have duplicate polls # Ensure we don't have duplicate polls
DB.exec <<~SQL DB.exec <<~SQL
@ -86,10 +88,10 @@ class MigratePollsData < ActiveRecord::Migration[5.2]
status = poll["status"] == "open" ? 0 : 1 status = poll["status"] == "open" ? 0 : 1
visibility = poll["public"] == "true" ? 1 : 0 visibility = poll["public"] == "true" ? 1 : 0
close_at = (Time.zone.parse(poll["close"]) rescue nil) close_at = (Time.zone.parse(poll["close"]) rescue nil)
min = poll["min"].to_i min = poll["min"].to_i.clamp(0, PG_INTEGER_MAX)
max = poll["max"].to_i max = poll["max"].to_i.clamp(0, PG_INTEGER_MAX)
step = poll["step"].to_i step = poll["step"].to_i.clamp(0, max)
anonymous_voters = poll["anonymous_voters"].to_i anonymous_voters = poll["anonymous_voters"].to_i.clamp(0, PG_INTEGER_MAX)
poll_id = execute(<<~SQL poll_id = execute(<<~SQL
INSERT INTO polls ( INSERT INTO polls (