Skip to content

Commit 5220c6e

Browse files
Regex did not match CREATE TABLE in all cases
The regular expression did not match CREATE TABLE statements printed out by AWS Aurora MySQL 5.6 instances, because they lack the required space at that position.
1 parent 35c5aa1 commit 5220c6e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,12 +412,13 @@ def table_options(table_name) # :nodoc:
412412
create_table_info = create_table_info(table_name)
413413

414414
# strip create_definitions and partition_options
415-
raw_table_options = create_table_info.sub(/\A.*\n\) /m, "").sub(/\n\/\*!.*\*\/\n\z/m, "").strip
415+
# Be aware that `create_table_info` might not include any table options due to `NO_TABLE_OPTIONS` sql mode.
416+
raw_table_options = create_table_info.sub(/\A.*\n\) ?/m, "").sub(/\n\/\*!.*\*\/\n\z/m, "").strip
416417

417418
# strip AUTO_INCREMENT
418419
raw_table_options.sub!(/(ENGINE=\w+)(?: AUTO_INCREMENT=\d+)/, '\1')
419420

420-
table_options[:options] = raw_table_options
421+
table_options[:options] = raw_table_options unless raw_table_options.blank?
421422

422423
# strip COMMENT
423424
if raw_table_options.sub!(/ COMMENT='.+'/, "")

activerecord/test/cases/adapters/mysql2/table_options_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ def teardown
4141
options = %r{create_table "mysql_table_options", options: "(?<options>.*)"}.match(output)[:options]
4242
assert_match %r{COLLATE=utf8mb4_bin}, options
4343
end
44+
45+
test "schema dump works with NO_TABLE_OPTIONS sql mode" do
46+
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"
47+
48+
old_sql_mode = @connection.exec_query("SELECT @@session.sql_mode").first["@@session.sql_mode"]
49+
new_sql_mode = old_sql_mode.split(",") + ["NO_TABLE_OPTIONS"]
50+
51+
begin
52+
@connection.execute("SET @@session.sql_mode=\"#{new_sql_mode.join(",")}\"")
53+
54+
@connection.create_table "mysql_table_options", force: true
55+
output = dump_table_schema("mysql_table_options")
56+
assert_no_match %r{options:}, output
57+
rescue
58+
assert(false, "Changing sql mode failed")
59+
ensure
60+
@connection.execute("SET @@session.sql_mode=\"#{old_sql_mode}\"")
61+
end
62+
end
4463
end
4564

4665
class Mysql2DefaultEngineOptionSchemaDumpTest < ActiveRecord::Mysql2TestCase

0 commit comments

Comments
 (0)