If you’ve worked with web apps for a while, you know how important it is to move database info around. You’ve got to make backups. Go through the real data to see if you’re creating junk. Test schema migrations. Make test fixtures. Maintain realistic data in staging. All of this is a total pain.
That’s why the folks at heroku created taps. It’s a simple service to push and pull info from your database. And if you’re using heroku, it’s a breeze, just run heroku db:pull and your production database is copied to your local.
But if you’re running your own machines, taps is a bit more tricky. It wants you to identify your database using a URI. That’s great for datamapper and sequel users, but what abound the everyday rails app with activerecord? You’ve already got a config/database.yml, why not use it? That’s why I put together the taps_plugin for rails 3.
Installation
Install the taps plugin in your Rails 3 app by going to the application directory and running:
rails plugin install http://github.com/josephholsten/taps_plugin.git
Then run the generator to add the taps script:
rails generate taps
Usage
The taps plugin provides a script that will talk to your rails environment to figure out your database configuration, start a taps service, and pull data from other taps services.
Start a taps server by heading to your live app and telling the script to start a server, passing it a username and password:
script/taps server bobdobbs secret
By default, this will start a taps server to the database for your Rails.env on port 5000.
You can then pull from this taps server into your local copy of your app by using the taps script to pull from the server using your username and password:
script/taps pull http://bobdobbs:secret@liveapp.host:5000
That’s all you’ve got to do. Of course, taps supports many more options. You can also run the taps server for a different database on a different port with debug logging:
RAILS_ENV=development script/taps server --port 3000 --debug bobdobbs secret
You can also pull just some tables without compression:
script/taps pull --disable-compression --tables=Posts,Users http://bobdobbs:secret@liveapp.host:5000
You can explore even more options in the usage messages from running the taps script.
Rails 2
This plugin is designed to work with Rails 3, but most people aren’t ready to make the leap so the taps plugin still works with a little effort. The generator only works in Rails 3, you’ll need to add the taps script by hand. Just save this code in script/taps:
#!/usr/bin/env ruby
require File.expand_path('../../config/environment', __FILE__)
require 'taps_plugin'
TapsPlugin::Command.start(ARGV)
Make sure to make it executable and the script should work as normal.