20161103160230_convert_tables_to_utf8.rb 653 B

123456789101112131415161718192021222324252627
  1. class ConvertTablesToUtf8 < ActiveRecord::Migration
  2. def change_encoding(encoding,collation)
  3. connection = ActiveRecord::Base.connection
  4. tables = connection.tables
  5. dbname = connection.current_database
  6. execute <<-SQL
  7. ALTER DATABASE #{dbname} CHARACTER SET #{encoding} COLLATE #{collation};
  8. SQL
  9. tables.each do |tablename|
  10. execute <<-SQL
  11. ALTER TABLE #{dbname}.#{tablename} CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation};
  12. SQL
  13. end
  14. end
  15. def change
  16. reversible do |dir|
  17. dir.up do
  18. change_encoding('utf8','utf8_general_ci')
  19. end
  20. dir.down do
  21. change_encoding('latin1','latin1_swedish_ci')
  22. end
  23. end
  24. end
  25. end