File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed
lib/active_record/connection_adapters
test/cases/adapters/mysql2 Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff 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='.+'/ , "" )
Original file line number Diff line number Diff 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
4463end
4564
4665class Mysql2DefaultEngineOptionSchemaDumpTest < ActiveRecord ::Mysql2TestCase
You can’t perform that action at this time.
0 commit comments