Give each git branch its own databases for ActiveRecord.
create, delete, copy git branch-specific databases using rake tasks
automatic switching based on currently checked out branch
Put the following block of code into config/environment.rb before Rails::Initializer.run. This code ensures that the database is automatically switched, based on the checked out git branch.
# config/environment.rb ... require 'branch_db/configuration_twiddler' Rails::Configuration.class_eval do include ::BranchDb::ConfigurationTwiddler end ...
In config/database.yml, mark the configuration that may have databases specific to current branch
# config/database.yml development: &development adapter: postgresql database: myproject_development host: localhost port: 5432 per_branch: true
To your Rakefile or a file in lib/tasks add
Then you can use these rake tasks to manage your databases.
rake db:branches:list List all branch databases rake db:branches:current Currently selected databases. rake db:branches:create Create empty databases for a branch. Current or BRANCH. rake db:branches:copy Copy databases from one branch to another. Default is from ORIG_BRANCH=master to BRANCH=<current branch> rake db:branches:delete Delete databases for a branch given by BRANCH
Say, you have a configuration as above and you're currently on git branch “feature” when you decide that you need a database specific to this branch. Then
~/projects/myproject (feature*)$ rake db:branches:copy ORIG_BRANCH=master
will give you a copy of the database from branch “master”. If your branch already has a database that you want to overwrite, add OVERWRITE=true to the command line.
sudo gem install mschuerig-branch_db
(The MIT License)
Copyright © 2009 Michael Schuerig
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.