Skip to content

refactor(apphost): extract WithClearDatabaseCommand into MongoDbResourceBuilderExtensions#262

Merged
mpaulosky merged 1 commit into
devfrom
squad/259-extract-withcleardatabasecommand
May 8, 2026
Merged

refactor(apphost): extract WithClearDatabaseCommand into MongoDbResourceBuilderExtensions#262
mpaulosky merged 1 commit into
devfrom
squad/259-extract-withcleardatabasecommand

Conversation

@mpaulosky

Copy link
Copy Markdown
Owner

Summary

Extracts the inline WithCommand clear-data block from AppHost.cs into a new MongoDbResourceBuilderExtensions class.

Changes

  • New: src/AppHost/MongoDbResourceBuilderExtensions.cs — contains WithMongoDbDevCommands public entry point and private WithClearDatabaseCommand
  • Simplified: src/AppHost/AppHost.cs — reduced from ~157 lines to ~30 lines; single mongo.WithMongoDbDevCommands("myblog") call

Testing

All 10 existing tests pass:

  • 5 unit tests in MongoDbClearCommandTests
  • 5 integration tests in MongoClearDataIntegrationTests

Closes #259
Working as Sam (Backend/.NET)

…rceBuilderExtensions

Moves the 120-line inline WithCommand clear-data block into a new
MongoDbResourceBuilderExtensions class. AppHost.cs is reduced to a
single mongo.WithMongoDbDevCommands("myblog") call.

- All 10 existing tests (5 unit + 5 integration) pass unchanged
- Command name 'clear-myblog-data' preserved exactly
- Semaphore, IsRunMode guard, and error handling preserved exactly

Closes #259

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 8, 2026 16:15
@github-actions github-actions Bot added the squad Squad triage inbox — Lead will assign to a member label May 8, 2026
@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

🏗️ PR Added to Squad Triage Queue

This PR has been labeled with squad and added to the triage queue.

Next steps:

  • The squad Lead will review and assign to an appropriate team member
  • A squad:member label will be added after triage

If you know which squad member should handle this, you can add the appropriate squad:member label yourself.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the AppHost MongoDB “clear data” operator command by extracting the inline WithCommand logic from AppHost.cs into a dedicated MongoDbResourceBuilderExtensions extension, keeping the AppHost bootstrap minimal while preserving the existing operator command behavior.

Changes:

  • Added MongoDbResourceBuilderExtensions with WithMongoDbDevCommands(databaseName) to register the MongoDB dev/operator commands in run mode.
  • Simplified AppHost.cs to a single mongo.WithMongoDbDevCommands("myblog") call and removed inline command implementation/imports.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/AppHost/MongoDbResourceBuilderExtensions.cs New extension class that encapsulates MongoDB dev/operator command registration and clear-data handler logic.
src/AppHost/AppHost.cs Removes inline clear-data command block and replaces it with a single extension method call.

Comment on lines +20 to +31
// Shared semaphore — one per process; all commands share the same mutex.
private static readonly SemaphoreSlim _clearMutex = new(1, 1);

