<?xml version="1.0" encoding="UTF-8"?>
<post>
  <alt nil="true"></alt>
  <body>&lt;p&gt;On ne va pas le r&#233;perter, rake c'est le couteau suisse avec RoR, il regroupe plusieurs fonctions pour automatiser certaine t&#226;ches, comme : effectuer des tests, effectuer un dump de la structure de base de donn&#233;es, effacer la table des sessions, effacer les fichiers log,  g&#233;n&#233;rer une documentation pour expliquer les fonctions de l'application, purger la base de donn&#233;es, effectuer un rapport sur le code de l'application... enfin la liste est longue, il faudra tout un manuel pour r&#233;unir toutes ces fonctions qu'on n'arrive plus &#224; s'en s&#233;parer une fois adopt&#233;es.&lt;p&gt;
&lt;p&gt;Dans ce modeste post, je vais essayer de pr&#233;sente les fonctions de migration avec Rake, concernant la base de donn&#233;es, bien sur&lt;/p&gt;
&lt;p&gt;Quand j'ai d&#233;couvert Rails et que j'ai commenc&#233; &#224; jouer au petit alchimiste avec, je changeais la structure de ma base de donn&#233;es manuellement avec des applications externes et parfois m&#234;me avec phpmyadmin quand l'application est d&#233;j&#224; sur le serveur de production. J'avais une vision restreinte (la faute &#224; PHP), je passais &#224; cot&#233; du syst&#232;me de migration de rake sans y faire attention. Par la suite, une lampe s'est allum&#233;, et je me suis rendu compte de tout ce temps perdu dans l'ignorance.&lt;/p&gt;
&lt;p&gt;Quand on g&#233;n&#233;re des models ou bien des &#233;chaffaudage (scaffold), les fichiers de la structure de base de donn&#233;es s'empile au fur et &#224; mesure dans le r&#233;pertoire &lt;span&gt;application/db/migrate/&lt;/span&gt; &#224; cot&#233; se trouve le fichier &lt;span&gt;application/db/schema.rb&lt;/span&gt; qui regroupe la structure de la base de donn&#233;es avec la r&#233;f&#233;rence du derniers fichier empil&#233;, en r&#233;sum&#233; la version actuelle de la migration de la base de donn&#233;es.&lt;/p&gt;
&lt;p&gt;Voici un exemple d'un fichier dans la liste empil&#233;e qui servira &#224; cr&#233;er une table &lt;span&gt;users&lt;/span&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;c&quot;&gt;# fichier 20090813120000_create_users.rb&lt;/span&gt;

&lt;span class=&quot;r&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;CreateUsers&lt;/span&gt; &amp;lt; &lt;span class=&quot;co&quot;&gt;ActiveRecord&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;Migration&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.up
    create_table &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:force&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |t|
      t.column &lt;span class=&quot;sy&quot;&gt;:login&lt;/span&gt;,                     &lt;span class=&quot;sy&quot;&gt;:string&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:email&lt;/span&gt;,                     &lt;span class=&quot;sy&quot;&gt;:string&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:crypted_password&lt;/span&gt;,          &lt;span class=&quot;sy&quot;&gt;:string&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:limit&lt;/span&gt; =&amp;gt; &lt;span class=&quot;i&quot;&gt;40&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:salt&lt;/span&gt;,                      &lt;span class=&quot;sy&quot;&gt;:string&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:limit&lt;/span&gt; =&amp;gt; &lt;span class=&quot;i&quot;&gt;40&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:created_at&lt;/span&gt;,                &lt;span class=&quot;sy&quot;&gt;:datetime&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:updated_at&lt;/span&gt;,                &lt;span class=&quot;sy&quot;&gt;:datetime&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:remember_token&lt;/span&gt;,            &lt;span class=&quot;sy&quot;&gt;:string&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:remember_token_expires_at&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:datetime&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:activation_code&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:string&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:limit&lt;/span&gt; =&amp;gt; &lt;span class=&quot;i&quot;&gt;40&lt;/span&gt;
      t.column &lt;span class=&quot;sy&quot;&gt;:activated_at&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:datetime&lt;/span&gt;
      
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.down
    drop_table &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;On vois bien la nomination du fichier qui est en rapport direct avec le nom de la Classe. Deux m&#233;thodes se trouvent dans la classe : une pour la cr&#233;ation, la seconde pour la destruction(on verra plus tard comment utiliser la fonction destrutrice)&lt;/p&gt;

&lt;p&gt;Depuis la version 2.0 de Rails, il y a une nouvelle nomenclature plus &quot;sexy&quot; pour d&#233;crire la structure de la table:&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;r&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;CreateUsers&lt;/span&gt; &amp;lt; &lt;span class=&quot;co&quot;&gt;ActiveRecord&lt;/span&gt;::&lt;span class=&quot;co&quot;&gt;Migration&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.up
    create_table &lt;span class=&quot;sy&quot;&gt;:users&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:force&lt;/span&gt; =&amp;gt; &lt;span class=&quot;pc&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |t|
      t.string &lt;span class=&quot;sy&quot;&gt;:login&lt;/span&gt;,email,                     &lt;span class=&quot;sy&quot;&gt;:string&lt;/span&gt;
      t.string, &lt;span class=&quot;sy&quot;&gt;:limit&lt;/span&gt; =&amp;gt; &lt;span class=&quot;i&quot;&gt;40&lt;/span&gt;,  &lt;span class=&quot;sy&quot;&gt;:crypted_password&lt;/span&gt;, salt  
      t.string &lt;span class=&quot;sy&quot;&gt;:remember_token&lt;/span&gt;
      t.datetime &lt;span class=&quot;sy&quot;&gt;:remember_token_expires_at&lt;/span&gt;
      t.string, &lt;span class=&quot;sy&quot;&gt;:limit&lt;/span&gt; =&amp;gt; &lt;span class=&quot;i&quot;&gt;40&lt;/span&gt; &lt;span class=&quot;sy&quot;&gt;:activation_code&lt;/span&gt;, :
      t.datetime &lt;span class=&quot;sy&quot;&gt;:activated_at&lt;/span&gt;
      t.timestamps
      
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;pc&quot;&gt;self&lt;/span&gt;.down
    drop_table &lt;span class=&quot;sy&quot;&gt;:users&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
</body>
  <cat-id type="integer">1</cat-id>
  <created-at type="datetime">2009-08-13T12:47:56Z</created-at>
  <id type="integer">8</id>
  <pagetitle>Migration avec Rake</pagetitle>
  <photo-content-type nil="true"></photo-content-type>
  <photo-file-name nil="true"></photo-file-name>
  <photo-file-size type="integer" nil="true"></photo-file-size>
  <short>On ne peut pas d&#233;velopper sous Rails sans Rake, &#231;a sera comme r&#233;inventer une t&#233;l&#233;vision au lieu de zapper sur la t&#233;l&#233;commande.</short>
  <title>Migration avec Rake</title>
  <updated-at type="datetime">2009-08-13T14:59:15Z</updated-at>
  <url>rake-migration</url>
</post>
