From a8a60e92409620f795d04bc720f1dd120d6ec7e2 Mon Sep 17 00:00:00 2001 From: Victor Costan Date: Mon, 19 Nov 2012 22:38:19 -0500 Subject: [PATCH] Postgresql doesn't accepts limits on text columns. --- activerecord/CHANGELOG.md | 5 +++++ .../postgresql/schema_statements.rb | 7 +++++++ .../adapters/postgresql/sql_types_test.rb | 18 ++++++++++++++++++ .../test/schema/postgresql_specific_schema.rb | 5 +++++ 4 files changed, 35 insertions(+) create mode 100644 activerecord/test/cases/adapters/postgresql/sql_types_test.rb diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 082e3d1089..8e414d09d8 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped. + Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types. + + *Victor Costan* + * Don't change STI type when calling `ActiveRecord::Base#becomes`. Add `ActiveRecord::Base#becomes!` with the previous behavior. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 82a0b662f4..f0a03a2a37 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -396,6 +396,13 @@ module ActiveRecord when nil, 0..0x3fffffff; super(type) else raise(ActiveRecordError, "No binary type has byte size #{limit}.") end + when 'text' + # PostgreSQL doesn't support limits on text columns. + # The hard limit is 1Gb, according to section 8.3 in the manual. + case limit + when nil, 0..0x3fffffff; super(type) + else raise(ActiveRecordError, "The limit on text can be at most 1GB - 1byte.") + end when 'integer' return 'integer' unless limit diff --git a/activerecord/test/cases/adapters/postgresql/sql_types_test.rb b/activerecord/test/cases/adapters/postgresql/sql_types_test.rb new file mode 100644 index 0000000000..d7d40f6385 --- /dev/null +++ b/activerecord/test/cases/adapters/postgresql/sql_types_test.rb @@ -0,0 +1,18 @@ +require "cases/helper" + +class SqlTypesTest < ActiveRecord::TestCase + def test_binary_types + assert_equal 'bytea', type_to_sql(:binary, 100_000) + assert_raise ActiveRecord::ActiveRecordError do + type_to_sql :binary, 4294967295 + end + assert_equal 'text', type_to_sql(:text, 100_000) + assert_raise ActiveRecord::ActiveRecordError do + type_to_sql :text, 4294967295 + end + end + + def type_to_sql(*args) + ActiveRecord::Base.connection.type_to_sql(*args) + end +end diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index d0e7338f15..0cfde83778 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -192,5 +192,10 @@ end _SQL rescue #This version of PostgreSQL either has no XML support or is was not compiled with XML support: skipping table end + + create_table :limitless_fields, force: true do |t| + t.binary :binary, limit: 100_000 + t.text :text, limit: 100_000 + end end