From 0829e6f394e98145ee64c9e559c5368e61d58fb1 Mon Sep 17 00:00:00 2001 From: Dumitru Ursu Date: Fri, 6 Feb 2015 00:18:20 +0200 Subject: [PATCH] Add collation and config check --- Changelog.md | 5 +++-- config/database.yml.example | 1 + ...20150106050733_set_mysql_to_unicode_mb4.rb | 21 +++++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Changelog.md b/Changelog.md index 441983267..e89b2b318 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/config/database.yml.example b/config/database.yml.example index e62f7a9a6..f5a1e08cd 100644 --- a/config/database.yml.example +++ b/config/database.yml.example @@ -6,6 +6,7 @@ mysql: &mysql password: "" # socket: /tmp/mysql.sock encoding: utf8mb4 + collation: utf8mb4_bin postgres: &postgres adapter: postgresql diff --git a/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb b/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb index c8866bc5e..faf4ce870 100644 --- a/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb +++ b/db/migrate/20150106050733_set_mysql_to_unicode_mb4.rb @@ -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