Created
March 1, 2016 17:31
-
-
Save paulmars/d131b3c687c4d3f736db to your computer and use it in GitHub Desktop.
change encoding for a mysql table/db in rails
This file contains hidden or 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
| def change_encoding(encoding,collation) | |
| connection = ActiveRecord::Base.connection | |
| tables = connection.tables | |
| dbname = connection.current_database | |
| connection.execute <<-SQL | |
| ALTER DATABASE #{dbname} CHARACTER SET #{encoding} COLLATE #{collation}; | |
| SQL | |
| tables.each do |tablename| | |
| connection.execute <<-SQL | |
| ALTER TABLE #{dbname}.#{tablename} CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation}; | |
| SQL | |
| end | |
| end | |
| change_encoding('utf8','utf8_general_ci') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment