Skip to content

Commit

Permalink
Add collation and config check
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaursu committed Feb 5, 2015
1 parent c61a4a9 commit 0829e6f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ series and run our comprehensive test suite against it.
The default for including jQuery from a CDN has changed. If you want to continue to include it from a CDN, please explicitly set the `jquery_cdn` setting to `true` in diaspora.yml.

## Change in database.yml
For MySQL databases, replace `charset: utf8` with `encoding: utf8mb4` in the file `config/database.yml`. This is enables full UTF8 support (4bytes characters), including standard emoji characters. See `database.yml.example` for reference.
Also, do not forget to remove `collation: utf8_bin`. It will choose a compatible one automatically.
For MySQL databases, replace `charset: utf8` with `encoding: utf8mb4m` and change `collation` from `utf8_bin` to `utf8mb4_bin` in the file `config/database.yml`.
This is enables full UTF8 support (4bytes characters), including standard emoji characters.
See `database.yml.example` for reference.
Please make sure to stop Diaspora prior running this migration!

## Experimental chat feature
Expand Down
1 change: 1 addition & 0 deletions config/database.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mysql: &mysql
password: ""
# socket: /tmp/mysql.sock
encoding: utf8mb4
collation: utf8mb4_bin

postgres: &postgres
adapter: postgresql
Expand Down
21 changes: 15 additions & 6 deletions db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ class SetMysqlToUnicodeMb4 < ActiveRecord::Migration
def self.up
# shorten indexes regardless of the RDBMS provider - for consitency
shorten_indexes
change_encoding('utf8mb4') if AppConfig.mysql?
change_encoding('utf8mb4', 'utf8mb4_bin') if AppConfig.mysql?
end

def self.down
change_encoding('utf8') if AppConfig.mysql?
change_encoding('utf8', 'utf8_bin') if AppConfig.mysql?
end

def change_encoding(encoding)
execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET #{encoding};"
def check_config(encoding, collation)
connection_config = ActiveRecord::Base.connection_config
fail "Database encoding is not #{encoding}!" if connection_config[:encoding] != encoding
fail "Database collation is not #{collation}!" if connection_config[:collation] != collation
end

def change_encoding(encoding, collation)
# Make sure the podmin changed the database.yml file
check_config(encoding, collation)

execute "ALTER DATABASE `#{ActiveRecord::Base.connection.current_database}` CHARACTER SET #{encoding} COLLATE #{collation};"

tables.each do |table|
execute "ALTER TABLE `#{table}` CHARACTER SET = #{encoding}"
execute "ALTER TABLE `#{table}` CHARACTER SET = #{encoding} COLLATE #{collation}"
end

character_columns.each do |table, columns|
columns.each do |column|
execute "ALTER TABLE `#{table}` CHANGE `#{column.name}` `#{column.name}` #{column.sql_type} CHARACTER SET #{encoding} #{column.null ? 'NULL' : 'NOT NULL'} #{"DEFAULT '#{column.default}'" if column.has_default?};"
execute "ALTER TABLE `#{table}` CHANGE `#{column.name}` `#{column.name}` #{column.sql_type} CHARACTER SET #{encoding} COLLATE #{collation} #{column.null ? 'NULL' : 'NOT NULL'} #{"DEFAULT '#{column.default}'" if column.has_default?};"
end
end
end
Expand Down

0 comments on commit 0829e6f

Please sign in to comment.