Skip to content

feat: Allow worktrees to share DB snapshots - #8623

Closed
tyler36 wants to merge 2 commits into
ddev:mainfrom
tyler36:20260727-restore-worktree-dbs
Closed

tyler36 wants to merge 2 commits into
ddev:mainfrom
tyler36:20260727-restore-worktree-dbs

Conversation

@tyler36

@tyler36 tyler36 commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

The Issue

  • Fixes #REPLACE_ME_WITH_RELATED_ISSUE_NUMBER

Recently watched git-worktree-contributor-training which uses ddev export-db and ddev import-db via a top-level .tarballs directory:

.
├── .tarballs
├── d11simple
└── fancy-feat1

While this works, ddev snapshot restore can be faster and has a nice TUI to browse snapshots. It would be nice if we could reuse this when using worktree workflow.

How This PR Solves The Issue

This update enables database snapshots to be shared across git worktrees, allowing users to restore snapshots from corresponding project paths in other worktrees.

It attempts to detect if the project is a worktree and displays snapshots from other worktrees via TUI. Selecting a snapshot from a different worktree, copies the file locally before importing to maintain correct mounting paths.

Projects that are NOT a worktree are unchanged.

Manual Testing Instructions

"snapshot restore" lists worktree snapshots

  1. Clone a base repository ("d11simple")
git clone [email protected]:ddev/d11simple
cd d11simple
  1. Get "d11simple" site to working state
ddev start
ddev drush si -y
ddev launch
# Confirm Drupal welcome page is displayed.
  1. Create "d11simple" snapshot
$ ddev snapshot
...
Restore this snapshot with 'ddev snapshot restore d11simple_20260727184413'
  1. Create "feat-95" git worktree
git worktree add ../feat-95
cd ../feat-95
  1. Confirm "feat-95" worktree does NOT have initialized database
ddev launch
# Confirm page loads Drupal setup wizard
  1. Attempt to load "d11simple" snapshot created earlier (d11simple_20260727184413)
$ ddev snapshot restore
Use the arrow keys to navigate: ↓ ↑ → ←
Snapshot:
  ▸ d11simple_20260727184413
...
Database snapshot d11simple_20260727184413 was restored in 15s
  1. Confirm "feat-95" now loads with populated database
ddev launch
# Confirm DDEV welcome screen is displayed
  1. Confirm worktree in sub-folders work. Eg. git worktree add ../feat/new-67

"snapshot restore" does NOT see non-worktree snapshots at same level

Assuming the following file structure:

.
├── d11simple
└── feat-95
  1. Create clone at same level as "d11simple" (fake)
$ ls
d11simple  feat-95

$ git clone [email protected]:ddev/d11simple fake
...
Updating files: 100% (25351/25351), done.

$ ls
d11simple  fake  feat-95
  1. Attempt to load "d11simple" snapshot created earlier (d11simple_20260727184413)
$ ddev snapshot restore
No snapshots found for project fake
  1. Create "fake" snapshot
$ ddev snapshot
...
Restore this snapshot with 'ddev snapshot restore fake_20260727190329'
  1. Confirm snapshot is not visible from "d11simple"
cd ../d11simple
ddev snapshot restore
Snapshot:
  ▸ d11simple_20260727184413

Automated Testing Overview

Introduced tests to verify that the snapshot restoration process includes targets from other worktrees.

Release/Deployment Notes

No additional deployment steps are required, but ensure that users are aware of the new functionality regarding worktree snapshot sharing.

@tyler36
tyler36 requested a review from a team as a code owner July 27, 2026 11:12
@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

@tyler36
tyler36 marked this pull request as draft July 27, 2026 11:57
@rfay

rfay commented Jul 27, 2026

Copy link
Copy Markdown
Member

You may want to also think about how

affects your use-case (please review it)

