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

FIX: clamp integers to prevent ‘PG::NumericValueOutOfRange’ errors

From f1050350ede7491e158d9fd2b0600f26d40c403c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Tue, 4 Dec 2018 22:51:56 +0100
Subject: [PATCH] FIX: clamp integers to prevent 'PG::NumericValueOutOfRange'
 errors


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

GitHub

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

1 Like