Skip to content

Attach fields orphaned by an earlier seed run, not just new ones - #66

Merged
duguankui merged 1 commit into
mainfrom
seed-attach-new-fields
Sep 6, 2026
Merged

Attach fields orphaned by an earlier seed run, not just new ones#66
duguankui merged 1 commit into
mainfrom
seed-attach-new-fields

Conversation

@duguankui

Copy link
Copy Markdown
Member

The bug you found

#65 added about_highlights/about_milestones and claimed they were verified — against a throwaway database only. Against a real, already-migrated database (this repo's own dev Host.db), the seed's own idempotency worked against it: GetOrCreateContentTypeAsync found the about content type already present and returned it untouched, so both fields got created and then sat attached to nothing.

Why the straightforward "attach fields created this run" didn't work either

That was my first attempt, and it doesn't cover this exact database: GetOrCreateFieldAsync found the fields already existed, created by an earlier run of the old code before this content-type-append logic existed at all. "Created this run" can't see a field that predates the run.

The fix

GetOrCreateContentTypeAsync now computes, once at the start of SeedAsync, every field id already attached to any content type - and appends a requested usage only when the field is attached to none. A field with zero usages anywhere is specific enough to this failure mode to touch automatically for a local dev convenience seed: a real admin's normal workflow attaches a new field to something immediately, and a field still in use elsewhere (even a different content type than this seed asks for) is left alone either way.

New usages are appended after the current highest Order, so an existing arrangement someone has deliberately ordered isn't reshuffled underneath them.

Test plan

Against this repo's own dev Host.db, in the exact broken state the report described (backed up first):

  • Before: about_highlights/about_milestones exist as fields, about's content type has only title/body
  • After one run: both now attached at Order 2/3
  • After a second run: still exactly 4 fields, no duplicate appends
  • A from-scratch database still gets all four fields in one pass
  • Full solution build (0 errors) and dotnet test for Domain (252) and EntityFrameworkCore (317)

…w ones

#65 added about_highlights/about_milestones but the fix only worked on a
genuinely fresh database. GetOrCreateContentTypeAsync found the About content
type already present (this repo's own dev Host.db has run every prior version
of this seed) and returned it untouched, so both fields were created and then
left attached to nothing - reported after merge, from exactly that database.

The seed's own idempotency-by-name is what caused it: GetOrCreateFieldAsync
found the fields already existed - created by an earlier run of the OLD code,
before this content-type-append logic existed at all - so tracking "created
this run" (what the first attempt at this fix did) cannot see them; they
predate the run entirely.

Replaced that with "attached to no content type anywhere", computed once at
the start of SeedAsync over every existing content type and kept current as
this run attaches more. A field with zero usages anywhere is specific enough
to this bug to touch automatically for a *local dev convenience* seed - a
real admin's normal workflow attaches a new field somewhere immediately, and
a field still in use elsewhere is left alone either way. New usages are
appended after the current highest Order rather than at their declared
position, so a deliberate arrangement is not reshuffled.

Verified against this repo's own dev Host.db, in the exact broken state the
report described: the two fields moved from unattached to Order 2/3 on
About, a second run made no further change, and a from-scratch database
still gets all four fields in one pass.
@duguankui
duguankui merged commit 0c4a10c into main Sep 6, 2026
1 check passed
@duguankui
duguankui deleted the seed-attach-new-fields branch September 6, 2026 03:05
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