Skip to content

Commit

Permalink
Unifies mysql and mysql2 casting of booleans.
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed Nov 11, 2013
1 parent 07790d5 commit 07ae1e9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Unify boolean type casting for `MysqlAdapter` and `Mysql2Adapter`.
`type_cast` will return `1` for `true` and `0` for `false`.

Fixes #11119.

*Adam Williams*, *Yves Senn*

* Fix bug where has_one associaton record update result in crash, when replaced with itself.

Fixes #12834.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def supports_index_sort_order?
true
end

def type_cast(value, column)
return super unless value == true || value == false

value ? 1 : 0
end

# MySQL 4 technically support transaction isolation, but it is affected by a bug
# where the transaction level gets persisted for the whole session:
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ def error_number(exception) # :nodoc:

# QUOTING ==================================================

def type_cast(value, column)
return super unless value == true || value == false

value ? 1 : 0
end

def quote_string(string) #:nodoc:
@connection.quote(string)
end
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/adapters/mysql2/boolean_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class BooleanType < ActiveRecord::Base
assert_equal 1, attributes["archived"]
assert_equal "1", attributes["published"]

assert_equal "t", @connection.type_cast(true, boolean_column)
assert_equal "t", @connection.type_cast(true, string_column)
assert_equal 1, @connection.type_cast(true, boolean_column)
assert_equal 1, @connection.type_cast(true, string_column)
end

test "test type casting without emulated booleans" do
Expand All @@ -60,7 +60,7 @@ class BooleanType < ActiveRecord::Base
assert_equal "1", attributes["published"]

assert_equal 1, @connection.type_cast(true, boolean_column)
assert_equal "t", @connection.type_cast(true, string_column)
assert_equal 1, @connection.type_cast(true, string_column)
end

test "with booleans stored as 1 and 0" do
Expand Down

0 comments on commit 07ae1e9

Please sign in to comment.