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.

Keep the worktree template in step with the gem migrations

Damian Legawieca5f2a73aa55c
diff --git a/CLAUDE.md b/CLAUDE.md
index 66c73969c9..4d3479dd44 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -140,6 +140,7 @@ One-time machine setup: Homebrew `postgresql@18` running on :5432 (with a `postg
|---|---|
| Ruby code in `spree/*` gems | Nothing — path gems, reloads on next request |
| New migration in a gem | `cd server && bin/rails spree:install:migrations db:migrate`; then `pnpm wt:template` once so future worktrees inherit it |
+| Deleted and re-cloned `server/` | `pnpm wt:template` — the 6.0 migrations still ship from the gems, so re-cloning renumbers them and the old template no longer matches. `wt:setup` refuses to provision from a mismatched template and rebuilds it automatically when it re-created `server/` itself |
| Gem dependencies | `cd server && bundle install` (the gem home is shared across worktrees, so this is fast) |
| Need sample data (products + images) | `cd server && bin/rails spree:load_sample_data` — per worktree, on demand; takes minutes and hits the network |
| Rails console / database | `cd server && bin/rails console`; the DB is `spree_dev_<branch>` on `localhost:5432` |
diff --git a/scripts/worktree/lib.sh b/scripts/worktree/lib.sh
index 08b30dc03a..2aa5cb3842 100755
--- a/scripts/worktree/lib.sh
+++ b/scripts/worktree/lib.sh
@@ -205,3 +205,30 @@ warn_unless_mailpit() {
echo " ! Mailpit is not running — email will fail to deliver."
echo " brew install mailpit && brew services start mailpit"
}
+
+# Versions the template database has recorded but the primary checkout has no
+# migration file for. Any output means the two have drifted apart.
+#
+# The 6.0 migrations still live in the monorepo gems rather than the starter, and
+# `spree:install:migrations` stamps each copy with the time it runs — Rails' own
+# behaviour, and the right one while those migrations are unreleased and still
+# being reshaped in place. The consequence is that re-creating server/ renumbers
+# every gem migration, so a template built before that renumbering records
+# versions nobody can produce a file for any more. A worktree copied from it then
+# re-runs those migrations under their new numbers against tables they already
+# created, and fails on a duplicate table minutes into provisioning.
+#
+# Compares the primary's files, not the calling worktree's: setup.sh syncs the
+# primary's migrate directory into every new worktree, so the primary is the one
+# that has to agree with the template.
+template_drift() {
+ local primary_root="$1"
+
+ db_exists "$TEMPLATE_DB" || return 0
+ [ -d "$primary_root/server/db/migrate" ] || return 0
+
+ comm -23 \
+ <(psql "${PG_ARGS[@]}" -d "$TEMPLATE_DB" -tAc \
+ "SELECT version FROM schema_migrations ORDER BY 1" 2>/dev/null | tr -d '[:blank:]\r' | grep . | sort) \
+ <(ls "$primary_root/server/db/migrate" | cut -d_ -f1 | sort)
+}
diff --git a/scripts/worktree/make-template.sh b/scripts/worktree/make-template.sh
index f13f447d18..c005a05fb0 100755
--- a/scripts/worktree/make-template.sh
+++ b/scripts/worktree/make-template.sh
@@ -32,15 +32,25 @@ if [ "$resolved" != "$TEMPLATE_DB" ]; then
exit 1
fi
-echo "▸ Rebuilding $TEMPLATE_DB (migrations + seeds, ~2-3 min)"
+echo "▸ Rebuilding $TEMPLATE_DB (full migration run + seeds, ~4-5 min)"
# 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.
+#
+# db:migrate, not db:prepare. The starter commits a db/schema.rb, so db:prepare
+# would load that snapshot into the empty database and then run only the
+# migrations newer than its version — meaning the starter's own migrations never
+# execute here, and the 6.0 migrations still living in the monorepo gems are the
+# only ones actually exercised. Migrating from empty runs the whole set in order,
+# so a migration that is broken, mis-ordered or depends on something an earlier
+# one no longer creates fails while building the template rather than during
+# somebody's upgrade.
+#
# ADMIN_EMAIL/ADMIN_PASSWORD are required explicitly since 6.0 — the seed no
# longer ships dummy defaults. These are the documented worktree credentials.
(cd server && DATABASE_NAME="$TEMPLATE_DB" DATABASE_NAME_TEST="$TEMPLATE_DB" \
ADMIN_EMAIL="[email protected]" ADMIN_PASSWORD="spree123" \
- bin/rails db:drop db:create spree:install:migrations db:prepare)
+ bin/rails db:drop db:create spree:install:migrations db:migrate db:seed)
echo "✓ $TEMPLATE_DB ready — new worktrees copy it via createdb -T"
diff --git a/scripts/worktree/setup.sh b/scripts/worktree/setup.sh
index b7c4cf3eed..d46da4d944 100755
--- a/scripts/worktree/setup.sh
+++ b/scripts/worktree/setup.sh
@@ -38,6 +38,7 @@ setup_storefront() {
echo "▸ Provisioning worktree '$(branch_slug)' (db: $(db_name))"
primary_root=$(git worktree list --porcelain | head -1 | sed 's/^worktree //')
+created_primary_server=0
if [ ! -d server ]; then
echo "▸ Cloning spree-starter into server/"
@@ -55,6 +56,12 @@ if [ ! -d server ]; then
if [ "$primary_root" != "$(pwd)" ] && [ -d "$primary_root/server/db/migrate" ]; then
rsync -a --delete "$primary_root/server/db/migrate/" server/db/migrate/
[ -f "$primary_root/server/db/schema.rb" ] && cp "$primary_root/server/db/schema.rb" server/db/schema.rb
+ else
+ # This IS the primary's server/, freshly cloned, so the renumbering above
+ # has just happened to the migration set every other worktree copies. Any
+ # existing template was built against the old numbers and is now unusable;
+ # mark it so, and let the rebuild below replace it.
+ created_primary_server=1
fi
fi
@@ -106,6 +113,26 @@ fi
require_current_starter
+# Never copy a template that no longer matches the migrations this worktree is
+# about to run: the mismatch would otherwise surface as a duplicate-table error
+# from db:migrate several minutes in, long after the database, the gems and the
+# node modules have been installed.
+drifted=$(template_drift "$primary_root")
+if [ -n "$drifted" ] && ! db_exists "$(db_name)"; then
+ if [ "$created_primary_server" = 1 ]; then
+ # We caused the drift moments ago by re-cloning this checkout's server/, so
+ # rebuild rather than sending the developer away to run a script we could
+ # have run ourselves.
+ echo "▸ server/ was re-created, so $TEMPLATE_DB no longer matches — rebuilding it"
+ "$(dirname "${BASH_SOURCE[0]}")/make-template.sh"
+ else
+ echo "Template database '$TEMPLATE_DB' is out of date: it records $(echo "$drifted" | grep -c .) migrations that no longer exist in $primary_root/server/db/migrate." >&2
+ echo "This happens when that checkout's server/ is re-created, which renumbers the gem migrations." >&2
+ echo "Rebuild it, then re-run this script: pnpm wt:template" >&2
+ exit 1
+ fi
+fi
+
if ! db_exists "$(db_name)"; then
if ! db_exists "$TEMPLATE_DB"; then
echo "Template database '$TEMPLATE_DB' missing — run scripts/worktree/make-template.sh first." >&2