Skip to content

Instantly share code, notes, and snippets.

@bensheldon
Created August 17, 2025 15:20
Show Gist options
  • Save bensheldon/788d0b8a8f94890f3d444d20dcca8891 to your computer and use it in GitHub Desktop.
Save bensheldon/788d0b8a8f94890f3d444d20dcca8891 to your computer and use it in GitHub Desktop.
<% require_relative File.expand_path("git_worktree", __dir__) %>
default: &default
adapter: postgresql
encoding: unicode
pool: 20
connect_timeout: 5
checkout_timeout: 5
development:
<<: *default
database: myapp_development<%= GitWorktree.db_suffix %>
test:
<<: *default
database: myapp_test<%= GitWorktree.db_suffix %><%= ENV["TEST_ENV_NUMBER"] %>
production:
<<: *default
database: myapp_production
# config/git_worktree.rb
module GitWorktree
PROJECT_ROOT = File.expand_path("..", __dir__)
def self.name
git_path = File.join(PROJECT_ROOT, ".git")
git_dir = nil
if File.file?(git_path)
contents = File.read(git_path, 1024)
if contents && contents =~ /\Agitdir:\s*(.+)\s*\z/
git_dir = Regexp.last_match(1).strip
end
elsif File.directory?(git_path)
git_dir = git_path
end
return nil unless git_dir&.include?("/worktrees/")
raw = git_dir.split("/worktrees/").last.split("/").first
sanitized = raw.to_s.gsub(/[^a-zA-Z0-9_]/, "_").downcase
sanitized.empty? ? nil : sanitized
end
def self.db_suffix
worktree = name
worktree ? "_#{worktree}" : ""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment