Skip to content

Commit

Permalink
Merge pull request rails#6349 from erichmenge/patch-raise-type-errors
Browse files Browse the repository at this point in the history
Integer limit out of range should be allowed to raise. Closes rails#6272
  • Loading branch information
tenderlove committed May 16, 2012
2 parents 2ee8ed4 + 56cef49 commit 4025efb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Expand Up @@ -23,7 +23,7 @@ def string_to_binary(value)
end

def sql_type
base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
base.type_to_sql(type.to_sym, limit, precision, scale)
end

def to_sql
Expand Down
4 changes: 0 additions & 4 deletions activerecord/test/cases/migration/change_schema_test.rb
Expand Up @@ -83,7 +83,6 @@ def test_create_table_with_limits
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

columns = connection.columns(:testings)
Expand All @@ -94,20 +93,17 @@ 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) or current_adapter?(:Mysql2Adapter)
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
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/migration/column_attributes_test.rb
Expand Up @@ -183,6 +183,16 @@ def test_native_types
assert_instance_of TrueClass, bob.male?
assert_kind_of BigDecimal, bob.wealth
end

def test_out_of_range_limit_should_raise
skip("MySQL and PostgreSQL only") unless current_adapter?(:MysqlAdapter, :Mysql2Adapter, :PostgreSQLAdapter)

assert_raise(ActiveRecordError) { add_column :test_models, :integer_too_big, :integer, :limit => 10 }

unless current_adapter?(:PostgreSQLAdapter)
assert_raise(ActiveRecordError) { add_column :test_models, :text_too_big, :integer, :limit => 0xfffffffff }
end
end
end
end
end
25 changes: 23 additions & 2 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -375,6 +375,27 @@ def test_create_table_with_custom_sequence_name
end
end

def test_out_of_range_limit_should_raise
skip("MySQL and PostgreSQL only") unless current_adapter?(:MysqlAdapter, :Mysql2Adapter, :PostgreSQLAdapter)

Person.connection.drop_table :test_limits rescue nil
assert_raise(ActiveRecord::ActiveRecordError, "integer limit didn't raise") do
Person.connection.create_table :test_integer_limits, :force => true do |t|
t.column :bigone, :integer, :limit => 10
end
end

unless current_adapter?(:PostgreSQLAdapter)
assert_raise(ActiveRecord::ActiveRecordError, "text limit didn't raise") do
Person.connection.create_table :test_text_limits, :force => true do |t|
t.column :bigtext, :text, :limit => 0xfffffffff
end
end
end

Person.connection.drop_table :test_limits rescue nil
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
Expand Down Expand Up @@ -883,8 +904,8 @@ def test_copying_migrations_with_timestamps_to_destination_with_timestamps_in_fu
def test_skipping_migrations
@migrations_path = MIGRATIONS_ROOT + "/valid_with_timestamps"
@existing_migrations = Dir[@migrations_path + "/*.rb"]
sources = {}

sources = {}
sources[:bukkits] = MIGRATIONS_ROOT + "/to_copy_with_timestamps"
sources[:omg] = MIGRATIONS_ROOT + "/to_copy_with_name_collision"

Expand Down

0 comments on commit 4025efb

Please sign in to comment.