Skip to content

Commit

Permalink
Merge pull request #37991 from UlrichThomasGabor/patch-1
Browse files Browse the repository at this point in the history
Regex did not match CREATE TABLE in all cases
  • Loading branch information
kamipo committed Dec 23, 2019
1 parent ba63c47 commit f096acb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,13 @@ def table_options(table_name) # :nodoc:
create_table_info = create_table_info(table_name)

# strip create_definitions and partition_options
raw_table_options = create_table_info.sub(/\A.*\n\) /m, "").sub(/\n\/\*!.*\*\/\n\z/m, "").strip
# Be aware that `create_table_info` might not include any table options due to `NO_TABLE_OPTIONS` sql mode.
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')

table_options[:options] = raw_table_options
table_options[:options] = raw_table_options unless raw_table_options.blank?

# strip COMMENT
if raw_table_options.sub!(/ COMMENT='.+'/, "")
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/adapters/mysql2/table_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ def teardown
options = %r{create_table "mysql_table_options", options: "(?<options>.*)"}.match(output)[:options]
assert_match %r{COLLATE=utf8mb4_bin}, options
end

test "schema dump works with NO_TABLE_OPTIONS sql mode" do
skip "As of MySQL 5.7.22, NO_TABLE_OPTIONS is deprecated. It will be removed in a future version of MySQL." if @connection.database_version >= "5.7.22"

old_sql_mode = @connection.query_value("SELECT @@SESSION.sql_mode")
new_sql_mode = old_sql_mode + ",NO_TABLE_OPTIONS"

begin
@connection.execute("SET @@SESSION.sql_mode='#{new_sql_mode}'")

@connection.create_table "mysql_table_options", force: true
output = dump_table_schema("mysql_table_options")
assert_no_match %r{options:}, output
ensure
@connection.execute("SET @@SESSION.sql_mode='#{old_sql_mode}'")
end
end
end

class Mysql2DefaultEngineOptionSchemaDumpTest < ActiveRecord::Mysql2TestCase
Expand Down

0 comments on commit f096acb

Please sign in to comment.