<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>&lt;p&gt;Hi Sirs,&lt;/p&gt; &lt;p&gt;Many tutorials over the web shows how to load data from fixtures. But, if you want to migrate your &lt;em&gt;mysql&lt;/em&gt; data to a &lt;em&gt;postgresql&lt;/em&gt; database?&lt;/p&gt; &lt;p&gt;We can export our data to fixtures, and after that we can load the fixtures to every database management system we wish.&lt;/p&gt; &lt;p&gt;Let's go to the rake task:&lt;/p&gt;

&lt;pre&gt;&lt;span class=&quot;ident&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;export the database models to YML fixtures&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt; 
&lt;span class=&quot;ident&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:models_to_fixtures&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:environment&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; 
  &lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;establish_connection&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;symbol&quot;&gt;:adapter&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;postgresql&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;',&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;# mysql, sqlite3&lt;/span&gt;
    &lt;span class=&quot;symbol&quot;&gt;:encoding&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;utf8&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;',&lt;/span&gt;
    &lt;span class=&quot;symbol&quot;&gt;:database&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;cnxs_development&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;',&lt;/span&gt;
    &lt;span class=&quot;symbol&quot;&gt;:username&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;',&lt;/span&gt;
    &lt;span class=&quot;symbol&quot;&gt;:password&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;
  &lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;connection&lt;/span&gt;

  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;['&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;MODELS&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'].&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;nil?&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;['&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;MODELS&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'].&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;blank?&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;Please enter valid models names separated by coma. Ex: MODELS=User,Account&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;ident&quot;&gt;models&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;['&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;MODELS&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'].&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;('&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;').&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;collect&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;camelize&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;constantize&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;ident&quot;&gt;models&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;collection&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:all&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;collection&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_param&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;file_path&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;expr&quot;&gt;#{RAILS_ROOT}&lt;/span&gt;/tmp/&lt;span class=&quot;expr&quot;&gt;#{model.table_name}&lt;/span&gt;.yml&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;# /tmp/&lt;/span&gt;
    &lt;span class=&quot;constant&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;file_path&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;w+&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_yaml&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Here you need to fill your database attributes like you do in the &lt;em&gt;/config/database.yml&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;You will call the command line:&lt;/p&gt; &lt;pre&gt;&lt;code&gt;$ rake models_to_fixtures MODELS=&amp;lt;your model names separated by coma&amp;gt;&lt;/code&gt;&lt;/pre&gt; &lt;p&gt;For example:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;$ rake models_to_fixtures MODELS=User,Post&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;All your model data will be stored on &lt;em&gt;/tmp/&lt;/em&gt; directory.&lt;/p&gt; &lt;p&gt;Download this rake task &lt;a href=&quot;../../models_to_fixtures.rake&quot;&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;See you.&lt;/p&gt;</body>
  <created-at type="datetime">2008-07-08T17:12:49Z</created-at>
  <id type="integer">27</id>
  <permalink>export-the-database-models-to-yml-fixtures</permalink>
  <title>Export the database models to YML fixtures</title>
  <updated-at type="datetime">2008-11-13T19:54:17Z</updated-at>
  <user-id type="integer">3</user-id>
</post>
