Skip to content

Commit a497ece

Browse files
authored
Merge pull request rails#35887 from kamipo/argument_error
Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options
2 parents 35d388b + 20da6c7 commit a497ece

File tree

10 files changed

+40
-18
lines changed

10 files changed

+40
-18
lines changed

activerecord/CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
* Raise `ArgumentError` for invalid `:limit` and `:precision` like as other options.
2+
3+
Before:
4+
5+
```ruby
6+
add_column :items, :attr1, :binary, size: 10 # => ArgumentError
7+
add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
8+
add_column :items, :attr3, :integer, limit: 10 # => ActiveRecordError
9+
add_column :items, :attr4, :datetime, precision: 10 # => ActiveRecordError
10+
```
11+
12+
After:
13+
14+
```ruby
15+
add_column :items, :attr1, :binary, size: 10 # => ArgumentError
16+
add_column :items, :attr2, :decimal, scale: 10 # => ArgumentError
17+
add_column :items, :attr3, :integer, limit: 10 # => ArgumentError
18+
add_column :items, :attr4, :datetime, precision: 10 # => ArgumentError
19+
```
20+
21+
*Ryuta Kamizono*
22+
123
* Association loading isn't to be affected by scoping consistently
224
whether preloaded / eager loaded or not, with the exception of `unscoped`.
325

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, **) # :nodoc:
10971097
if (0..6) === precision
10981098
column_type_sql << "(#{precision})"
10991099
else
1100-
raise(ActiveRecordError, "No #{native[:name]} type has precision of #{precision}. The allowed range of precision is from 0 to 6")
1100+
raise ArgumentError, "No #{native[:name]} type has precision of #{precision}. The allowed range of precision is from 0 to 6"
11011101
end
11021102
elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit])
11031103
column_type_sql << "(#{limit})"

activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def limit_to_size(limit, type)
243243
when nil, 0x100..0xffff; nil
244244
when 0x10000..0xffffff; "medium"
245245
when 0x1000000..0xffffffff; "long"
246-
else raise ActiveRecordError, "No #{type} type has byte size #{limit}"
246+
else raise ArgumentError, "No #{type} type has byte size #{limit}"
247247
end
248248
end
249249
end
@@ -255,7 +255,7 @@ def integer_to_sql(limit)
255255
when 3; "mediumint"
256256
when nil, 4; "int"
257257
when 5..8; "bigint"
258-
else raise ActiveRecordError, "No integer type has byte size #{limit}. Use a decimal with scale 0 instead."
258+
else raise ArgumentError, "No integer type has byte size #{limit}. Use a decimal with scale 0 instead."
259259
end
260260
end
261261
end

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,21 +548,21 @@ def type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil, **) #
548548
# The hard limit is 1GB, because of a 32-bit size field, and TOAST.
549549
case limit
550550
when nil, 0..0x3fffffff; super(type)
551-
else raise ActiveRecordError, "No binary type has byte size #{limit}. The limit on binary can be at most 1GB - 1byte."
551+
else raise ArgumentError, "No binary type has byte size #{limit}. The limit on binary can be at most 1GB - 1byte."
552552
end
553553
when "text"
554554
# PostgreSQL doesn't support limits on text columns.
555555
# The hard limit is 1GB, according to section 8.3 in the manual.
556556
case limit
557557
when nil, 0..0x3fffffff; super(type)
558-
else raise ActiveRecordError, "No text type has byte size #{limit}. The limit on text can be at most 1GB - 1byte."
558+
else raise ArgumentError, "No text type has byte size #{limit}. The limit on text can be at most 1GB - 1byte."
559559
end
560560
when "integer"
561561
case limit
562562
when 1, 2; "smallint"
563563
when nil, 3, 4; "integer"
564564
when 5..8; "bigint"
565-
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with scale 0 instead.")
565+
else raise ArgumentError, "No integer type has byte size #{limit}. Use a numeric with scale 0 instead."
566566
end
567567
else
568568
super

activerecord/test/cases/adapters/postgresql/bytea_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_column
3535

3636
def test_binary_columns_are_limitless_the_upper_limit_is_one_GB
3737
assert_equal "bytea", @connection.type_to_sql(:binary, limit: 100_000)
38-
assert_raise ActiveRecord::ActiveRecordError do
38+
assert_raise ArgumentError do
3939
@connection.type_to_sql(:binary, limit: 4294967295)
4040
end
4141
end

activerecord/test/cases/adapters/postgresql/datatype_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_update_oid
6464

6565
def test_text_columns_are_limitless_the_upper_limit_is_one_GB
6666
assert_equal "text", @connection.type_to_sql(:text, limit: 100_000)
67-
assert_raise ActiveRecord::ActiveRecordError do
67+
assert_raise ArgumentError do
6868
@connection.type_to_sql(:text, limit: 4294967295)
6969
end
7070
end

activerecord/test/cases/date_time_precision_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_passing_precision_to_datetime_does_not_set_limit
8282
end
8383

8484
def test_invalid_datetime_precision_raises_error
85-
assert_raises ActiveRecord::ActiveRecordError do
85+
assert_raises ArgumentError do
8686
@connection.create_table(:foos, force: true) do |t|
8787
t.timestamps precision: 7
8888
end

activerecord/test/cases/migration/column_attributes_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ def test_native_types
176176

177177
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
178178
def test_out_of_range_limit_should_raise
179-
assert_raise(ActiveRecordError) { add_column :test_models, :integer_too_big, :integer, limit: 10 }
180-
assert_raise(ActiveRecordError) { add_column :test_models, :text_too_big, :text, limit: 0xfffffffff }
181-
assert_raise(ActiveRecordError) { add_column :test_models, :binary_too_big, :binary, limit: 0xfffffffff }
179+
assert_raise(ArgumentError) { add_column :test_models, :integer_too_big, :integer, limit: 10 }
180+
assert_raise(ArgumentError) { add_column :test_models, :text_too_big, :text, limit: 0xfffffffff }
181+
assert_raise(ArgumentError) { add_column :test_models, :binary_too_big, :binary, limit: 0xfffffffff }
182182
end
183183
end
184184
end

activerecord/test/cases/migration_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def test_decimal_scale_without_precision_should_raise
583583

584584
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
585585
def test_out_of_range_integer_limit_should_raise
586-
e = assert_raise(ActiveRecord::ActiveRecordError, "integer limit didn't raise") do
586+
e = assert_raise(ArgumentError) do
587587
Person.connection.create_table :test_integer_limits, force: true do |t|
588588
t.column :bigone, :integer, limit: 10
589589
end
@@ -595,7 +595,7 @@ def test_out_of_range_integer_limit_should_raise
595595
end
596596

597597
def test_out_of_range_text_limit_should_raise
598-
e = assert_raise(ActiveRecord::ActiveRecordError, "text limit didn't raise") do
598+
e = assert_raise(ArgumentError) do
599599
Person.connection.create_table :test_text_limits, force: true do |t|
600600
t.text :bigtext, limit: 0xfffffffff
601601
end
@@ -607,15 +607,15 @@ def test_out_of_range_text_limit_should_raise
607607
end
608608

609609
def test_out_of_range_binary_limit_should_raise
610-
e = assert_raise(ActiveRecord::ActiveRecordError) do
611-
Person.connection.create_table :test_text_limits, force: true do |t|
610+
e = assert_raise(ArgumentError) do
611+
Person.connection.create_table :test_binary_limits, force: true do |t|
612612
t.binary :bigbinary, limit: 0xfffffffff
613613
end
614614
end
615615

616616
assert_includes e.message, "No binary type has byte size #{0xfffffffff}"
617617
ensure
618-
Person.connection.drop_table :test_text_limits, if_exists: true
618+
Person.connection.drop_table :test_binary_limits, if_exists: true
619619
end
620620
end
621621

activerecord/test/cases/time_precision_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_passing_precision_to_time_does_not_set_limit
7575
end
7676

7777
def test_invalid_time_precision_raises_error
78-
assert_raises ActiveRecord::ActiveRecordError do
78+
assert_raises ArgumentError do
7979
@connection.create_table(:foos, force: true) do |t|
8080
t.time :start, precision: 7
8181
t.time :finish, precision: 7

0 commit comments

Comments
 (0)