worktree

Public
The worktree management scripts with PostgreSQL database support
Used 1 time
Created by
d dpaluy

Usage
  - bin/worktree-setup [task_id] - Sets up new worktree with isolated test DB
  - bin/worktree-teardown - Drops test DB and prints removal command

Add custom config to the database.yml :

test:
  primary:
    <<: *default
    database: example_test<%= ENV.fetch('TEST_DB_SUFFIX', '') %>
  cache:
    <<: *default
    database: example_test_cache<%= ENV.fetch('TEST_DB_SUFFIX', '') %>
    migrations_paths: db/cache_migrate
  queue:
    <<: *default
    database: example_test_queue<%= ENV.fetch('TEST_DB_SUFFIX', '') %>
    migrations_paths: db/queue_migrate
  cable:
    <<: *default
    database: example_test_cable<%= ENV.fetch('TEST_DB_SUFFIX', '') %>
    migrations_paths: db/cable_migrate

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/x7msaq"
Template Source

Review the code before running this template on your machine.

run "touch bin/worktree-setup"

inject_into_file "bin/worktree-setup" do <<~EOF
#!/bin/bash
set -e

# Use task ID if provided, otherwise generate random suffix
if [ -n "$1" ]; then
  SUFFIX="$1"
else
  SUFFIX=$(LC_ALL=C tr -dc 'a-z0-9' < /dev/urandom | head -c 5)
fi

# Get main repo path from worktree
MAIN_REPO=$(git worktree list --porcelain | grep -m1 '^worktree ' | cut -d' ' -f2)

# Copy master.key from main repo
if [ -f "$MAIN_REPO/config/master.key" ]; then
  cp "$MAIN_REPO/config/master.key" config/master.key
  echo "Copied master.key from main repo"
fi

# Copy .env from main repo
if [ -f "$MAIN_REPO/.env" ]; then
  grep -v '^TEST_DB_SUFFIX=' "$MAIN_REPO/.env" > .env || true
  echo "Copied .env from main repo"
else
  touch .env
fi

echo "TEST_DB_SUFFIX=_${SUFFIX}" >> .env
bin/rails db:test:prepare
EOF
end

run "chmod +x bin/worktree-setup"
  
run "touch bin/worktree-teardown"
  
inject_into_file "bin/worktree-teardown" do <<~EOF
#!/bin/bash
set -eu

if [ "${RAILS_ENV:-}" = "production" ]; then
  echo "ERROR: Cannot run teardown in production environment"
  exit 1
fi

# Get main repo path from worktree
MAIN_REPO=$(git worktree list --porcelain | grep -m1 '^worktree ' | cut -d' ' -f2)
WORKTREE_PATH=$(pwd)

# Get database name from Rails
DB_NAME=$(bin/rails runner -e test "puts ActiveRecord::Base.configurations.find_db_config(:test).database")

echo "Worktree teardown:"
echo "  Database: $DB_NAME"
echo "  Path: $WORKTREE_PATH"
echo ""

# Drop the database
if psql -d postgres -t -c "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'" | grep -q 1; then
  echo "Dropping database $DB_NAME..."
  dropdb --if-exists -- "$DB_NAME"
  echo "Database dropped."
else
  echo "Database $DB_NAME does not exist, skipping."
fi

# Navigate out and remove worktree
echo ""
echo "To complete teardown, run from main repo:"
echo "  cd $MAIN_REPO && git worktree remove $WORKTREE_PATH"
EOF
end 

run "chmod +x bin/worktree-teardown"
Comments

Sign up or Login to leave a comment.