In our Ruby On Rails 3 application, it became necessary to switch from Sqlite to MySQL. We needed a simple and effective way, without the cumbersome Perl scripts and paid utilities, which the Internet and in particular StackOverflow replete with.
Description
The best option was gem
yaml_db , which arose in the depths of the
Heroku development team.
The essence of gem work is that it saves a data dump of one database into a YAML file to disk, and then loads it into another database (technically, of course, this approach can be used for backups).
The advantage of this approach is that it does not matter which base you work with. Theoretically supported all databases for which there is an ActiveRecord adapter.
')
Simplest use case
Add yaml_db to the gemfile:
gem 'yaml_db', :git => 'git://github.com/ludicast/yaml_db.git'
Run the Bundler:
bundle install
Initial database configuration:
config / database.yml
production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000
We make, actually, a dump:
export RAILS_ENV=production
rake db:data:dump
After that in db / data.yml there should be data of your base.
If everything is good, then change the database configuration to the one you need:
config / database.yml
production: adapter: mysql2 database: app_production username: root password: root host: localhost pool: 5 timeout: 5000
Do not forget to add the necessary gem in the gemfile.
Create a database and run the migration:
rake db:create
rake db:migrate
And finally, we load the dump into the new database:
rake db:data:load
Now you can start the server and check that everything is working.
Remarks
If you are using Ruby 1.9, then Bundler version 1.0.10 loads Psych as the YAML engine by default, which does not work correctly with some versions of Rails, so if you have any problems inserting a dump, then one of the solutions Psych can be replaced by Syck:
in config / application.rb after line
Bundler.require(:default, Rails.env) if defined?(Bundler)
to insert
YAML::ENGINE.yamler = 'syck'
yaml_db can do not only a data dump, but also a database structure:
rake db:dump
rake db:load
The first task dumps data and structures, the second creates tables and loads data.
Links
Article about yaml_db on Heroku blog
The yaml_db branch for Rails 2.x