aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-16 09:07:23 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-16 09:07:23 -0700
commita1945cb0b9844f3a43fe03d754979885aa5df48a (patch)
tree1d747920e72c8b4128d4bb91ec008262f934cbf4
parentb8e26642504fb8722f88b68a211eeaf5d40e2749 (diff)
parent01a2ac12b90691315dec7792f98f895819c53e06 (diff)
downloadrails-a1945cb0b9844f3a43fe03d754979885aa5df48a.tar.gz
rails-a1945cb0b9844f3a43fe03d754979885aa5df48a.tar.bz2
rails-a1945cb0b9844f3a43fe03d754979885aa5df48a.zip
Merge pull request #8206 from kennyj/fix_7619-2
Fix #7619. 0x prefix must be added when assigning hexadecimal string into bit column in Postgresql, because solving ambiguity.
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/cast.rb6
-rw-r--r--activerecord/test/cases/adapters/postgresql/datatype_test.rb8
3 files changed, 15 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 61c217857d..52300fd53f 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* `0x` prefix must be added when assigning hexadecimal string into `bit` column in PostgreSQL.
+
+ *kennyj*
+
* Added Statement Cache to allow the caching of a single statement. The cache works by
duping the relation returned from yielding a statement, which allows skipping the AST
building phase for following executes. The cache returns results in array format.
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
index 5789a2ae5f..a9ef11aa83 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -28,8 +28,10 @@ module ActiveRecord
def string_to_bit(value)
case value
- when /^[01]*$/ then value # Bit-string notation
- when /^[0-9A-F]*$/i then value.hex.to_s(2) # Hexadecimal notation
+ when /^0x/i
+ value[2..-1].hex.to_s(2) # Hexadecimal notation
+ else
+ value # Bit-string notation
end
end
diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
index 13731beb01..8c17372286 100644
--- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -545,7 +545,7 @@ _SQL
def test_update_bit_string
new_bit_string = '11111111'
- new_bit_string_varying = 'FF'
+ new_bit_string_varying = '0xFF'
assert @first_bit_string.bit_string = new_bit_string
assert @first_bit_string.bit_string_varying = new_bit_string_varying
assert @first_bit_string.save
@@ -553,6 +553,12 @@ _SQL
assert_equal @first_bit_string.bit_string, new_bit_string
assert_equal @first_bit_string.bit_string, @first_bit_string.bit_string_varying
end
+
+ def test_invalid_hex_string
+ new_bit_string = 'FF'
+ @first_bit_string.bit_string = new_bit_string
+ assert_raise(ActiveRecord::StatementInvalid) { assert @first_bit_string.save }
+ end
def test_update_oid
new_value = 567890