Skip to content

Add find_orphaned_dbs() / clean_testdb(orphaned=True) - #34

Merged
aersam merged 3 commits into
mainfrom
orphaned-testdb-cleanup
Sep 14, 2026
Merged

aersam merged 3 commits into
mainfrom
orphaned-testdb-cleanup

Conversation

@aersam

@aersam aersam commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • New find_orphaned_dbs(project_root=None): diffs this project's live git worktrees against actual Postgres DBs matching the project's name-slug prefix, returning DBs with no matching live worktree
  • clean_testdb() gains an orphaned=True flag (mutually exclusive with all=True)
  • CLI: pgdb testdb list-orphaned (dry-run) and pgdb testdb clean --orphaned
  • New [tool.pgdevkit].extra_db_suffixes config lets a consumer repo (e.g. CCMT2's _onetrade sibling DB) teach orphan detection about its own sibling DBs, without pgdevkit hardcoding repo-specific conventions
  • Mirrored into the MSSQL backend for engine parity

Test plan

  • ty check pgdevkit clean
  • pytest tests/ 184/184 pass (49 podman-gated skipped in this sandbox; called those functions directly against this sandbox's native Postgres for live end-to-end confidence, including detached-HEAD and manually-deleted-worktree scenarios)
  • /code-review caught and fixed two real bugs: detached-HEAD worktrees were misclassified as orphaned (would have dropped a live DB out from under a CI checkout); deduplicated LIKE-prefix-escaping logic shared between postgres/mssql backends

🤖 Generated with Claude Code

aersam and others added 3 commits September 14, 2026 19:14
Adds per-repo orphaned test-DB cleanup to pgdevkit, so a project can
find/drop Postgres test DBs left behind by branches whose git worktree
no longer exists, without a separate tool reimplementing the naming
algorithm.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Adds pgdevkit.testdb.find_orphaned_dbs() and clean_testdb(orphaned=True)
(plus `pgdb testdb list-orphaned` / `clean --orphaned`), so a project can
find/drop Postgres (and MSSQL) test DBs whose branch no longer has a live
git worktree, without a separate tool (e.g. bdt) reimplementing pgdevkit's
naming algorithm to do the same thing.

- naming.py: live_worktree_branches() parses `git worktree list
  --porcelain`, treating a detached-HEAD worktree as branch "HEAD" (matching
  current_branch()'s own literal output for one) so CI-style detached
  checkouts aren't misreported as orphaned; expected_db_names() maps live
  branches to the DB names they own.
- config.py: new `[tool.pgdevkit].extra_db_suffixes` lets a repo teach
  orphan detection about its own sibling DBs (e.g. CCMT2's "_onetrade")
  without pgdevkit needing to know why they exist.
- api.py / mssql/api.py: shared `_dbs_with_prefix` helper (used by both
  `clean_testdb(all=True)` and the new orphaned path); clean_testdb() gains
  an `orphaned` flag, mutually exclusive with `all`.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@aersam
aersam force-pushed the orphaned-testdb-cleanup branch from eed9948 to 11d1dd3 Compare September 14, 2026 17:15
@aersam
aersam marked this pull request as ready for review September 14, 2026 17:34
@aersam
aersam merged commit 12d594b into main Sep 14, 2026
2 checks passed
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.

1 participant