Add those files to bin folder.
Usage:
bin/worktree-setup [task_id]- Sets up new worktree with isolated test DBbin/worktree-teardown- Drops test DB and prints removal command
Add those files to bin folder.
Usage:
bin/worktree-setup [task_id] - Sets up new worktree with isolated test DBbin/worktree-teardown - Drops test DB and prints removal command| 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 |
| #!/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 |
| #!/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" |