Sha256: 47226465828ad62426d231c7b66180097531c81c2076f9529ed607c5c914c600

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

class ChangeCollation < ActiveRecord::Migration[4.2]
  MYSQL_COLLATION_SUPPORT_VERSION = 8

  def up
    change_database_encoding('utf8mb4', 'utf8mb4_0900_as_ci') if check_supporting_mysql_version
  end

  def down
    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
    tables = connection.tables

    execute <<-SQL
      ALTER DATABASE #{database} CHARACTER SET #{encoding} COLLATE #{collation};
      SQL

    tables.each do |table|
      execute <<-SQL
        ALTER TABLE #{database}.#{table} CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation};
        SQL
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
iqvoc-4.14.5 db/migrate/20190403133600_change_collation.rb
iqvoc-4.14.4 db/migrate/20190403133600_change_collation.rb
iqvoc-4.13.2 db/migrate/20190403133600_change_collation.rb
iqvoc-4.13.0 db/migrate/20190403133600_change_collation.rb