refactor(store/postgres): split adapter by domain and extract shared helpers#58
Merged
Conversation
…helpers Break the 1584-line store.go into per-domain files (principal, role, provisioning, identity, oidc, token) plus shared helpers tx.go, errors.go, codec.go, clone.go, validation.go. CreateProvisioningRule, UpdateProvisioningRule, ProvisionIdentity, and CreateRegistration now use a generic withTx helper instead of duplicated Begin/Rollback/Commit blocks. ProvisionIdentity's unique-violation race recovery uses a package-private sentinel so the post-rollback read happens after withTx unwinds; LinkIdentity and the passkey-registration link path share a single findIdentityLink. The public surface stays exactly NewStore/*Store/Migrate. Test files mirror the new layout with per-domain TestStoreSatisfiesXContracts assertion bundles; store_integration_test.go is untouched. Every unexported helper gains a one-line godoc; inline comments name the migration advisory lock, transaction scopes, the credential-binding-pinned WHERE clause as a security invariant, the malformed-hash rejection in findToken, and the race-recovery path in ProvisionIdentity. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
store/postgres/store.go(1584 lines, 6 fused domains) into per-domain files:principal.go,role.go,provisioning.go,identity.go,oidc.go,token.go, plus shared helperstx.go(genericwithTx),errors.go(isPostgresCode),codec.go(JSONB encode/decode),clone.go(defensive copies),validation.go(string validators / dedup).withTx[T any]collapses four repeatedBegin/defer Rollback/Commitblocks (CreateProvisioningRule, UpdateProvisioningRule, ProvisionIdentity, passkey CreateRegistration). ProvisionIdentity uses a package-private sentinelerrProvisionIdentityConflictso the post-rollback read for race recovery happens afterwithTxunwinds.findIdentityLink(was a method on*Storeand a free function inpasskey.go) into one free function shared byLinkIdentity,resolveIdentityLinkConflict, andlinkPasskeyIdentity.TestStoreSatisfiesXContractscompile-time port-assertion bundles;store_integration_test.gois untouched.UpdateCredentialAfterLoginas a security invariant, the malformed-hash rejection infindToken, the cross-principal rebind rejection, and the race-recovery path inProvisionIdentity.go doc ./store/postgresshows exactlyfunc Migrate(...),type Store struct{ ... },func NewStore(...). Behavior unchanged.This is PR C of a three-PR storage-package refactor; PR A (
internal/storetest, #56) and PR B (store/memory, #57) have both landed.Test plan
moon run root:check --summary minimal(vet, lint, build, format, unit tests)moon run root:integration(Testcontainers Postgres suite)go doc ./store/postgresmatchesmastergit diff --check(no whitespace damage)store.godropped from 1584 → 68 lines; heaviest production file is nowpasskey.goat 499 lines (was 486; +13 from added godocs andwithTxadoption)🤖 Generated with Claude Code