-
-
Save ryanfb/59409 to your computer and use it in GitHub Desktop.
SQLite & rake-based Git & Rails database branching, see http://tinyurl.com/a7e6jo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bootstrap the Rails environment, frameworks, and default configuration | |
require File.join(File.dirname(__FILE__), 'boot') | |
require 'git_conf' | |
Rails::Initializer.run(:process, GitConf.new) do |config| | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## lib/tasks/git.rake | |
# | |
# Rake task to fully copy your master database into a new database for current branch. | |
# Sample usage: | |
# | |
# rake git:db:clone | |
# | |
# What gets run: | |
# | |
# cp #{from} #{target} | |
# | |
namespace :git do | |
namespace :db do | |
desc "Branch your development database" | |
task :clone => :environment do | |
require 'fileutils' | |
require 'config/boot' | |
require 'git_conf' | |
config = GitConf.new | |
dbconfig = config.database_configuration["development"] | |
if config.branched_database? | |
user = dbconfig["username"] | |
from = dbconfig["master-database"] | |
to = dbconfig["database"] | |
FileUtils.cp(from, to) | |
else | |
warn "branch isn't configured for a separate database" | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## lib/git_conf.rb | |
# Tell a branch to have its own database with: | |
# git config branch.#{branch_name}.database true | |
# Clone from master with rake git:db:clone | |
require 'rubygems' | |
require 'grit' # "mojombo-grit" gem from github | |
def Rails.repo | |
@@repository ||= Grit::Repo.new(Rails.root) | |
end | |
# Adjust your environment.rb to use this subclass: | |
# Rails::Initializer.run(:process, GitConf.new) do |config| | |
# ... | |
# end | |
class GitConf < Rails::Configuration | |
def initialize | |
super | |
@branched_database = false | |
end | |
def branched_database?() @branched_database end | |
# agument the original method in order to append | |
# the branch name suffix in certain conditions | |
def database_configuration | |
@database_configuration ||= begin | |
config = super | |
if Rails.env == "development" | |
head = Rails.repo.head | |
branch = head && head.name | |
# check if this branch has a special database | |
if branch and branch != "master" and branch !~ /\W/ and branch_has_database?(branch) | |
development = config["development"] | |
# save original configuration | |
development["master-database"] = development["database"] | |
# append branch name suffix | |
base_name, extension = development["database"].split(".", 2) | |
development["database"] = "#{base_name}_#{branch}" | |
development["database"] += ".#{extension}" if extension | |
# finally, indicate that this database has branched | |
@branched_database = true | |
end | |
end | |
config | |
end | |
end | |
protected | |
def branch_has_database?(branch) | |
Rails.repo.config["branch.#{branch}.database"] == "true" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment