Merge pull request #18602 from kamipo/respect_database_charset_and_collation

Respect the database default charset for `schema_migrations` table.
This commit is contained in:
Andrew White 2015-02-08 13:12:01 +00:00
commit 31fafe0a32
3 changed files with 21 additions and 5 deletions

@ -1,3 +1,11 @@
* Respect the database default charset for `schema_migrations` table.
The charset of `version` column in `schema_migrations` table is depend
on the database default charset and collation rather than the encoding
of the connection.
*Ryuta Kamizono*
* Raise `ArgumentError` when passing `nil` or `false` to `Relation#merge`.
These are not valid values to merge in a relation so it should warn the users

@ -39,7 +39,7 @@ def initialize(connection, logger, connection_options, config)
MAX_INDEX_LENGTH_FOR_UTF8MB4 = 191
def initialize_schema_migrations_table
if @config[:encoding] == 'utf8mb4'
if charset == 'utf8mb4'
ActiveRecord::SchemaMigration.create_table(MAX_INDEX_LENGTH_FOR_UTF8MB4)
else
ActiveRecord::SchemaMigration.create_table

@ -18,21 +18,29 @@ def test_initializes_schema_migrations_for_encoding_utf8mb4
smtn = ActiveRecord::Migrator.schema_migrations_table_name
connection.drop_table smtn, if_exists: true
config = connection.instance_variable_get(:@config)
original_encoding = config[:encoding]
database_name = connection.current_database
database_info = connection.select_one("SELECT * FROM information_schema.schemata WHERE schema_name = '#{database_name}'")
original_charset = database_info["DEFAULT_CHARACTER_SET_NAME"]
original_collation = database_info["DEFAULT_COLLATION_NAME"]
execute("ALTER DATABASE #{database_name} DEFAULT CHARACTER SET utf8mb4")
config[:encoding] = 'utf8mb4'
connection.initialize_schema_migrations_table
assert connection.column_exists?(smtn, :version, :string, limit: Mysql2Adapter::MAX_INDEX_LENGTH_FOR_UTF8MB4)
ensure
config[:encoding] = original_encoding
execute("ALTER DATABASE #{database_name} DEFAULT CHARACTER SET #{original_charset} COLLATE #{original_collation}")
end
private
def connection
@connection ||= ActiveRecord::Base.connection
end
def execute(sql)
connection.execute(sql)
end
end
end
end