提交 949fd758 编写于 作者: R Ryuta Kamizono

Merge pull request #36604 from kamipo/fix_schema_dumping_enum

MySQL: Fix schema dumping `enum` and `set` columns correctly
上级 ef4379b2
......@@ -41,13 +41,15 @@ def schema_type(column)
case column.sql_type
when /\Atimestamp\b/
:timestamp
when /\A(?:enum|set)\b/
column.sql_type
else
super
end
end
def schema_limit(column)
super unless /\A(?:tiny|medium|long)?(?:text|blob)/.match?(column.sql_type)
super unless /\A(?:enum|set|(?:tiny|medium|long)?(?:text|blob))\b/.match?(column.sql_type)
end
def schema_precision(column)
......
......@@ -146,7 +146,11 @@ def table(table, stream)
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
next if column.name == pk
type, colspec = column_spec(column)
tbl.print " t.#{type} #{column.name.inspect}"
if type.is_a?(Symbol)
tbl.print " t.#{type} #{column.name.inspect}"
else
tbl.print " t.column #{column.name.inspect}, #{type.inspect}"
end
tbl.print ", #{format_colspec(colspec)}" if colspec.present?
tbl.puts
end
......
# frozen_string_literal: true
require "cases/helper"
require "support/schema_dumping_helper"
class Mysql2EnumTest < ActiveRecord::Mysql2TestCase
include SchemaDumpingHelper
class EnumTest < ActiveRecord::Base
end
def setup
EnumTest.connection.create_table :enum_tests, id: false, force: true do |t|
t.column :enum_column, "enum('text','blob','tiny','medium','long','unsigned','bigint')"
end
end
def test_enum_limit
column = EnumTest.columns_hash["enum_column"]
assert_equal 8, column.limit
......@@ -20,4 +29,9 @@ def test_should_not_be_bigint
column = EnumTest.columns_hash["enum_column"]
assert_not_predicate column, :bigint?
end
def test_schema_dumping
schema = dump_table_schema "enum_tests"
assert_match %r{t\.column "enum_column", "enum\('text','blob','tiny','medium','long','unsigned','bigint'\)"$}, schema
end
end
# frozen_string_literal: true
require "cases/helper"
require "support/schema_dumping_helper"
class Mysql2SetTest < ActiveRecord::Mysql2TestCase
include SchemaDumpingHelper
class SetTest < ActiveRecord::Base
end
def setup
SetTest.connection.create_table :set_tests, id: false, force: true do |t|
t.column :set_column, "set('text','blob','tiny','medium','long','unsigned','bigint')"
end
end
def test_should_not_be_unsigned
column = SetTest.columns_hash["set_column"]
assert_not_predicate column, :unsigned?
end
def test_should_not_be_bigint
column = SetTest.columns_hash["set_column"]
assert_not_predicate column, :bigint?
end
def test_schema_dumping
schema = dump_table_schema "set_tests"
assert_match %r{t\.column "set_column", "set\('text','blob','tiny','medium','long','unsigned','bigint'\)"$}, schema
end
end
......@@ -40,7 +40,7 @@ def test_set_type_with_value_matching_other_type
end
def test_enum_type_with_value_matching_other_type
assert_lookup_type :string, "ENUM('unicode', '8bit', 'none')"
assert_lookup_type :string, "ENUM('unicode', '8bit', 'none', 'time')"
end
def test_binary_types
......
......@@ -62,10 +62,6 @@
t.binary :binary_column, limit: 1
end
create_table :enum_tests, id: false, force: true do |t|
t.column :enum_column, "ENUM('text','blob','tiny','medium','long','unsigned','bigint')"
end
execute "DROP PROCEDURE IF EXISTS ten"
execute <<~SQL
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册