themactep.com

A home of miscellaneous projects by Paul Philippov.

Notes

How to convert DB of an older Rails project to full Unicode

Adjust encoding in database.yml as following:

  encoding: utf8mb4
  collation: utf8mb4_polish_ci

Create and run the following migration:

class ConvertToFullUnicode < ActiveRecord::Migration[5.0]
  def change
    config = Rails.configuration.database_configuration
    db_name = config[Rails.env]["database"]
    collate = 'utf8mb4_unicode_ci'
    char_set = 'utf8mb4'
    row_format = 'DYNAMIC'

    execute("ALTER DATABASE #{db_name} CHARACTER SET #{char_set} COLLATE #{collate};")
 
    ActiveRecord::Base.connection.tables.each do |table|
      execute("ALTER TABLE #{table} ROW_FORMAT=#{row_format};")
      execute("ALTER TABLE #{table} CONVERT TO CHARACTER SET #{char_set} COLLATE #{collate};")
    end
  end
end

(Based on this post.)