Rails: Migrate between databases

These days I’ve been wanting to migrate my Redmine installation from Sqlite3 to MySQL, because sqlite wasn’t giving me the performance I wanted.

After much research :) , I’ve found many scripts (many in perl) promising to process the Sqlite3 dump to a MySQL appropriate script. But none worked for me, either because they did not treat correctly string literals or because they did not pay attention to MySQL reserved words being used in table names…I tried to hack one of them, but an extensive work would be needed because I was trying to migrate a fully populated database, and several differing details between Sqlite and MySQL would need to be treated.

But ha! :) I was forgetting I was dealing with Rails: it’s engine is database agnostic and it has plugins.

After some googling, I found a handy plugin, yaml_db, which dumps the database in yaml format. After this, my work is just plain easy: change the database.yml to point to my new MySQL database and use the plugin to import the data from the file. It’s a complete copy of the database from one engine to the other.

To avoid some problems, I installed the plugin with script/plugin instead of using the gem:

$ ruby script/plugin install git://github.com/ludicast/yaml_db.git

Then, it was just a matter of following the instructions given in the page:

$ RAILS_ENV=production rake db:dump
$ vim config/database.yml # change the production database to point to my new MySQL db. I need to have user and db created.
$ RAILS_ENV=production rake db:load

This little beauty saved my day.

Consulted Material

Spread the word...
  • Twitter
  • Facebook
  • del.icio.us
  • Google Bookmarks
  • StumbleUpon
  • Digg

Post a Comment

Your email is never published nor shared. Required fields are marked *

-->

Page optimized by WP Minify WordPress Plugin

Miscellaneous is Digg proof thanks to caching by WP Super Cache