Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Drop worktree setup patches now carried by spree-starter (#14391)

Damian Legawiec4c8437a35db0
diff --git a/scripts/worktree/lib.sh b/scripts/worktree/lib.sh
index cce5050a21..6d0320f536 100755
--- a/scripts/worktree/lib.sh
+++ b/scripts/worktree/lib.sh
@@ -7,10 +7,45 @@
set -euo pipefail
TEMPLATE_DB="spree_worktree_template"
-PG_ARGS=(-h localhost -p "${DATABASE_PORT:-5432}" -U "${DATABASE_USERNAME:-postgres}")
+# Same defaults as the starter's config/database.yml, so the psql tools and
+# Rails always agree on which server they are talking to. teardown runs after
+# the worktree (and its bundled Rails) is gone, so these cannot come from Rails.
+PG_ARGS=(-h "${DATABASE_HOST:-localhost}" -p "${DATABASE_PORT:-5432}" -U "${DATABASE_USERNAME:-postgres}")
worktree_root() { git rev-parse --show-toplevel; }
+# Both pieces below arrived in spree/spree-starter#1376. A clone predating it
+# passes provisioning but then either shares the default database or rejects
+# every proxied request, so check both before touching anything.
+# Renders database.yml with sentinel values and applies the host rule, rather
+# than grepping for tokens: a comment, or the same token in the wrong YAML
+# entry, would otherwise vouch for a config that still points at the defaults.
+# Ruby only — no Rails boot, so this runs before `bundle install`.
+require_current_starter() {
+ local missing=()
+ local report
+
+ report=$(cd server && DATABASE_NAME=__dev_probe__ DATABASE_NAME_TEST=__test_probe__ ruby -ryaml -rerb -e '
+ config = YAML.safe_load(ERB.new(File.read("config/database.yml")).result, aliases: true)
+ puts "dev" if config.dig("development", "database") == "__dev_probe__"
+ puts "test" if config.dig("test", "database") == "__test_probe__"
+ puts "host" if File.read("config/environments/development.rb")
+ .then { |src| src[/config\.hosts\s*<<\s*(\S+localhost\S+)/, 1] }
+ &.then { |rule| eval(rule) =~ "feature-x.spree.localhost:1355" }
+ ' 2>/dev/null) || true
+
+ grep -qx dev <<<"$report" || missing+=("config/database.yml: the development entry ignores DATABASE_NAME — worktrees would share spree_development")
+ grep -qx test <<<"$report" || missing+=("config/database.yml: the test entry ignores DATABASE_NAME_TEST — worktrees would share spree_test")
+ grep -qx host <<<"$report" || missing+=("config/environments/development.rb: no host rule accepting proxied .localhost names — Rails would block every request")
+
+ if [ ${#missing[@]} -gt 0 ]; then
+ echo "This server/ clone predates spree-starter#1376:" >&2
+ printf ' - %s\n' "${missing[@]}" >&2
+ echo "Refresh it: rm -rf server && re-run scripts/worktree/setup.sh" >&2
+ return 1
+ fi
+}
+
branch_name() { echo "${WT_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}"; }
# Readable prefix plus a hash of the full branch name: normalization alone maps
diff --git a/scripts/worktree/make-template.sh b/scripts/worktree/make-template.sh
index 7c896d433b..01c27ade8d 100755
--- a/scripts/worktree/make-template.sh
+++ b/scripts/worktree/make-template.sh
@@ -9,19 +9,32 @@ cd "$(worktree_root)"
[ -d server ] || { echo "server/ missing — run pnpm server:create (or scripts/worktree/setup.sh) first." >&2; exit 1; }
-if ! grep -q 'DATABASE_NAME' server/config/database.yml; then
- sed -i '' \
- -e 's|database: spree_development|database: <%= ENV.fetch("DATABASE_NAME") { "spree_development" } %>|' \
- -e 's|database: spree_test|database: <%= ENV.fetch("DATABASE_NAME_TEST") { "spree_test" } %>|' \
- server/config/database.yml
+require_current_starter
+
+(cd server && bundle install --quiet)
+
+# Prove Rails resolves DATABASE_NAME before dropping anything: this script
+# rebuilds from scratch, so a config that ignored it would destroy the
+# developer's own spree_development database. Needs the bundle above, and
+# keeps stderr so a boot failure reports itself instead of looking like a
+# resolution mismatch.
+resolved=$(cd server && DATABASE_NAME="$TEMPLATE_DB" \
+ bin/rails runner 'print ActiveRecord::Base.connection_db_config.database') || {
+ echo "Could not read the database configuration from server/ — see the error above." >&2
+ exit 1
+}
+if [ "$resolved" != "$TEMPLATE_DB" ]; then
+ echo "Rails resolved the development database to '$resolved', not '$TEMPLATE_DB'." >&2
+ echo "Refusing to rebuild — check server/config/database.yml and server/.env." >&2
+ exit 1
fi
echo "▸ Rebuilding $TEMPLATE_DB (migrations + seeds, ~2-3 min)"
-dropdb "${PG_ARGS[@]}" --if-exists "$TEMPLATE_DB"
-createdb "${PG_ARGS[@]}" "$TEMPLATE_DB"
-(cd server && bundle install --quiet && \
- DATABASE_NAME="$TEMPLATE_DB" DATABASE_NAME_TEST="$TEMPLATE_DB" \
- bin/rails spree:install:migrations db:prepare)
+# db:drop/db:create rather than dropdb/createdb: Rails connects using the config
+# just verified, so the destructive step cannot reach a different host or port
+# than the name check covered.
+(cd server && DATABASE_NAME="$TEMPLATE_DB" DATABASE_NAME_TEST="$TEMPLATE_DB" \
+ bin/rails db:drop db:create spree:install:migrations db:prepare)
echo "✓ $TEMPLATE_DB ready — new worktrees copy it via createdb -T"
diff --git a/scripts/worktree/setup.sh b/scripts/worktree/setup.sh
index 8b4928f5ce..3a86e763ca 100755
--- a/scripts/worktree/setup.sh
+++ b/scripts/worktree/setup.sh
@@ -26,24 +26,7 @@ DATABASE_NAME_TEST=$(test_db_name)
EOF
fi
-# The starter hardcodes database names; make them env-driven (defaults keep the
-# stock behavior). server/ is gitignored so the patch is invisible to git.
-# TODO: upstream this to spree-starter 6-0-dev, then drop the sed.
-if ! grep -q 'DATABASE_NAME' server/config/database.yml; then
- sed -i '' \
- -e 's|database: spree_development|database: <%= ENV.fetch("DATABASE_NAME") { "spree_development" } %>|' \
- -e 's|database: spree_test|database: <%= ENV.fetch("DATABASE_NAME_TEST") { "spree_test" } %>|' \
- server/config/database.yml
-fi
-
-# The Host header arrives from the portless proxy with its port attached
-# (foo.spree.localhost:1355 when the proxy isn't on 443), which Rails' default
-# ".localhost" rule rejects — allow .localhost hosts with or without a port.
-if [ ! -f server/config/initializers/worktree_hosts.rb ]; then
- cat > server/config/initializers/worktree_hosts.rb <<'RUBY'
-Rails.application.config.hosts << /\A[a-z0-9-]+(\.[a-z0-9-]+)*\.localhost(:\d+)?\z/i
-RUBY
-fi
+require_current_starter
if ! db_exists "$(db_name)"; then
if ! db_exists "$TEMPLATE_DB"; then