@@ -55,13 +55,25 @@ cmd_add() {
5555 local wt_dir=" ${2:- $REPO_ROOT / ../ backend-$slug } "
5656
5757 load_db_env " $SRC_ENV "
58- local new_db=" ${DB_NAME} __${slug} "
59- [ " ${# new_db} " -le 63 ] || die " db name '$new_db ' exceeds PG 63-char limit"
58+ # PG identifier limit is 63 chars; pytest-django auto-creates test_<dbname>,
59+ # so reserve 5 more for that prefix.
60+ local db_prefix=" ${DB_NAME} __"
61+ local max_db_slug=$(( 63 - 5 - ${# db_prefix} ))
62+ [ " $max_db_slug " -ge 10 ] \
63+ || die " DATABASE_NAME '$DB_NAME ' leaves <10 chars for a worktree slug"
64+ local db_slug=" $slug "
65+ if [ " ${# db_slug} " -gt " $max_db_slug " ]; then
66+ local hash ; hash=$( printf ' %s' " $branch " | shasum | cut -c1-6)
67+ db_slug=" ${db_slug: 0: $((max_db_slug - 7))} _${hash} "
68+ fi
69+ local new_db=" ${db_prefix}${db_slug} "
6070 [ " $new_db " != " $DB_NAME " ] || die " computed db name collides with source"
6171
6272 echo " → worktree dir : $wt_dir "
6373 echo " → branch : $branch "
6474 echo " → database : $new_db "
75+ [ " $db_slug " = " $slug " ] \
76+ || echo " (slug truncated for PG 63-char limit: $slug → $db_slug )"
6577
6678 if [ -d " $wt_dir " ]; then
6779 echo " worktree dir already exists — skipping git worktree add"
@@ -119,7 +131,10 @@ cmd_remove() {
119131
120132 pg dropdb --if-exists " $DB_NAME "
121133 pg dropdb --if-exists " test_$DB_NAME "
122- git -C " $REPO_ROOT " worktree remove " $wt_dir "
134+ # --force: envfile/.env.local is tracked but rewritten with a per-worktree
135+ # DATABASE_NAME on add, and the tree may contain .venv/__pycache__/coverage
136+ # artifacts. The y/N prompt above is the safety gate.
137+ git -C " $REPO_ROOT " worktree remove --force " $wt_dir "
123138}
124139
125140main () {
0 commit comments