Skip to content

feat: add wt and db CLI tools for worktree and database branching#915

Draft
gudnuf wants to merge 3 commits intomasterfrom
tools/worktree-db-cli
Draft

feat: add wt and db CLI tools for worktree and database branching#915
gudnuf wants to merge 3 commits intomasterfrom
tools/worktree-db-cli

Conversation

@gudnuf
Copy link
Contributor

@gudnuf gudnuf commented Mar 4, 2026

Summary

  • wt CLI for managing git worktrees with per-branch Supabase databases
  • db CLI for database branching — fork, switch, list, delete, link, status
image

@vercel
Copy link

vercel bot commented Mar 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agicash Ready Ready Preview, Comment Mar 4, 2026 11:09pm

Request Review

@supabase
Copy link

supabase bot commented Mar 4, 2026

This pull request has been ignored for the connected project hrebgkfhjpkbxpztqqke because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

2. **Switch**: `ALTER DATABASE postgres RENAME TO wt_<old>; ALTER DATABASE wt_<new> RENAME TO postgres;` — since services hardcode `postgres`, renaming transparently switches which branch they see
3. **Restart**: Bounce services to reconnect (~5 seconds)

This gives full isolation with a single Supabase stack. The only trade-off: one DB branch is "active" at a time.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too bad that we can't work on more than one in parallel but this is still pretty useful I'd say

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now that fully separate supabase stack for this is possible but v2. I like that idea so one can pick the level of isolation needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which plugin did you use to generate this file? I used superpowers but it didn't create me a brainstorm doc, just a plan.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you committed these files. It made it lot easier for me to review

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need tsconfig for this?

tools/db.ts Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would maybe create a folder just for this tool and put all its files in there to separate from other tools. then I'd probably put lock and state files there too to reduce num of top level files (there's already so many of them which makes it harder to find things there)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you committed these files. It made it lot easier for me to review

2. **Switch**: `ALTER DATABASE postgres RENAME TO wt_<old>; ALTER DATABASE wt_<new> RENAME TO postgres;` — since services hardcode `postgres`, renaming transparently switches which branch they see
3. **Restart**: Bounce services to reconnect (~5 seconds)

This gives full isolation with a single Supabase stack. The only trade-off: one DB branch is "active" at a time.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now that fully separate supabase stack for this is possible but v2. I like that idea so one can pick the level of isolation needed

└── db.ts # Database branching CLI entry point (commander)
```

**Worktree location:** `.trees/` at project root (already in use for manual worktrees, gitignored via `.git/info/exclude` — move to `.gitignore`). `.claude/worktrees/` is a separate system managed by Claude Code and is not touched by `wt`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't we use the same folder? my understanding is that claude is also just wrapping git worktrees so should be the same

3. Terminate any remaining connections to `postgres` AND `wt_<target>`
↳ ALTER DATABASE RENAME fails if any connections exist.
Supabase stop handles most, but stray connections (dev servers, psql sessions) may linger.
4. RENAME postgres → wt_<temp_sentinel> -- Step A
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know why is this temp_sentinel needed?


### Dev Server Port Configuration

`app/server.ts` currently hardcodes port 3000. Modify to read from `PORT` env var:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to be done yet

fi
```

This replaces the current `bun run db:generate-types` hook entry with a script that handles branching.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does it replace it? I am not sure I understand. the devenv.nix still has:

git-hooks.hooks.generate-db-types = {
    enable = true;
    name = "Generate database types from local db";
    entry = "bun run db:generate-types";
  };

- [ ] `wt list` shows accurate worktree table
- [ ] Failed setups clean up after themselves

### Phase 4: Integration — Dev Server, Hooks, devenv
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this now. This was not implemented yet right?

…eedback

- Move wt/db CLI files into tools/worktree-db/ subfolder to reduce
  top-level clutter (reviewer feedback from josip)
- Fix generate-db-types.sh to write to temp file before mv (prevents
  types file truncation on failure)
- Move readState() inside lock acquisition for db fork and wt setup
  to prevent stale-state race conditions
- Add input validation guard in postgres.ts databaseExists() and
  terminateConnections()
- Add try/catch around JSON.parse in readState() with helpful error
- Remove dead exports: getDbPort(), getMainPort()
- Deduplicate git worktree list --porcelain call in wt list
- Remove checkDbBranchAlignment() from app/server.ts (wrong layer)
- Keep PORT env var reading in app/server.ts
- Add .trees/ to .gitignore
- Add implementation notes section to plan doc
- Update devenv.nix paths for restructured tools
- Use bunx for supabase CLI in hook script (not on system PATH)
Worktrees need the full .env (secrets, API keys), not just a PORT
override. Copy the root .env and replace the PORT line. Also add
dotenv/config import to server.ts for .env loading.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants