Skip to content

Commit 3a89068

Browse files
committed
Merge pull request rails#8276 from pwnall/pgsql_text_limits
Postgresql doesn't accepts limits on text columns
2 parents abb38fe + a8a60e9 commit 3a89068

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped.
4+
Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types.
5+
6+
*Victor Costan*
7+
38
* Don't change STI type when calling `ActiveRecord::Base#becomes`.
49
Add `ActiveRecord::Base#becomes!` with the previous behavior.
510

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
396396
when nil, 0..0x3fffffff; super(type)
397397
else raise(ActiveRecordError, "No binary type has byte size #{limit}.")
398398
end
399+
when 'text'
400+
# PostgreSQL doesn't support limits on text columns.
401+
# The hard limit is 1Gb, according to section 8.3 in the manual.
402+
case limit
403+
when nil, 0..0x3fffffff; super(type)
404+
else raise(ActiveRecordError, "The limit on text can be at most 1GB - 1byte.")
405+
end
399406
when 'integer'
400407
return 'integer' unless limit
401408

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "cases/helper"
2+
3+
class SqlTypesTest < ActiveRecord::TestCase
4+
def test_binary_types
5+
assert_equal 'bytea', type_to_sql(:binary, 100_000)
6+
assert_raise ActiveRecord::ActiveRecordError do
7+
type_to_sql :binary, 4294967295
8+
end
9+
assert_equal 'text', type_to_sql(:text, 100_000)
10+
assert_raise ActiveRecord::ActiveRecordError do
11+
type_to_sql :text, 4294967295
12+
end
13+
end
14+
15+
def type_to_sql(*args)
16+
ActiveRecord::Base.connection.type_to_sql(*args)
17+
end
18+
end

activerecord/test/schema/postgresql_specific_schema.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,10 @@
192192
_SQL
193193
rescue #This version of PostgreSQL either has no XML support or is was not compiled with XML support: skipping table
194194
end
195+
196+
create_table :limitless_fields, force: true do |t|
197+
t.binary :binary, limit: 100_000
198+
t.text :text, limit: 100_000
199+
end
195200
end
196201

0 commit comments

Comments
 (0)