-
-
Save bjeanes/14007 to your computer and use it in GitHub Desktop.
PostgreSQL version of GitConf to manage branched databases
This file contains hidden or 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 hidden or 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: | |
# | |
# createdb -U #{user} #{to} | |
# pg_dump --format=T -D -U #{user} #{from} | pg_restore --no-owner -d #{to} -U #{user} | |
# | |
# 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 "createdb -U #{user} #{to} 2>&1", true | |
unless last_command_was_success? | |
if @output.index('already exists') | |
warn %(Database "#{to}" already exists) | |
return unless options[:force] | |
else | |
warn @output | |
return | |
end | |
end | |
# clone development database | |
command "pg_dump --format=T -D -U #{user} #{from} | pg_restore --no-owner -d #{to} -U #{user}" | |
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 hidden or 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 | |
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