New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set charset/collation properly for each text column if using MySQL. #414
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d1caa4
Merge remote-tracking branch 'origin/dsander-omniauth'
knu db792cd
Set charset/collation properly for each text column if using MySQL.
knu 59482ee
Add support for Ruby 1.9.x.
knu 66d1ac0
Fall back utf8mb4 to utf8 if the server does not support it.
knu 393e3c4
Follow the default collation of Rails rather than MySQL.
knu 857b8ea
Merge remote-tracking branch 'origin/master' into set_charset_for_mysql
knu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ActiveSupport.on_load :active_record do | ||
require 'ar_mysql_column_charset' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class SetCharsetForMysql < ActiveRecord::Migration | ||
def all_models | ||
@all_models ||= [ | ||
Agent, | ||
AgentLog, | ||
Contact, | ||
Event, | ||
Link, | ||
Scenario, | ||
ScenarioMembership, | ||
User, | ||
UserCredential, | ||
Delayed::Job, | ||
] | ||
end | ||
|
||
def change | ||
conn = ActiveRecord::Base.connection | ||
|
||
# This is migration is for MySQL only. | ||
return unless conn.is_a?(ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter) | ||
|
||
reversible do |dir| | ||
dir.up do | ||
all_models.each { |model| | ||
table_name = model.table_name | ||
|
||
# `contacts` may not exist | ||
next unless connection.table_exists? table_name | ||
|
||
model.columns.each { |column| | ||
name = column.name | ||
type = column.type | ||
limit = column.limit | ||
options = { | ||
limit: limit, | ||
null: column.null, | ||
default: column.default, | ||
} | ||
|
||
case type | ||
when :string, :text | ||
options.update(charset: 'utf8', collation: 'utf8_unicode_ci') | ||
case name | ||
when 'username' | ||
options.update(limit: 767 / 4, charset: 'utf8mb4', collation: 'utf8mb4_unicode_ci') | ||
when 'message', 'options', 'name', 'memory', | ||
'handler', 'last_error', 'payload', 'description' | ||
options.update(charset: 'utf8mb4', collation: 'utf8mb4_bin') | ||
when 'type', 'schedule', 'mode', 'email', | ||
'invitation_code', 'reset_password_token' | ||
options.update(collation: 'utf8_bin') | ||
when 'guid', 'encrypted_password' | ||
options.update(charset: 'ascii', collation: 'ascii_bin') | ||
end | ||
else | ||
next | ||
end | ||
|
||
change_column table_name, name, type, options | ||
} | ||
|
||
execute 'ALTER TABLE %s CHARACTER SET utf8 COLLATE utf8_unicode_ci' % table_name | ||
} | ||
|
||
execute 'ALTER DATABASE %s CHARACTER SET utf8 COLLATE utf8_unicode_ci' % conn.current_database | ||
end | ||
|
||
dir.down do | ||
# Do nada; no use to go back | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require 'active_record' | ||
|
||
# Module#prepend support for Ruby 1.9 | ||
require 'prepend' unless Module.method_defined?(:prepend) | ||
|
||
module ActiveRecord::ConnectionAdapters | ||
class ColumnDefinition | ||
module CharsetSupport | ||
attr_accessor :charset, :collation | ||
end | ||
|
||
prepend CharsetSupport | ||
end | ||
|
||
class TableDefinition | ||
module CharsetSupport | ||
def new_column_definition(name, type, options) | ||
column = super | ||
column.charset = options[:charset] | ||
column.collation = options[:collation] | ||
column | ||
end | ||
end | ||
|
||
prepend CharsetSupport | ||
end | ||
|
||
class AbstractMysqlAdapter | ||
module CharsetSupport | ||
def prepare_column_options(column, types) | ||
spec = super | ||
conn = ActiveRecord::Base.connection | ||
spec[:charset] = column.charset.inspect if column.charset && column.charset != conn.charset | ||
spec[:collation] = column.collation.inspect if column.collation && column.collation != conn.collation | ||
spec | ||
end | ||
|
||
def migration_keys | ||
super + [:charset, :collation] | ||
end | ||
|
||
def utf8mb4_supported? | ||
if @utf8mb4_supported.nil? | ||
@utf8mb4_supported = !select("show character set like 'utf8mb4'").empty? | ||
else | ||
@utf8mb4_supported | ||
end | ||
end | ||
|
||
def charset_collation(charset, collation) | ||
[charset, collation].map { |name| | ||
case name | ||
when nil | ||
nil | ||
when /\A(utf8mb4(_\w*)?)\z/ | ||
if utf8mb4_supported? | ||
$1 | ||
else | ||
"utf8#{$2}" | ||
end | ||
else | ||
name.to_s | ||
end | ||
} | ||
end | ||
end | ||
|
||
prepend CharsetSupport | ||
|
||
class SchemaCreation | ||
module CharsetSupport | ||
def column_options(o) | ||
column_options = super | ||
column_options[:charset] = o.charset unless o.charset.nil? | ||
column_options[:collation] = o.collation unless o.collation.nil? | ||
column_options | ||
end | ||
|
||
def add_column_options!(sql, options) | ||
charset, collation = @conn.charset_collation(options[:charset], options[:collation]) | ||
|
||
if charset | ||
sql << " CHARACTER SET #{charset}" | ||
end | ||
|
||
if collation | ||
sql << " COLLATE #{collation}" | ||
end | ||
|
||
super | ||
end | ||
end | ||
|
||
prepend CharsetSupport | ||
end | ||
|
||
class Column | ||
module CharsetSupport | ||
attr_reader :charset | ||
|
||
def initialize(*args) | ||
super | ||
@charset = @collation[/\A[^_]+/] unless @collation.nil? | ||
end | ||
end | ||
|
||
prepend CharsetSupport | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth adding some comments at the top of this explaining what it's for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!