Dumping and restoring data with the YamlDB gem in Rails
What is YamlDB (yaml_db)?
YamlDb is a Rails plugin and a gem for dumping and restoring data in YAML format. It complements the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml.
Some Real-world usages
- Backup data in YAML format (database-independent)
- Restore YAML data to virtually any database
- Switching from say SQLite to mySQL, or other databases
- Replicate production database data to development database with ease
You can find out how to do all of this in the following blogs:
http://www.railslodge.com/plugins/830-yaml-db
http://accidentaltechnologist.com/ruby/change-databases-in-rails-with-yamldb/
http://blog.heroku.com/archives/2007/11/23/yamldb_for_databaseindependent_data_dumps/
This plugin/gem is really good, the only thing I think it's lacking of is a command-line utility. Why? Because some times you only need to switch the database engine or replicate data one time, why would we have to install a plugin to a specific Rails application if we can have a command-line that works for all the Rails application we work in?
One of the main issues is that yaml_db works with rake tasks so although it's a gem you have to install the rake tasks in your Rails application manually.
That was the main reason I forked the yaml_db project and added it a command-line utility, the only think you'd need to do is to install my fork of the yaml_db gem, not the official one.
For instructions to install my fork click here.
Once installed just issue:
$ yaml_db -h
to see the help.
Usage:
yaml_db task environment
- task must be an existing rake task in the yaml_db plugin.
Dumping and restoring data with the command-line utility
yaml_db won't work if you're outside a Rails app folder, so...
$ cd ~/projects/my_dummy_rails_application
Replicate production database data to the development one
$ yaml_db data:dump production $ yaml_db data:load development
Dumping production schema to db/schema.rb and data to db/data.yml
$ yaml_db dump production
Loading db/schema.rb and db/data.yml to development database
$ yaml_db load #environment defaults to development
Installing a gem fork from Github source
Usually, installing a gem is as easy as issuing:
gem install gem_name
but, what if you want to install a fork of a gem?
As you may know Github no longer hosts gems, so doing the following won't work:
$ sudo gem install technoweenie-grit --source http://gems.github.com
NOTE: Actually it'd work if the gem you're trying to install is in this list: http://gems.github.com/list.html
Disecting the "Download Source" button
Let's suppose we want to install the Techno Weenie's fork of Grit from Github:
Main repo => http://github.com/mojombo/grit
Techno Weenie's fork => http://github.com/technoweenie/grit
Github has a "Download Source" button, if you click it you'll be presented with two big images (say buttons): "ZIP" and "TAR", clicking these buttons will start downloading the code of the master branch in the selected format; below those buttons you'll find the list of tags of the repository, you'll be able to click one of the tags and again you'll be presented with "ZIP" and "TAR" to download the source of such tag.
How does this works?
This is how the URL is built:
http://github.com/USER/REPOSITORY/FORMAT/BRANCH_OR_TAG
"ZIP" => http://github.com/technoweenie/grit/zipball/master
"TAR" => http://github.com/technoweenie/grit/tarball/master
said this:
$ wget http://github.com/technoweenie/grit/zipball/master $ unzip technoweenie-grit-v2.0.0-12-g0bd0c5f.zip $ cd technoweenie-grit-0bd0c5f
Installing the gem
Gems source code has a special file called gem_name.gemspec, this is like the DNA of the gem and it's used to build it:
$ gem build grit.gemspec
Issuing this command will generate a gem_name-version.gem file which is the gem itself, let's install it:
$ sudo gem install grit-2.0.0.gem
Some projects doesn't have a .gemspec file but rake tasks, in that case you just need to do:
$ rake build $ rake install
If any of these methods work, you'd have to look at the Rakefile for tasks to generate the .gemspec file.
...and we're done!
NOTE: 'gem build' will not take in consideration special and external dependencies the gem might require, so you must take care of the dependencies manually. Check in the code all the 'require' lines to see dependencies.
Hope it helps.
Does anyone knows an easier method?