Created
October 25, 2013 11:54
-
-
Save richhollis/7153490 to your computer and use it in GitHub Desktop.
Ruby script to convert Rails Latin1 / latin1_swedish_ci database tables to UTF8 / utf8_unicode_ci. See: http://stackoverflow.com/questions/9479607/rails-show-question-marks-for-my-input-utf8-data http://stackoverflow.com/questions/13999026/utf-8-data-in-latin1-database-can-it-be-saved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tables = [] # or ActiveRecord::Base.connection.tables | |
collation = "utf8_unicode_ci" | |
char_set = "utf8" | |
db = "your_database_name" | |
# write out | |
puts "USE #{db};" | |
puts "ALTER DATABASE #{db} CHARACTER SET #{char_set} COLLATE #{collation};" | |
tables.each do |t| | |
puts "ALTER TABLE #{t} CHARACTER SET #{char_set} COLLATE #{collation};" # changes for new records | |
puts "ALTER TABLE #{t} CONVERT TO CHARACTER SET #{char_set} COLLATE #{collation};" # migrates old records | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment