aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Daer <jeremydaer@gmail.com>2015-09-19 16:05:34 -0700
committerJeremy Daer <jeremydaer@gmail.com>2015-09-19 16:05:34 -0700
commit5837de5360b9a38ca5b07f26b1d03b1ce4763f17 (patch)
tree03375e471ed6e3568c86a93d963456585c1d2760 /activerecord
parentac2b312219faf67d08a4fb3fd622ecb4c9bc3623 (diff)
parent206cf5193808b41c45fd287e5134dffa961a88de (diff)
downloadrails-5837de5360b9a38ca5b07f26b1d03b1ce4763f17.tar.gz
rails-5837de5360b9a38ca5b07f26b1d03b1ce4763f17.tar.bz2
rails-5837de5360b9a38ca5b07f26b1d03b1ce4763f17.zip
Merge pull request #20645 from kamipo/fix_mysql_set_type_bug
Fix undesired type lookup with `SET` in MySQL
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb7
-rw-r--r--activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb4
2 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index f0004e398e..0564bfaf8e 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -814,7 +814,6 @@ module ActiveRecord
register_integer_type m, %r(^tinyint)i, limit: 1
m.alias_type %r(tinyint\(1\))i, 'boolean' if emulate_booleans
- m.alias_type %r(set)i, 'varchar'
m.alias_type %r(year)i, 'integer'
m.alias_type %r(bit)i, 'binary'
@@ -823,6 +822,12 @@ module ActiveRecord
.split(',').map{|enum| enum.strip.length - 2}.max
MysqlString.new(limit: limit)
end
+
+ m.register_type(%r(^set)i) do |sql_type|
+ limit = sql_type[/^set\((.+)\)/i, 1]
+ .split(',').map{|set| set.strip.length - 1}.sum - 1
+ MysqlString.new(limit: limit)
+ end
end
def register_integer_type(mapping, key, options) # :nodoc:
diff --git a/activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb b/activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb
index 80244d1439..2749273884 100644
--- a/activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb
+++ b/activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb
@@ -22,6 +22,10 @@ module ActiveRecord
assert_lookup_type :string, "SET('one', 'two', 'three')"
end
+ def test_set_type_with_value_matching_other_type
+ assert_lookup_type :string, "SET('unicode', '8bit', 'none', 'time')"
+ end
+
def test_enum_type_with_value_matching_other_type
assert_lookup_type :string, "ENUM('unicode', '8bit', 'none')"
end