MySQL: treat integer with :limit => 11 as a display width, not byte size, for backward-compatibility.

This commit is contained in:
Jeremy Kemper 2008-06-27 01:06:33 -07:00
parent b2b761166d
commit 4498aad4ac
4 changed files with 29 additions and 21 deletions

View File

@ -304,8 +304,7 @@ module ActiveRecord
#
# 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 File

@ -473,11 +473,12 @@ module ActiveRecord
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 File

@ -48,9 +48,12 @@ module ActiveRecord
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.
@ -795,9 +798,10 @@ module ActiveRecord
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 File

@ -153,9 +153,10 @@ if ActiveRecord::Base.connection.supports_migrations?
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 @@ if ActiveRecord::Base.connection.supports_migrations?
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 @@ if ActiveRecord::Base.connection.supports_migrations?
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