Also think about testing how symlinks in db_snapshots might possibly work (I don't think they would, but take a look)

@weitzman

weitzman commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Also think about testing how symlinks in db_snapshots might possibly work (I don't think they would, but take a look)

Why do you suspect they would not work. Seems like an interesting solution to me.

An entirely different solution is to have the worktrees point to the DB service of the parent. I know that can be chaotic for some use cases but for others its perfectly reasonable. It also saves you the restore time at the start of each new worktree.

@rfay

rfay commented Aug 8, 2026

Copy link
Copy Markdown
Member

Why do you suspect they would not work. Seems like an interesting solution to me.

The .ddev/db_snapshots is bind-mounted; I don't expect the bind mount to follow a host symlink.

@rfay

rfay commented Aug 23, 2026

Copy link
Copy Markdown
Member

Thanks for the good thinking here @tyler36 . Please take a look at #8705 (would appreciate your review). It may scratch this itch, as you can now ddev start --seed-snapshot=/path/to/snapshot --reset-database -y

I do think the strategy there (and additional mount) would make this work, and maybe the logic could be shared.

rfay added a commit to rfay/ddev that referenced this pull request Aug 25, 2026
…napshot restore`

## The Issue

Related to ddev#8623 ("Allow worktrees to share DB snapshots" by @tyler36): a
snapshot taken in one git worktree of a project isn't visible to
`ddev snapshot restore` in another worktree of the same repository, so
reusing a database across worktrees means manually copying snapshot
files around.

ddev#8623 predates the path-based snapshot restore/seed support that's
landed on `main` since it was opened (ddev#8704, ddev#8705), and its
`RestoreSnapshot(name, sourceRoot)` signature no longer matches
`RestoreSnapshot(name, force)` on `main` - it needs a rewrite on top of
current code either way. This is that rewrite, built directly on the
newer mechanism instead of duplicating it.

## How This PR Solves The Issue

`ddev snapshot restore` and `ddev snapshot restore --latest` already
support restoring from an arbitrary host path via `resolveSnapshotSource`.
This adds automatic discovery on top of that, rather than a parallel
restore mechanism:

- `DdevApp.ListSnapshotRestoreTargets()` lists the current project's own
  snapshots plus any snapshots found in the same project checked out in
  other worktrees of the same git repository (via `git worktree list`),
  newest first. A worktree match requires the same repository and the
  same relative project path - determined entirely from git and the
  filesystem, never from the `.ddev/config.yaml` project name (worktree
  projects don't usually share a name). A separate clone that happens to
  sit at the same relative path is not offered.
- `DdevApp.GetLatestSnapshotRestoreTarget()` extends `--latest` the same
  way.
- The interactive picker in `ddev snapshot restore` shows a worktree
  snapshot's name and source directory (`snapshot-name (worktree-dir)`);
  selecting one restores by its absolute path, reusing the existing
  path-based restore/bind-mount plumbing rather than a copy step.

I considered going further with a repository-agnostic shared snapshot
pool keyed by project name (so any two checkouts with the same name
could share snapshots, not just worktrees of one repo). I left that out:
project names collide too easily across unrelated projects, and a git
worktree relationship is a precise, verifiable identity check that a
name is not. The fully general case - "restore a snapshot from anywhere
on disk" - is already covered by the existing path argument.

## Manual Testing Instructions

1. Create a git repo and a worktree of it, each with a DDEV project at
   the same relative path (project names deliberately differ, since
   that's the normal case for worktrees):

   ```bash
   git init repo && cd repo && git commit --allow-empty -m init
   ddev config --project-type=php --project-name=repo-a
   ddev start
   ddev snapshot --name=shared-snapshot
   cd .. && git -C repo worktree add ../repo-b HEAD
   cd repo-b
   ddev config --project-type=php --project-name=repo-b
   ddev start
   ```

2. `ddev snapshot restore` in `repo-b` - the picker should list
   `shared-snapshot (repo)` alongside any of `repo-b`'s own snapshots.
   `ddev snapshot restore --latest` should restore it directly.
3. A separate `git clone` of `repo` at the same relative depth should
   *not* have its snapshots offered from `repo-b`.

## Automated Testing Overview

Covered in the following commit, along with a fix for a real bug found
while writing it.

## Release/Deployment Notes

No config or behavior changes for projects outside a git worktree.
Within a worktree, `ddev snapshot restore` and `--latest` now also see
snapshots from sibling worktrees of the same project; this is additive
and doesn't change what a bare snapshot name resolves to.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
rfay added a commit to rfay/ddev that referenced this pull request Aug 25, 2026
…napshot restore`

## The Issue

Related to ddev#8623 ("Allow worktrees to share DB snapshots" by @tyler36): a
snapshot taken in one git worktree of a project isn't visible to
`ddev snapshot restore` in another worktree of the same repository, so
reusing a database across worktrees means manually copying snapshot
files around.

ddev#8623 predates the path-based snapshot restore/seed support that's
landed on `main` since it was opened (ddev#8704, ddev#8705), and its
`RestoreSnapshot(name, sourceRoot)` signature no longer matches
`RestoreSnapshot(name, force)` on `main` - it needs a rewrite on top of
current code either way. This is that rewrite, built directly on the
newer mechanism instead of duplicating it.

## How This PR Solves The Issue

`ddev snapshot restore` and `ddev snapshot restore --latest` already
support restoring from an arbitrary host path via `resolveSnapshotSource`.
This adds automatic discovery on top of that, rather than a parallel
restore mechanism:

- `DdevApp.ListSnapshotRestoreTargets()` lists the current project's own
  snapshots plus any snapshots found in the same project checked out in
  other worktrees of the same git repository (via `git worktree list`),
  newest first. A worktree match requires the same repository and the
  same relative project path - determined entirely from git and the
  filesystem, never from the `.ddev/config.yaml` project name (worktree
  projects don't usually share a name). A separate clone that happens to
  sit at the same relative path is not offered.
- `DdevApp.GetLatestSnapshotRestoreTarget()` extends `--latest` the same
  way.
- The interactive picker in `ddev snapshot restore` shows a worktree
  snapshot's name and source directory (`snapshot-name (worktree-dir)`);
  selecting one restores by its absolute path, reusing the existing
  path-based restore/bind-mount plumbing rather than a copy step.

I considered going further with a repository-agnostic shared snapshot
pool keyed by project name (so any two checkouts with the same name
could share snapshots, not just worktrees of one repo). I left that out:
project names collide too easily across unrelated projects, and a git
worktree relationship is a precise, verifiable identity check that a
name is not. The fully general case - "restore a snapshot from anywhere
on disk" - is already covered by the existing path argument.

## Manual Testing Instructions

1. Create a git repo and a worktree of it, each with a DDEV project at
   the same relative path (project names deliberately differ, since
   that's the normal case for worktrees):

   ```bash
   git init repo && cd repo && git commit --allow-empty -m init
   ddev config --project-type=php --project-name=repo-a
   ddev start
   ddev snapshot --name=shared-snapshot
   cd .. && git -C repo worktree add ../repo-b HEAD
   cd repo-b
   ddev config --project-type=php --project-name=repo-b
   ddev start
   ```

2. `ddev snapshot restore` in `repo-b` - the picker should list
   `shared-snapshot (repo)` alongside any of `repo-b`'s own snapshots.
   `ddev snapshot restore --latest` should restore it directly.
3. A separate `git clone` of `repo` at the same relative depth should
   *not* have its snapshots offered from `repo-b`.

## Automated Testing Overview

Covered in the following commit, along with a fix for a real bug found
while writing it.

## Release/Deployment Notes

No config or behavior changes for projects outside a git worktree.
Within a worktree, `ddev snapshot restore` and `--latest` now also see
snapshots from sibling worktrees of the same project; this is additive
and doesn't change what a bare snapshot name resolves to.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@rfay

rfay commented Aug 27, 2026

Copy link
Copy Markdown
Member

Closing in favor of

which uses newer available snapshot techniques.

@rfay rfay closed this Aug 27, 2026
rfay added a commit to rfay/ddev that referenced this pull request Aug 27, 2026
…napshot restore`

## The Issue

Related to ddev#8623 ("Allow worktrees to share DB snapshots" by @tyler36): a
snapshot taken in one git worktree of a project isn't visible to
`ddev snapshot restore` in another worktree of the same repository, so
reusing a database across worktrees means manually copying snapshot
files around.

ddev#8623 predates the path-based snapshot restore/seed support that's
landed on `main` since it was opened (ddev#8704, ddev#8705), and its
`RestoreSnapshot(name, sourceRoot)` signature no longer matches
`RestoreSnapshot(name, force)` on `main` - it needs a rewrite on top of
current code either way. This is that rewrite, built directly on the
newer mechanism instead of duplicating it.

## How This PR Solves The Issue

`ddev snapshot restore` and `ddev snapshot restore --latest` already
support restoring from an arbitrary host path via `resolveSnapshotSource`.
This adds automatic discovery on top of that, rather than a parallel
restore mechanism:

- `DdevApp.ListSnapshotRestoreTargets()` lists the current project's own
  snapshots plus any snapshots found in the same project checked out in
  other worktrees of the same git repository (via `git worktree list`),
  newest first. A worktree match requires the same repository and the
  same relative project path - determined entirely from git and the
  filesystem, never from the `.ddev/config.yaml` project name (worktree
  projects don't usually share a name). A separate clone that happens to
  sit at the same relative path is not offered.
- `DdevApp.GetLatestSnapshotRestoreTarget()` extends `--latest` the same
  way.
- The interactive picker in `ddev snapshot restore` shows a worktree
  snapshot's name and source directory (`snapshot-name (worktree-dir)`);
  selecting one restores by its absolute path, reusing the existing
  path-based restore/bind-mount plumbing rather than a copy step.

I considered going further with a repository-agnostic shared snapshot
pool keyed by project name (so any two checkouts with the same name
could share snapshots, not just worktrees of one repo). I left that out:
project names collide too easily across unrelated projects, and a git
worktree relationship is a precise, verifiable identity check that a
name is not. The fully general case - "restore a snapshot from anywhere
on disk" - is already covered by the existing path argument.

## Manual Testing Instructions

1. Create a git repo and a worktree of it, each with a DDEV project at
   the same relative path (project names deliberately differ, since
   that's the normal case for worktrees):

   ```bash
   git init repo && cd repo && git commit --allow-empty -m init
   ddev config --project-type=php --project-name=repo-a
   ddev start
   ddev snapshot --name=shared-snapshot
   cd .. && git -C repo worktree add ../repo-b HEAD
   cd repo-b
   ddev config --project-type=php --project-name=repo-b
   ddev start
   ```

2. `ddev snapshot restore` in `repo-b` - the picker should list
   `shared-snapshot (repo)` alongside any of `repo-b`'s own snapshots.
   `ddev snapshot restore --latest` should restore it directly.
3. A separate `git clone` of `repo` at the same relative depth should
   *not* have its snapshots offered from `repo-b`.

## Automated Testing Overview

Covered in the following commit, along with a fix for a real bug found
while writing it.

## Release/Deployment Notes

No config or behavior changes for projects outside a git worktree.
Within a worktree, `ddev snapshot restore` and `--latest` now also see
snapshots from sibling worktrees of the same project; this is additive
and doesn't change what a bare snapshot name resolves to.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants