{"id":95,"date":"2006-11-06T15:44:44","date_gmt":"2006-11-06T13:44:44","guid":{"rendered":"http:\/\/www.dotmana.com\/index.php\/?p=95"},"modified":"2006-11-06T16:11:13","modified_gmt":"2006-11-06T14:11:13","slug":"ruby-on-rails-utf8-mysql","status":"publish","type":"post","link":"http:\/\/www.dotmana.com\/weblog\/2006\/11\/ruby-on-rails-utf8-mysql\/","title":{"rendered":"Ruby On Rails + UTF8 + Mysql"},"content":{"rendered":"<p>Voici plusieurs tutoriaux complets permettant de faire fonctionner la cha\u00eene de liaison depuis le navigateur jusqu&#8217;\u00e0 Mysql en passant par rails.<\/p>\n<p><!--more--><\/p>\n<p>Le premier :<\/p>\n<div class=\"entry\">\n<p style=\"margin-left: 40px\">In a post about <a title=\"infundibulum \u00bb Ruby and Unicode\" href=\"http:\/\/ruphus.com\/blog\/2005\/06\/11\/ruby-and-unicode\/\">Ruby and Unicode<\/a> a while back, I mentioned the page at the Rails wiki called <a title=\"How To Use Unicode Strings in Rails\" href=\"http:\/\/wiki.rubyonrails.com\/rails\/show\/HowToUseUnicodeStrings\">How To Use Unicode Strings in Rails<\/a>. (Btw, check out <a title=\"RedHanded \u00bb Closing in on Unicode with Jcode\" href=\"http:\/\/redhanded.hobix.com\/inspect\/closingInOnUnicodeWithJcode.html\">Why the Lucky Stiff\u2019s response to my post, some useful code there<\/a>.)<\/p>\n<p style=\"margin-left: 40px\">It turns out that there were <span style=\"background: #ffffcc none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial\">a few more mostly MySQL-specific steps involved in getting Unicode to work correctly with Rails<\/span>. So I thought I\u2019d describe all the steps <a title=\"Jonas Galvez\" href=\"http:\/\/www.jonasgalvez.com\/\">we<\/a> went through to get it set up in one place. This has only been tested with MySQL 4.1.<\/p>\n<h3 style=\"margin-left: 40px\">In MySQL: Set the Encoding when you Create Tables<\/h3>\n<p style=\"margin-left: 40px\">You need to explicity tell MySQL that you want your tables to be encoded in UTF-8. Here\u2019s a sample table with 3 columns, <code>id<\/code>, <code>foo<\/code>, and <code>bar<\/code>:<\/p>\n<pre style=\"margin-left: 40px\">create table samples (\r\nid int not null auto_increment,\r\nfoo varchar(100) not null,\r\nbar text not null,\r\nprimary key (id)\r\n) Type=MyISAM CHARACTER SET utf8;<\/pre>\n<p style=\"margin-left: 40px\">The line <span style=\"background: #ffffcc none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial\"><code>Type=MyISAM CHARACTER SET utf8;<\/code><\/span> is where the action is. The table type has to be MyISAM, not innoDB, because unfortunately innoDB tables don\u2019t support full text searching of UTF-8 encoded content (<a title=\"MySQL Reference Manual :: 12.7.3 Full-Text Restrictions\" href=\"http:\/\/dev.mysql.com\/doc\/mysql\/en\/fulltext-restrictions.html\">details here<\/a>). Apparently <a title=\"Linux Magazine - January 2003 | Feature Story | MySQL 4.x:\" href=\"http:\/\/www.linux-mag.com\/content\/view\/1243\/2203\/\">innoDB tables are more flexible in general<\/a>, but if full-text search is crucial for you, you\u2019ll have to go with MyISAM.<\/p>\n<p style=\"margin-left: 40px\">Bummer, that.<\/p>\n<p style=\"margin-left: 40px\">In any case, then <span style=\"background: #ffffcc none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial\">add the <code>CHARACTER SET utf8<\/code><\/span> directive, as shown.<\/p>\n<p style=\"margin-left: 40px\">(I wonder if there is some way to set this as a default, without adding the line to the <acronym title=\"Data Definition Language\">DDL<\/acronym> for every table?)<\/p>\n<h3 style=\"margin-left: 40px\">Set the \u201ccharset\u201d and \u201cContent-type\u201d  in the Application Controller<\/h3>\n<p style=\"margin-left: 40px\">This is also described at <a title=\"How To Use Unicode Strings in Rails\" href=\"http:\/\/wiki.rubyonrails.com\/rails\/show\/HowToUseUnicodeStrings\">How To Use Unicode Strings in Rails<\/a>.<\/p>\n<pre style=\"margin-left: 40px\">class ApplicationController < ActionController::Base\r\nbefore_filter :set_charset\r\n\r\ndef set_charset\r\n@headers[\"Content-Type\"] = \"text\/html; charset=utf-8\"\r\nend\r\nend<\/pre>\n<p style=\"margin-left: 40px\">The previous steps were enough to get Unicode showing up in a little test app I generated with <code>scaffold<\/code> (the app just consisted of an input field and a textarea).<\/p>\n<p style=\"margin-left: 40px\">To test it, I pasted in some <a title=\"What is Unicode?\" href=\"http:\/\/www.unicode.org\/standard\/WhatIsUnicode.html\">sample text in various languages<\/a>. It worked okay for text containing only the characters found in ASCII or latin1, but among other characters there were weird cases of random characters being removed or added. The problem seemed not to have anything to do with the script (i.e., the Unicode block). For instance, in an Esperanto text, \u201c\u0109\u201d (U+0109 LATIN SMALL LETTER C WITH CIRCUMFLEX) came out fine, but \u201c\u011d\u201d (U+011D LATIN SMALL LETTER G WITH CIRCUMFLEX) was borked. Go figure.<\/p>\n<p style=\"margin-left: 40px\">It took some digging to find the next bit \u2014 thanks to Ben Jackson of <a title=\"INCOMUM Design &#038; Conceito \/ Rio de Janeiro\" href=\"http:\/\/www.incomumdesign.com\/\">INCOMUM Design & Conceito<\/a> for <a title=\"#1387 (Allow configuration option for mySQL SET NAMES 'utf8') - Ruby on Rails - Trac\" href=\"http:\/\/dev.rubyonrails.com\/ticket\/1387\">getting the straight story on the Rails list<\/a>. The solution is\u2026<\/p>\n<h3 style=\"margin-left: 40px\">Tell Rails to tell MySQL to Use UTF-8. (Got that?)<\/h3>\n<p style=\"margin-left: 40px\">It came down to a MySQL configuration option: You have to <span style=\"background: #ffffcc none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial\">tell MySQL to <code>SET NAMES UTF8<\/code><\/span>, as <a title=\"Loud Thinking: Broadcasting Brain\" href=\"http:\/\/www.loudthinking.com\/\">DHH<\/a> pointed out in the previous link. You can either do it in the source to ActiveRecord, in <code>mysql_adapter.rb<\/code>, or you can just make the change in your own application. We chose the latter route.<\/p>\n<p style=\"margin-left: 40px\">So, here\u2019s our <code>app\/controllers\/application.rb<\/code> as it stands:<\/p>\n<pre style=\"margin-left: 40px\">class ApplicationController < ActionController::Base\r\nbefore_filter :set_charset\r\nbefore_filter :configure_charsets\r\n\r\ndef set_charset\r\n@headers[\"Content-Type\"] = \"text\/html; charset=utf-8\"\r\nend\r\n\r\ndef configure_charsets\r\n@response.headers[\"Content-Type\"] = \"text\/html; charset=utf-8\"\r\n# Set connection charset. MySQL 4.0 doesn't support this so it\r\n# will throw an error, MySQL 4.1 needs this\r\nsuppress(ActiveRecord::StatementInvalid) do\r\nActiveRecord::Base.connection.execute 'SET NAMES UTF8'\r\nend\r\nend\r\nend<\/pre>\n<p style=\"margin-left: 40px\">(In case you\u2019re wondering, yes, you can have more than one <code>before_filter<\/code>.)<\/p>\n<div id=\"updatedappcontroller\" style=\"background: #eeeeee none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; margin-left: 40px\"><span style=\"color: #ff0000\">UPDATE<\/span> Er, but you can get away with just one: Jonas refactored it. Use this version instead. <img decoding=\"async\" alt=\":)\" class=\"wp-smiley\" src=\"http:\/\/ruphus.com\/blog\/wp-images\/smilies\/icon_smile.gif\" \/><\/p>\n<pre>class ApplicationController < ActionController::Base\r\nbefore_filter :configure_charsets\r\n\r\ndef configure_charsets\r\n@response.headers[\"Content-Type\"] = \"text\/html; charset=utf-8\"\r\n# Set connection charset. MySQL 4.0 doesn't support this so it\r\n# will throw an error, MySQL 4.1 needs this\r\nsuppress(ActiveRecord::StatementInvalid) do\r\nActiveRecord::Base.connection.execute 'SET NAMES UTF8'\r\nend\r\nend\r\nend<\/pre>\n<p><span style=\"color: #ff0000\">UPDATED AGAIN<\/span> Argh, I forgot to remove the <code>set_charset<\/code> definition before. It should be correct now\u2026<\/div>\n<p><!-- #updatedappcontroller --><\/p>\n<p style=\"margin-left: 40px\">Anyway, now our Rails installation seems to handle (almost) <a title=\"Omniglot - a guide to written language (alphabets, hieroglyphs, Chinese characters, etc)\" href=\"http:\/\/www.omniglot.com\/\">any nutty writing system<\/a> that we throw at it.<\/p>\n<p style=\"margin-left: 40px\">I\u2019m not sure these are all the Right Way\u2122, I\u2019m just saying it\u2019s worked for us (so far).<\/p>\n<p style=\"margin-left: 40px\">For one thing, it seems like it might make sense to just go ahead and <a title=\"MySQL Reference Manual :: 10.3 Determining the Default Character Set and Collation\" href=\"http:\/\/dev.mysql.com\/doc\/mysql\/en\/charset-defaults.html\">set the default character set and collation<\/a> for MySQL to UTF-8, independently of any Rails stuff at all. Aren\u2019t all character sets supposed to be slouching toward Unicode, anyway? But that sounds like a rather apocalyptic measure, for some reason\u2026 I guess I\u2019ll hold off on the rapture of the character sets.<\/p>\n<p style=\"margin-left: 40px\">Collation is it\u2019s own topic \u2014 but then we\u2019ve not gotten to sorting anything yet. <a title=\"Unicode Data with PHP 5 and MySQL 4.1\" href=\"http:\/\/www.shawnolson.net\/a\/946\/\">Here\u2019s a recommendation for building the tables<\/a> with:<\/p>\n<pre style=\"margin-left: 40px\">CHARACTER SET utf8 COLLATE utf8_general_ci;<\/pre>\n<p style=\"margin-left: 40px\">To set the collation order. Wading through the MySQL docs on that is next on the agenda.<\/p>\n<\/div>\n<p><a target=\"_blank\" href=\"http:\/\/ruphus.com\/blog\/2005\/06\/23\/getting-unicode-mysql-and-rails-to-cooperate\/\">source<\/a><\/p>\n<p>Un autre tuto assez complet \u00e9galement :<\/p>\n<blockquote><p>Le but de cet article est de synth\u00e9tiser les param\u00e8tres de configuration de rails et de mysql pour n\u2019avoir aucun probl\u00e8me de charset dans la vie d\u2019une application rails.<\/p>\n<p>L\u2019id\u00e9e est d\u2019utiliser utf-8 comme standard \u00e0 tous les niveau. Cela peut para\u00eetre simple mais \u00e7a ne l\u2019est pas trop. <a href=\"http:\/\/blog.leetsoft.com\/\">Tobias Luetke<\/a> disait que le plus simple dans rails \u00e9tait de ne rien param\u00e9trer. J\u2019ai essay\u00e9 (h\u00e9 oui je sais \u00eatre paresseux parfois \ud83d\ude42 ). Mais sans succ\u00e8s.<\/p>\n<h2>Du cot\u00e9 de rails version 1.x et sup\u00e9rieur<\/h2>\n<p>Dans le fichier <em><span style=\"color: red\">config\/database.yml<\/span><\/em><\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">development:\r\nadapter: mysql\r\ndatabase: app_dev\r\nusername: myuser\r\npassword: myuserpassword\r\nhost: localhost\r\nencoding: utf8<\/code><\/pre>\n<\/div>\n<p>Dans le fichier <em><span style=\"color: red\">config\/environment.rb<\/span><\/em><\/p>\n<p>ajouter le support(<em>partiel<\/em>) pour l\u2019unicode<\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">#unicode support\r\n$KCODE = 'u'\r\nrequire_dependency 'jcode'<\/code><\/pre>\n<\/div>\n<p>Je ne comprends d\u2019ailleurs pas pourquoi ce n\u2019est pas la config par d\u00e9faut comme c\u2019est d\u00e9j\u00e0 le cas pour l\u2019actionmailer.<\/p>\n<p>Dans le fichier <em><span style=\"color: red\">app\/controller\/application.rb<\/span><\/em><\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">  before_filter :set_charset\r\ndef set_charset\r\nsuppress(ActiveRecord::StatementInvalid) do\r\nActiveRecord::Base.connection.execute 'SET NAMES UTF8'\r\nend\r\n\r\nif request.xhr?\r\n@headers['Content-Type'] = 'text\/javascript; charset=utf-8'\r\nelse\r\n@headers['Content-Type'] = 'text\/html; charset=utf-8'\r\nend\r\nend<\/code><\/pre>\n<\/div>\n<p>Normalement (voir le point sur mysql, le fait de placer encoding: utf8 dans le fichier database.yml devrait suffire. Enfin pas chez moi !:( Suite \u00e0 cela je rajoute l\u2019instruction \u2018SET <span class=\"caps\">NAMES UTF8<\/span>\u2019 directement avant chaque appel au query.<\/p>\n<p>Dans le fichier <em><span style=\"color: red\">app\/helpers\/application_helper.rb<\/span><\/em><\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">  #for ajax call use this instead of only h()\r\ndef ha(html)\r\nh(html).gsub(\/([^\\x00-\\x9f])\/u) { |s|\r\n\"&#x%x;\" % $1.unpack('U')[0] }\r\nend<\/code><\/pre>\n<\/div>\n<p>pour n\u2019avoir pas de soucis sous safari.<\/p>\n<h3>Les mails (un bref aper\u00e7u)<\/h3>\n<p>je cr\u00e9e par d\u00e9faut une classe de base dont tous les notificateurs vont h\u00e9riter<\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">class Notifier < ActionMailer::Base\r\ndef setup_email\r\n@sent_on    = Time.now\r\n@headers['Content-Type'] = \"text\/plain; charset=utf-8; format=flowed\"\r\ncontent_type('text\/plain; charset=utf-8; format=flowed')\r\nend\r\ndef setup_email_html\r\n@sent_on    = Time.now\r\n@headers['Content-Type'] = \"text\/html; charset=utf-8; format=flowed\"\r\ncontent_type('text\/html; charset=utf-8; format=flowed')\r\nend\r\n\r\nend<\/code><\/pre>\n<\/div>\n<p>Enfin pour le sujet du mail j\u2019utilise la fonction  <strong>quote_if_necessary(@subject,\u201dUTF-8\u201d)<\/strong>. En effet les diff\u00e9rentes directive (RFC) stipulent que le sujet lors d\u2019un transfert <span class=\"caps\">SMTP<\/span> doit \u00eatre encod\u00e9 en 7bit donc pas compatible utf8(8bit). R\u00e9sultat des course de ce sujet hyper-r\u00e9sum\u00e9 : transformer le sujet en format uuencode ou en quoted-print.<\/p>\n<p>Bon selon les version d\u2019outlook , on peut avoir encore de belles surprises \ud83d\ude42 mais \u00e7a c\u2019est une autre histoire.<\/p>\n<h2>Mysql 4.1+<\/h2>\n<p>Si la base de donn\u00e9es mysql est souvent fournie compil\u00e9e avec le latin1 comme charset par d\u00e9faut.<\/p>\n<p>il sufit de rajouter\/moidifier le fichier suivant <em><span style=\"color: red\">\/etc\/my.cnf<\/span><\/em> avec<\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">[mysqld]\r\n#Set the default character set.\r\ndefault-character-set=utf8\r\n#Set the default collation.\r\ndefault-collation=utf8_general_ci\r\n\r\n#character-set-results=utf8\r\n#character-set-client=utf8\r\n#character-set-connection=utf8<\/code><\/pre>\n<\/div>\n<p>Ensuite il faut cr\u00e9er les tables sans pr\u00e9ciser le charset ou avec <span class=\"caps\">DEFAULT CHARSET<\/span>=utf8<\/p>\n<p>Les variables suivantes sous mysql d\u00e9finissent son comportment client-server:<\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">character_set_results=latin1\r\ncharacter_set_client=latin1\r\ncharacter_set_connection=latin1\r\n\r\ncharacter_set_server=utf8\r\ncollation_server=utf8_general_ci\r\n\r\ncharacter_set_database=utf8\r\ncollation_connection=utf8_general_ci\r\n<\/code><\/pre>\n<\/div>\n<p>Les 4 derni\u00e8res variables d\u00e9finissent le charset et sa collation (la fa\u00e7on dont sont index\u00e9 les accents, influence les resultats de recherche de type <span class=\"caps\">FULLTEXT<\/span>) au niveau de la base de donn\u00e9es elle-m\u00eame c-\u00e0-dire la fa\u00e7on dont il stocke l\u2019info.<\/p>\n<p>Les 3 premi\u00e8res d\u00e9finissent le charset auquel s\u2019attend le client qui se connecte.<\/p>\n<p>Dans le cadre de mon example je n\u2019ai pas forc\u00e9 les param\u00e8tres par d\u00e9faut de la db \u00e0 utf8 car de nombreuses applications <span class=\"caps\">PHP<\/span> sur le serveur s\u2019attendent \u00e0 ces param\u00e8tres par d\u00e9faut.<\/p>\n<p>Donc si je ne pr\u00e9cise rien, le serveur envoie \u00e0 mon application rails des caract\u00e8res latin1. Par contre lors d\u2019un update ( avec juste l\u2019encoding plac\u00e9 dans le fichier database.yml), les caract\u00e8res sont correctement encod\u00e9 dans la db. D\u2019ou la n\u00e9cessit\u00e9 de placer la commande <span class=\"caps\">SET NAMES UTF8<\/span> qui a pour effet de changer ces 3 variables<\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">character-set-results=utf8\r\ncharacter-set-client=utf8\r\ncharacter-set-connection=utf8<\/code><\/pre>\n<\/div>\n<p>ce qui a pour effet de bien conserver de l\u2019utf8.<\/p>\n<p>On pourrait se poser la question de laisser le latin1 par d\u00e9faut dans la db, car si l\u2019application pr\u00e9cise que l\u2019on veut de l\u2019utf8, mysql convertira le latin1 en utf8.<\/p>\n<p>Le probl\u00e8me survient lors de la gestion de la DB.<\/p>\n<p>Les backup (dump) ou l\u2019\u00e9dition via des outils web comme <span class=\"caps\">PHP<\/span>Myadmin ou la plupart des client <span class=\"caps\">GUI<\/span> vont poser probl\u00e8me.<\/p>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">mysqldump db_to_export >file.sql --default-character-set=utf8 -u myuser -p --add-drop-table -c\r\n\r\nmysql db_to_import <file.sql --default-character-set=utf8 -u myuser -p<\/code><\/pre>\n<\/div>\n<p>Pour ceux qui veulent je vous propose un script d\u2019adaptation de vos tables latin1 en utf8<\/p>\n<p><code class=\"typocode_default\" \/><br \/>\n<code class=\"typocode_default\" \/><br \/>\n<code class=\"typocode_default\"># This script has been written to convert utf-8 character stored in latin1 table in mysql Table<\/code><br \/>\n<code class=\"typocode_default\"># work for mysql 4.1 <\/code><br \/>\n<code class=\"typocode_default\">database = \"database\"<\/code><br \/>\n<code class=\"typocode_default\">sqlconn = ActiveRecord::Base.establish_connection(<\/code><br \/>\n<code class=\"typocode_default\">:adapter  => \"mysql\", <\/code><br \/>\n<code class=\"typocode_default\">:host     => \"localhost\", <\/code><br \/>\n<code class=\"typocode_default\">:username => \"root\", <\/code><br \/>\n<code class=\"typocode_default\">:password => \"\", <\/code><br \/>\n<code class=\"typocode_default\">:database => database<\/code><br \/>\n<code class=\"typocode_default\">)<\/code><br \/>\n<code class=\"typocode_default\" \/><br \/>\n<code class=\"typocode_default\">#conn.execute(\"ALTER TABLE contents MODIFY title BINARY(255);\")<\/code><br \/>\n<code class=\"typocode_default\">#=> nil<\/code><br \/>\n<code class=\"typocode_default\">#>> conn.execute(\"ALTER TABLE contents MODIFY title varchar(255) character set utf8;\")<\/code><br \/>\n<code class=\"typocode_default\">#=> nil<\/code><br \/>\n<code class=\"typocode_default\">#>> conn.execute(\"ALTER TABLE contents MODIFY body BLOB;\")<\/code><br \/>\n<code class=\"typocode_default\">#=> nil<\/code><br \/>\n<code class=\"typocode_default\">#>> conn.execute(\"ALTER TABLE contents MODIFY body TEXT CHARACTER SET utf8;\")<\/code><br \/>\n<code class=\"typocode_default\">#=> nil<\/code><br \/>\n<code class=\"typocode_default\">#>> conn.execute(\"ALTER TABLE contents MODIFY title BINARY(255);\")<\/code><br \/>\n<code class=\"typocode_default\">#=> nil<\/code><br \/>\n<code class=\"typocode_default\">#>> conn.execute(\"ALTER TABLE contents MODIFY title varchar(255) character set utf8;\")<\/code><br \/>\n<code class=\"typocode_default\" \/><br \/>\n<code class=\"typocode_default\"># open a connection to the table<\/code><br \/>\n<code class=\"typocode_default\">conn = ActiveRecord::Base.send(sqlconn.adapter_method,sqlconn.config)<\/code><br \/>\n<code class=\"typocode_default\"># run the converion on each table<\/code><br \/>\n<code class=\"typocode_default\">conn.tables.each do |table|<\/code><br \/>\n<code class=\"typocode_default\">    res = conn.execute(\"show create table #{table}\")<\/code><br \/>\n<code class=\"typocode_default\">    str = res.fetch_hash[\"Create Table\"]<\/code><br \/>\n<code class=\"typocode_default\">    #find which table has the latin1 charset<\/code><br \/>\n<code class=\"typocode_default\">    charset = str.scan(\/CHARSET=(.*)\/).flatten[0] <\/code><br \/>\n<code class=\"typocode_default\">    if charset == \"latin1\"<\/code><br \/>\n<code class=\"typocode_default\">        message =\"table #{table} process \"<\/code><br \/>\n<code class=\"typocode_default\">        #convert each varchar and text field<\/code><br \/>\n<code class=\"typocode_default\">        #res = conn.execute(\"show FULL columns from contents\")<\/code><br \/>\n<code class=\"typocode_default\">        table_columns = conn.columns(table)<\/code><br \/>\n<code class=\"typocode_default\">        table_columns.each do |column|<\/code><br \/>\n<code class=\"typocode_default\">            next unless [:text,:string].include?(column.type)<\/code><br \/>\n<code class=\"typocode_default\">            case column.type<\/code><br \/>\n<code class=\"typocode_default\">            when :text<\/code><br \/>\n<code class=\"typocode_default\">                message << column.name + \" \"<\/code><br \/>\n<code class=\"typocode_default\">                puts \"ALTER TABLE #{table} MODIFY #{column.name} blob;\"<\/code><br \/>\n<code class=\"typocode_default\">                #Transform in binary blob<\/code><br \/>\n<code class=\"typocode_default\">                res_step1 = conn.execute(\"ALTER TABLE #{table} MODIFY #{column.name} blob;\")<\/code><br \/>\n<code class=\"typocode_default\">                puts \"ALTER TABLE #{table} MODIFY #{column.name} text CHARACTER SET utf8;\"<\/code><br \/>\n<code class=\"typocode_default\">                #convert the binary into text with the good charset utf8<\/code><br \/>\n<code class=\"typocode_default\">                res_step2 = conn.execute(\"ALTER TABLE #{table} MODIFY #{column.name} text CHARACTER SET utf8;\")<\/code><br \/>\n<code class=\"typocode_default\" \/><br \/>\n<code class=\"typocode_default\">            when :string<\/code><br \/>\n<code class=\"typocode_default\">                message << column.name + \" \"<\/code><br \/>\n<code class=\"typocode_default\">                #puts \"ALTER TABLE #{table} MODIFY #{column.name} binary(#{column.limit});\"<\/code><br \/>\n<code class=\"typocode_default\">                #Transform in binary string<\/code><br \/>\n<code class=\"typocode_default\">                res_step1 = conn.execute(\"ALTER TABLE #{table} MODIFY #{column.name} binary(#{column.limit});\")<\/code><br \/>\n<code class=\"typocode_default\">                #puts \"ALTER TABLE #{table} MODIFY #{column.name} varchar(#{column.limit}) CHARACTER SET utf8;\"<\/code><br \/>\n<code class=\"typocode_default\">                #convert the binary into text with the good charset utf8<\/code><br \/>\n<code class=\"typocode_default\">                res_step2 = conn.execute(\"ALTER TABLE #{table} MODIFY #{column.name} varchar(#{column.limit}) CHARACTER SET utf8;\")<\/code><br \/>\n<code class=\"typocode_default\">            end<\/code><br \/>\n<code class=\"typocode_default\" \/><br \/>\n<code class=\"typocode_default\">        end<\/code><br \/>\n<code class=\"typocode_default\">        puts message<\/code><br \/>\n<code class=\"typocode_default\">        #now that every field are updated  we update the table charset<\/code><br \/>\n<code class=\"typocode_default\">        res_step3 = conn.execute(\"ALTER TABLE #{table} DEFAULT CHARACTER SET utf8 ;\")<\/code><br \/>\n<code class=\"typocode_default\">    end<\/code><br \/>\n<code class=\"typocode_default\">end <\/code><br \/>\n<code class=\"typocode_default\">#now that every table are updated  we update the table charset<\/code><br \/>\n<code class=\"typocode_default\">res_step3 = conn.execute(\"ALTER DATABASE #{database} DEFAULT CHARACTER SET utf8 ;\")<\/code><br \/>\n<code class=\"typocode_default\" \/><br \/>\n<code class=\"typocode_default\">#be sure to change the connection<\/code><br \/>\n<code class=\"typocode_default\">SET GLOBAL character-set-results = 'utf8';<\/code><br \/>\n<code class=\"typocode_default\">SET GLOBAL character-set-client = 'utf8';<\/code><br \/>\n<code class=\"typocode_default\">SET GLOBAL character-set-connection = 'utf8';<\/code><br \/>\n<code class=\"typocode_default\">SET GLOBAL character-set-server = 'utf8';<\/code><br \/>\n<code class=\"typocode_default\">SET GLOBAL character-set-database = 'utf8';<\/code><br \/>\n<code class=\"typocode_default\">SET GLOBAL collation-connection = 'utf8_general_ci';<\/code><br \/>\n<code class=\"typocode_default\" \/><\/p><\/blockquote>\n<blockquote>\n<div class=\"typocode\">\n<pre><code class=\"typocode_default\">SET GLOBAL collation-server = 'utf8_general_ci';<\/code><\/pre>\n<\/div>\n<\/blockquote>\n<p><a target=\"_blank\" href=\"http:\/\/denis.shoob.com\/articles\/tag\/utf8\">source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voici plusieurs tutoriaux complets permettant de faire fonctionner la cha\u00eene de liaison depuis le navigateur jusqu&#8217;\u00e0 Mysql en passant par rails.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-ruby-on-rails"],"_links":{"self":[{"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":0,"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.dotmana.com\/weblog\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}