diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2014-11-10 05:19:45 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2015-05-03 16:41:19 +0900 |
commit | 22ba159efcf85d66ff086d0f760d22c239607b77 (patch) | |
tree | 93be8062807396705e69eaad8ee3fb5307681d27 /activerecord/lib/active_record | |
parent | 76836ef7db8d0b1e40492c9a62f7c637718d813d (diff) | |
download | rails-22ba159efcf85d66ff086d0f760d22c239607b77.tar.gz rails-22ba159efcf85d66ff086d0f760d22c239607b77.tar.bz2 rails-22ba159efcf85d66ff086d0f760d22c239607b77.zip |
Correctly dump `:options` on `create_table` for MySQL
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index c4a37f8538..654ed0250e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -14,6 +14,10 @@ module ActiveRecord {} end + def table_options(table_name) + nil + end + # Truncates a table alias according to the limits of the current adapter. def table_alias_for(table_name) table_name[0...table_alias_length].tr('.', '_') 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 76aee452ca..75c81cadfa 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -686,6 +686,16 @@ module ActiveRecord end end + def table_options(table_name) + create_table_info = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")["Create Table"] + + # strip create_definitions and partition_options + raw_table_options = create_table_info.sub(/\A.*\n\) /m, '').sub(/\n\/\*!.*\*\/\n\z/m, '').strip + + # strip AUTO_INCREMENT + raw_table_options.sub(/(ENGINE=\w+)(?: AUTO_INCREMENT=\d+)/, '\1') + end + # Maps logical Rails types to MySQL-specific data types. def type_to_sql(type, limit = nil, precision = nil, scale = nil) case type.to_s diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index eaeaf0321b..321611d6c9 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -131,6 +131,10 @@ HEADER tbl.print ", id: false" end tbl.print ", force: :cascade" + + table_options = @connection.table_options(table) + tbl.print ", options: #{table_options.inspect}" unless table_options.blank? + tbl.puts " do |t|" # then dump all non-primary key columns |