Skip to content
Browse files

MySQL: treat integer with :limit => 11 as a display width, not byte s…

…ize, for backward-compatibility.
  • Loading branch information...
1 parent be099c0 commit a892af60cb0ba3dd9d58d557c0bb1d11dbbf045b @jeremy jeremy committed Jun 27, 2008
View
3 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -304,8 +304,7 @@ def [](name)
#
# Available options are (none of these exists by default):
# * <tt>:limit</tt> -
- # Requests a maximum column length (<tt>:string</tt>, <tt>:text</tt>,
- # <tt>:binary</tt> or <tt>:integer</tt> columns only)
+ # Requests a maximum column length. This is number of characters for <tt>:string</tt> and <tt>:text</tt> columns and number of bytes for :binary and :integer columns.
# * <tt>:default</tt> -
# The column's default value. Use nil for NULL.
# * <tt>:null</tt> -
View
11 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -480,11 +480,12 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
return super unless type.to_s == 'integer'
case limit
- when 1; 'tinyint'
- when 2; 'smallint'
- when 3; 'mediumint'
- when 4, nil; 'int(11)'
- else; 'bigint'
+ when 1; 'tinyint'
+ when 2; 'smallint'
+ when 3; 'mediumint'
+ when nil, 4, 11; 'int(11)' # compatibility with MySQL default
+ when 5..8; 'bigint'
+ else raise(ActiveRecordError, "No integer type has byte size #{limit}")
end
end
View
12 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -48,9 +48,12 @@ def initialize(name, default, sql_type = nil, null = true)
private
def extract_limit(sql_type)
- return 8 if sql_type =~ /^bigint/i
- return 2 if sql_type =~ /^smallint/i
- super
+ case sql_type
+ when /^integer/i; 4
+ when /^bigint/i; 8
+ when /^smallint/i; 2
+ else super
+ end
end
# Extracts the scale from PostgreSQL-specific data types.
@@ -776,9 +779,10 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
when 1..2; 'smallint'
when 3..4, nil; 'integer'
when 5..8; 'bigint'
+ else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
end
end
-
+
# Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
#
# PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
View
24 activerecord/test/cases/migration_test.rb
@@ -153,9 +153,10 @@ def test_create_table_with_limits
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
+ t.column :one_int, :integer, :limit => 1
+ t.column :four_int, :integer, :limit => 4
+ t.column :eight_int, :integer, :limit => 8
+ t.column :eleven_int, :integer, :limit => 11
end
end
@@ -167,17 +168,20 @@ def test_create_table_with_limits
one = columns.detect { |c| c.name == "one_int" }
four = columns.detect { |c| c.name == "four_int" }
eight = columns.detect { |c| c.name == "eight_int" }
+ eleven = columns.detect { |c| c.name == "eleven_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
+ assert_equal 'integer', eleven.sql_type
elsif current_adapter?(:MysqlAdapter)
- assert_match /^int\(\d+\)/, default.sql_type
- assert_match /^tinyint\(\d+\)/, one.sql_type
- assert_match /^int\(\d+\)/, four.sql_type
- assert_match /^bigint\(\d+\)/, eight.sql_type
+ assert_match 'int(11)', default.sql_type
+ assert_match 'tinyint', one.sql_type
+ assert_match 'int', four.sql_type
+ assert_match 'bigint', eight.sql_type
+ assert_match 'int(11)', eleven.sql_type
elsif current_adapter?(:OracleAdapter)
assert_equal 'NUMBER(38)', default.sql_type
assert_equal 'NUMBER(1)', one.sql_type
@@ -1242,10 +1246,10 @@ def string_column
end
def integer_column
- if current_adapter?(:SQLite3Adapter) || current_adapter?(:SQLiteAdapter) || current_adapter?(:PostgreSQLAdapter)
- "integer"
- else
+ if current_adapter?(:MysqlAdapter)
'int(11)'
+ else
+ 'integer'
end
end

0 comments on commit a892af6

Please sign in to comment.
Something went wrong with that request. Please try again.