Skip to content

Commit

Permalink
fix tests by checking if mysql version supports new collation
Browse files Browse the repository at this point in the history
  • Loading branch information
philippneugebauer committed Apr 16, 2019
1 parent 8927a73 commit c4623d7
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions db/migrate/20190403133600_change_collation.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
class ChangeCollation < ActiveRecord::Migration
MYSQL_COLLATION_SUPPORT_VERSION = 8

def up
if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
change_database_encoding('utf8mb4', 'utf8mb4_0900_as_ci')
end
change_database_encoding('utf8mb4', 'utf8mb4_0900_as_ci') if check_supporting_mysql_version
end

def down
if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
change_database_encoding('utf8', 'utf8mb4_general_ci')
end
change_database_encoding('utf8', 'utf8mb4_general_ci') if check_supporting_mysql_version
end

private
def check_supporting_mysql_version
connection = ActiveRecord::Base.connection
if connection.adapter_name == 'Mysql2'
version_row = connection.select_rows("SHOW VARIABLES WHERE variable_name = 'version'").try(:first)
raise "There is a problem of our code with your MySQL version, please report in GitHub Repository" if version_row.first != "version"
version_row.last.first.to_i >= MYSQL_COLLATION_SUPPORT_VERSION
else
false
end
end

def change_database_encoding(encoding, collation)
connection = ActiveRecord::Base.connection
database = connection.current_database
Expand Down

0 comments on commit c4623d7

Please sign in to comment.