Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

PostgreSQL: migrations support :limit with :integer columns by mappin…

…g limit < 4 to smallint, > 4 to bigint, and anything else to integer. Closes #2900.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4335 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 491b4a3c84194931de51c3331b028dcfcb606074 1 parent c069f36
Jeremy Kemper jeremy authored
2  activerecord/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* PostgreSQL: migrations support :limit with :integer columns by mapping limit < 4 to smallint, > 4 to bigint, and anything else to integer. #2900 [keegan@thebasement.org]
+
* Dates and times interpret empty strings as nil rather than 2000-01-01. #4830 [kajism@yahoo.com]
* Allow :uniq => true with has_many :through associations. [Jeremy Kemper]
12 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
View
@@ -339,6 +339,18 @@ def remove_index(table_name, options) #:nodoc:
execute "DROP INDEX #{index_name(table_name, options)}"
end
+ def type_to_sql(type, limit = nil) #:nodoc:
+ return super unless type.to_s == 'integer'
+
+ if limit.nil? || limit == 4
+ 'integer'
+ elsif limit < 4
+ 'smallint'
+ else
+ 'bigint'
+ end
+ end
+
private
BYTEA_COLUMN_TYPE_OID = 17
TIMESTAMPOID = 1114
40 activerecord/test/migration_test.rb
View
@@ -118,6 +118,46 @@ def test_create_table_with_defaults
ensure
Person.connection.drop_table :testings rescue nil
end
+
+ def test_create_table_with_limits
+ Person.connection.create_table :testings do |t|
+ t.column :foo, :string, :limit => 255
+
+ t.column :default_int, :integer
+
+ t.column :one_int, :integer, :limit => 1
+ t.column :four_int, :integer, :limit => 4
+ t.column :eight_int, :integer, :limit => 8
+ end
+
+ columns = Person.connection.columns(:testings)
+ foo = columns.detect { |c| c.name == "foo" }
+ assert_equal 255, foo.limit
+
+ default = columns.detect { |c| c.name == "default_int" }
+ one = columns.detect { |c| c.name == "one_int" }
+ four = columns.detect { |c| c.name == "four_int" }
+ eight = columns.detect { |c| c.name == "eight_int" }
+
+ if current_adapter?(:PostgreSQLAdapter)
+ assert_equal 'integer', default.sql_type
+ assert_equal 'smallint', one.sql_type
+ assert_equal 'integer', four.sql_type
+ assert_equal 'bigint', eight.sql_type
+ elsif current_adapter?(:MysqlAdapter)
+ assert_equal 'int(11)', default.sql_type
+ assert_equal 'int(1)', one.sql_type
+ assert_equal 'int(4)', four.sql_type
+ assert_equal 'int(8)', eight.sql_type
+ else
+ assert_equal 'integer', default.sql_type
+ assert_equal 'integer(1)', one.sql_type
+ assert_equal 'integer(4)', four.sql_type
+ assert_equal 'integer(8)', eight.sql_type
+ end
+ ensure
+ Person.connection.drop_table :testings rescue nil
+ end
# SQL Server and Sybase will not allow you to add a NOT NULL column
# to a table without specifying a default value, so the
Please sign in to comment.
Something went wrong with that request. Please try again.