提交 8246b593 编写于 作者: S Sean Griffin

Allow specifying the default table options for mysql adapters

It's often the case that you want to have an option that you cannot
specify at the database level, but want applied to *all* tables that you
create. For example, you might want to specify `ROW_FORMAT=DYNAMIC` to
not have to limit text columns to length 171 for indexing when using
utf8mb4. This allows an easy way to specify this in your database
configuration.

While this change affects both MySQL and MySQL2, the test only covers
MySQL2, as the legacy mysql adapter appears to always return ASCII
strings, and is tangential to what we're actually doing.
上级 7e200f29
* Allow specifying the default options for new tables on mysql and mysql2 in
database.yml, via the key `default_table_options`.
*Sean Griffin*
* Except keys of `build_record`'s argument from `create_scope` in `initialize_attributes`.
Fixes #21893.
......
......@@ -597,7 +597,8 @@ def columns(table_name)#:nodoc:
end
def create_table(table_name, options = {}) #:nodoc:
super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
default_table_options = @config.fetch(:default_table_options, "ENGINE=InnoDB")
super(table_name, options.reverse_merge(options: default_table_options))
end
def bulk_change_table(table_name, operations) #:nodoc:
......
......@@ -1020,4 +1020,23 @@ def test_check_pending_with_stdlib_logger
ActiveRecord::Base.logger = old
end
if current_adapter?(:Mysql2Adapter)
def test_default_table_options
config = ActiveRecord::Base.configurations['arunit'].merge(
encoding: 'utf8mb4',
default_table_options: "ENGINE=InnoDB CHARACTER SET utf8mb4",
)
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.create_table(:foos) do |t|
t.string :emoji
end
ActiveRecord::Base.connection.execute("INSERT INTO foos (emoji) VALUES ('💩')")
emoji = ActiveRecord::Base.connection.execute("SELECT emoji FROM foos").first.first
assert_equal "💩", emoji
ensure
ActiveRecord::Base.connection.drop_table(:foos, if_exists: true)
ActiveRecord::Base.establish_connection(:arunit)
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册