Skip to content

Commit

Permalink
Merge pull request #18602 from kamipo/respect_database_charset_and_co…
Browse files Browse the repository at this point in the history
…llation

Respect the database default charset for `schema_migrations` table.
  • Loading branch information
pixeltrix committed Feb 8, 2015
2 parents b1e6794 + 0bbff5e commit 31fafe0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions activerecord/CHANGELOG.md
@@ -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
Expand Down
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions activerecord/test/cases/adapters/mysql2/schema_migrations_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 31fafe0

Please sign in to comment.