public static IResourceBuilder<MongoDBServerResource> WithMongoDbDevCommands(
this IResourceBuilder<MongoDBServerResource> builder,
string databaseName)
{
if (!builder.ApplicationBuilder.ExecutionContext.IsRunMode)
return builder;

builder.WithClearDatabaseCommand(databaseName);
return builder;
Comment on lines +39 to +43
"clear-myblog-data",
"⚠️ Clear MyBlog Data",
executeCommand: async context =>
{
// AC2: Non-blocking acquire — return immediately if another clear is already in flight.
@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

Test Results Summary

314 tests  ±0   313 ✅ ±0   19s ⏱️ ±0s
  6 suites ±0     1 💤 ±0 
  6 files   ±0     0 ❌ ±0 

Results for commit 1686a0b. ± Comparison against base commit 0dc7e88.

@codecov

codecov Bot commented May 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.38843% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.48%. Comparing base (0dc7e88) to head (1686a0b).

Files with missing lines Patch % Lines
src/AppHost/MongoDbResourceBuilderExtensions.cs 93.38% 7 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #262      +/-   ##
==========================================
+ Coverage   83.01%   84.48%   +1.47%     
==========================================
  Files          43       44       +1     
  Lines         730      851     +121     
  Branches      112      114       +2     
==========================================
+ Hits          606      719     +113     
- Misses         81       88       +7     
- Partials       43       44       +1     
Files with missing lines Coverage Δ
src/AppHost/MongoDbResourceBuilderExtensions.cs 93.38% <93.38%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mpaulosky
mpaulosky merged commit 8571bf3 into dev May 8, 2026
23 checks passed
@mpaulosky
mpaulosky deleted the squad/259-extract-withcleardatabasecommand branch May 8, 2026 16:24
mpaulosky added a commit that referenced this pull request May 8, 2026
## Sprint 18 — AppHost MongoDB Dev Commands Refactor

This release promotes all Sprint 18 work from `dev` to `main`.

### What's included

#### Feature work
- **#262** — `refactor(apphost): extract WithClearDatabaseCommand into
MongoDbResourceBuilderExtensions`
- **#263** — `feat(AppHost): add WithSeedDataCommand for local dev
seeding`
- **#264** — `feat(AppHost): add WithShowStatsCommand for local dev
stats`
- **#267** — `refactor: rename _clearMutex to _dbMutex in
MongoDbResourceBuilderExtensions`

#### CI fixes
- **#270** — `fix(ci): Blog → README Sync — push to dev instead of main`
- **#271** — `fix(ci): add pre-flight token validation and fix
permissions block in squad-mark-released`

### CI status

- ✅ **Squad CI** (`ci.yml`) — green on latest `dev` commit
- ⚠️ **Test Suite** — 1 flaky test failure: `SeedMyBlogData Concurrent
Invocations Allow Only One Run` (timing-sensitive concurrency test; race
condition in test harness, not production code). All other 47 tests
pass. Squad CI gate is the authoritative pass/fail gate.

### Notes

- Last release tag: `v1.5.0`
- `dev` is 55 commits ahead of `main`
- No open Sprint 18 issues, no open PRs
- Merge strategy: squash merge per playbook

---

## Release Checklist

- [ ] Latest `dev` Squad CI is green ✅
- [ ] Release scope reviewed (Sprint 18 milestone closed, all 4 issues
merged)
- [ ] Breaking changes documented (none — additive AppHost extension
methods only)
- [ ] Flaky test acknowledged (`SeedMyBlogData Concurrent` — timing race
in test, not prod)
- [ ] Release notes drafted (see sprint summary above)
- [ ] Aragorn approval received
- [ ] Boromir confirms branch state and workflow health
- [ ] PR CI passes before merge
- [ ] Merge to `main` with squash merge
- [ ] Tag `main` with appropriate `vX.Y.Z` after merge + CI green

Closes the Sprint 18 release gate.
Working as Boromir (DevOps)

---------

Co-authored-by: Boromir <boromir@squad.dev>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
mpaulosky added a commit that referenced this pull request Jul 2, 2026
#422)

…rceBuilderExtensions (#262)

## Summary

Extracts the inline `WithCommand` clear-data block from `AppHost.cs`
into a new `MongoDbResourceBuilderExtensions` class.

## Changes

- **New**: `src/AppHost/MongoDbResourceBuilderExtensions.cs` — contains
`WithMongoDbDevCommands` public entry point and private
`WithClearDatabaseCommand`
- **Simplified**: `src/AppHost/AppHost.cs` — reduced from ~157 lines to
~30 lines; single `mongo.WithMongoDbDevCommands("myblog")` call

## Testing

All 10 existing tests pass:
- 5 unit tests in `MongoDbClearCommandTests`
- 5 integration tests in `MongoClearDataIntegrationTests`

Closes #259
Working as Sam (Backend/.NET)

Co-authored-by: Boromir <boromir@squad.dev>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>##
Summary

<!-- Describe what this PR does and why. Link the issue it closes. -->

Closes #<!-- issue number -->

## Type of Change

<!-- Check all that apply -->

- [ ] 🐛 Bug fix (non-breaking change that fixes an issue)
- [ ] ✨ Feature (non-breaking change that adds functionality)
- [ ] ♻️ Refactor (no behavior change, code cleanup/restructure)
- [ ] 🧪 Tests (new or updated tests only)
- [ ] 📝 Docs (README, XML docs, comments)
- [ ] ⚙️ Infra/CI (GitHub Actions, Aspire, NuGet, deployment)
- [ ] 🔒 Security (auth, permissions, secrets, headers)
- [ ] 💥 Breaking change (existing behavior changes)

## Domain Affected

<!-- Check all that apply — this determines which reviewers are required
-->

- [ ] 🏗️ Architecture / domain logic / CQRS → **Aragorn required**
- [ ] 🔧 Backend (handlers, repositories, API endpoints, MediatR) → **Sam
required**
- [ ] ⚛️ Frontend (Blazor components, Razor pages, CSS, JS) → **Legolas
required**
- [ ] 🧪 Unit / bUnit / integration tests → **Gimli required**
- [ ] 🧪 E2E / Playwright / Aspire integration tests → **Pippin
required**
- [ ] ⚙️ CI/CD / GitHub Actions / NuGet / Aspire AppHost → **Boromir
required**
- [ ] 🔒 Auth0 / authorization / security-relevant changes → **Gandalf
required**
- [ ] 📝 Docs / README / XML docs → **Frodo required**

## Self-Review Checklist

<!-- Complete before requesting review — incomplete PRs will be returned
-->

### Code Quality

- [ ] I ran `dotnet build MyBlog.slnx --configuration Release` — 0
errors, 0 warnings
- [ ] I ran `dotnet test MyBlog.slnx --configuration Release --no-build`
— all pass
- [ ] No TODO/FIXME left unless tracked in a follow-up issue (link it)
- [ ] No secrets, API keys, or credentials committed

### Architecture

- [ ] New handlers follow the `Command`/`Query`/`Handler`/`Validator`
naming conventions
- [ ] New handlers are `sealed`
- [ ] Domain layer has no references to `Web` or `Persistence.*`
projects
- [ ] `Result<T>` / `ResultErrorCode` used for expected failures (no
exception-driven control flow)
- [ ] DTOs are records in `Domain.DTOs`; Models are in `Domain.Models`
- [ ] No DTO types embedded in Model classes

### Tests

- [ ] New code has corresponding unit tests
- [ ] Integration tests use domain-specific collections
(`[Collection("XxxIntegration")]`)
- [ ] No test compares two `IssueDto.Empty` / `CommentDto.Empty`
instances directly

### Security (check if security-relevant)

- [ ] New endpoints have appropriate `RequireAuthorization` / policy
applied
- [ ] No `MarkupString` used with user-supplied content
- [ ] No user input reflected in MongoDB queries without sanitization

### Merge Readiness

- [ ] Branch is up to date with `main` (no merge conflicts)
- [ ] CI checks are green (do not request review while checks are
pending/failing)
- [ ] PR description is complete — reviewers should not have to ask what
this does

## Screenshots / Evidence

<!-- For UI changes: before/after screenshots. For fixes: evidence the
bug is resolved. -->

## Notes for Reviewers

<!-- Anything you want reviewers to pay special attention to, or context
they need. -->

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Boromir <boromir@squad.dev>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: GitHub Copilot <copilot@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Boromir <lead-organizer@squad.local>
Co-authored-by: mpaulosky <mpaulosky@users.noreply.github.com>
mpaulosky added a commit that referenced this pull request Jul 4, 2026
#427)

…rceBuilderExtensions (#262)

## Summary

Extracts the inline `WithCommand` clear-data block from `AppHost.cs`
into a new `MongoDbResourceBuilderExtensions` class.

## Changes

- **New**: `src/AppHost/MongoDbResourceBuilderExtensions.cs` — contains
`WithMongoDbDevCommands` public entry point and private
`WithClearDatabaseCommand`
- **Simplified**: `src/AppHost/AppHost.cs` — reduced from ~157 lines to
~30 lines; single `mongo.WithMongoDbDevCommands("myblog")` call

## Testing

All 10 existing tests pass:
- 5 unit tests in `MongoDbClearCommandTests`
- 5 integration tests in `MongoClearDataIntegrationTests`

Closes #259
Working as Sam (Backend/.NET)

Co-authored-by: Boromir <boromir@squad.dev>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>##
Summary

<!-- Describe what this PR does and why. Link the issue it closes. -->

Closes #<!-- issue number -->

## Type of Change

<!-- Check all that apply -->

- [ ] 🐛 Bug fix (non-breaking change that fixes an issue)
- [ ] ✨ Feature (non-breaking change that adds functionality)
- [ ] ♻️ Refactor (no behavior change, code cleanup/restructure)
- [ ] 🧪 Tests (new or updated tests only)
- [ ] 📝 Docs (README, XML docs, comments)
- [ ] ⚙️ Infra/CI (GitHub Actions, Aspire, NuGet, deployment)
- [ ] 🔒 Security (auth, permissions, secrets, headers)
- [ ] 💥 Breaking change (existing behavior changes)

## Domain Affected

<!-- Check all that apply — this determines which reviewers are required
-->

- [ ] 🏗️ Architecture / domain logic / CQRS → **Aragorn required**
- [ ] 🔧 Backend (handlers, repositories, API endpoints, MediatR) → **Sam
required**
- [ ] ⚛️ Frontend (Blazor components, Razor pages, CSS, JS) → **Legolas
required**
- [ ] 🧪 Unit / bUnit / integration tests → **Gimli required**
- [ ] 🧪 E2E / Playwright / Aspire integration tests → **Pippin
required**
- [ ] ⚙️ CI/CD / GitHub Actions / NuGet / Aspire AppHost → **Boromir
required**
- [ ] 🔒 Auth0 / authorization / security-relevant changes → **Gandalf
required**
- [ ] 📝 Docs / README / XML docs → **Frodo required**

## Self-Review Checklist

<!-- Complete before requesting review — incomplete PRs will be returned
-->

### Code Quality

- [ ] I ran `dotnet build MyBlog.slnx --configuration Release` — 0
errors, 0 warnings
- [ ] I ran `dotnet test MyBlog.slnx --configuration Release --no-build`
— all pass
- [ ] No TODO/FIXME left unless tracked in a follow-up issue (link it)
- [ ] No secrets, API keys, or credentials committed

### Architecture

- [ ] New handlers follow the `Command`/`Query`/`Handler`/`Validator`
naming conventions
- [ ] New handlers are `sealed`
- [ ] Domain layer has no references to `Web` or `Persistence.*`
projects
- [ ] `Result<T>` / `ResultErrorCode` used for expected failures (no
exception-driven control flow)
- [ ] DTOs are records in `Domain.DTOs`; Models are in `Domain.Models`
- [ ] No DTO types embedded in Model classes

### Tests

- [ ] New code has corresponding unit tests
- [ ] Integration tests use domain-specific collections
(`[Collection("XxxIntegration")]`)
- [ ] No test compares two `IssueDto.Empty` / `CommentDto.Empty`
instances directly

### Security (check if security-relevant)

- [ ] New endpoints have appropriate `RequireAuthorization` / policy
applied
- [ ] No `MarkupString` used with user-supplied content
- [ ] No user input reflected in MongoDB queries without sanitization

### Merge Readiness

- [ ] Branch is up to date with `main` (no merge conflicts)
- [ ] CI checks are green (do not request review while checks are
pending/failing)
- [ ] PR description is complete — reviewers should not have to ask what
this does

## Screenshots / Evidence

<!-- For UI changes: before/after screenshots. For fixes: evidence the
bug is resolved. -->

## Notes for Reviewers

<!-- Anything you want reviewers to pay special attention to, or context
they need. -->

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Boromir <boromir@squad.dev>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: GitHub Copilot <copilot@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Boromir <lead-organizer@squad.local>
Co-authored-by: mpaulosky <mpaulosky@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

squad Squad triage inbox — Lead will assign to a member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract WithClearDatabaseCommand into MongoDbResourceBuilderExtensions

2 participants