1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

♻️ Fix mysql type map for enum and set

This commit is contained in:
bannzai 2019-01-07 23:36:31 +09:00
parent 9cfcc067e6
commit 45cfe9f8b6
2 changed files with 6 additions and 2 deletions

View file

@ -579,13 +579,13 @@ module ActiveRecord
m.alias_type %r(bit)i, "binary" m.alias_type %r(bit)i, "binary"
m.register_type(%r(enum)i) do |sql_type| m.register_type(%r(enum)i) do |sql_type|
limit = sql_type[/^enum\((.+)\)/i, 1] limit = sql_type[/^enum\s*\((.+)\)/i, 1]
.split(",").map { |enum| enum.strip.length - 2 }.max .split(",").map { |enum| enum.strip.length - 2 }.max
MysqlString.new(limit: limit) MysqlString.new(limit: limit)
end end
m.register_type(%r(^set)i) do |sql_type| m.register_type(%r(^set)i) do |sql_type|
limit = sql_type[/^set\((.+)\)/i, 1] limit = sql_type[/^set\s*\((.+)\)/i, 1]
.split(",").map { |set| set.strip.length - 1 }.sum - 1 .split(",").map { |set| set.strip.length - 1 }.sum - 1
MysqlString.new(limit: limit) MysqlString.new(limit: limit)
end end

View file

@ -27,8 +27,12 @@ if current_adapter?(:Mysql2Adapter)
def test_string_types def test_string_types
assert_lookup_type :string, "enum('one', 'two', 'three')" assert_lookup_type :string, "enum('one', 'two', 'three')"
assert_lookup_type :string, "ENUM('one', 'two', 'three')" assert_lookup_type :string, "ENUM('one', 'two', 'three')"
assert_lookup_type :string, "enum ('one', 'two', 'three')"
assert_lookup_type :string, "ENUM ('one', 'two', 'three')"
assert_lookup_type :string, "set('one', 'two', 'three')" assert_lookup_type :string, "set('one', 'two', 'three')"
assert_lookup_type :string, "SET('one', 'two', 'three')" assert_lookup_type :string, "SET('one', 'two', 'three')"
assert_lookup_type :string, "set ('one', 'two', 'three')"
assert_lookup_type :string, "SET ('one', 'two', 'three')"
end end
def test_set_type_with_value_matching_other_type def test_set_type_with_value_matching_other_type