For a much more elegant way, visit my blog.
Created
September 17, 2008 17:49
-
-
Save mislav/11264 to your computer and use it in GitHub Desktop.
"Branch" your database together with git
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.thor | |
# | |
# Thor task to fully copy your master database into a new database for current branch. | |
# Sample usage: | |
# | |
# thor git:db:clone | |
# | |
# What gets run: | |
# | |
# mysqladmin -u #{user} create #{target} | |
# mysqldump -u #{user} #{from} | mysql -u #{user} #{target} | |
# | |
# Limitation: only works if password authentication isn't required. | |
# | |
class Git < Thor | |
module Command | |
protected | |
def command(com, capture = false) | |
unless capture | |
system(com) | |
else | |
@output = %x(#{com}).chomp | |
end | |
end | |
def last_command_was_success? | |
$?.success? | |
end | |
end | |
class Db < Thor | |
include Command | |
desc "clone", "Branch your development database" | |
method_options :force => :boolean | |
def clone | |
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"] | |
# create the new database | |
command "mysqladmin -u #{user} create #{to} 2>&1", true | |
unless last_command_was_success? | |
if @output.index('database exists') | |
warn %(Database "#{to}" already exists) | |
return unless options[:force] | |
else | |
warn @output | |
return | |
end | |
end | |
# clone development database | |
command "mysqldump -u #{user} #{from} | mysql -u #{user} #{to}" | |
puts %(Finished cloning "#{from}" to "#{to}".) if last_command_was_success? | |
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 | |
if 'development' == RAILS_ENV | |
require 'rubygems' | |
require 'grit' # use "mojombo-grit" from github | |
## uncomment if your application is running in an environment | |
## where `git` command cannot be found in the PATH (i.e. Passenger) | |
# Grit::Git.git_binary = '/usr/local/git/bin/git' | |
end | |
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