refactor: Repository内トランザクションをrunInTransaction/getClient経由に統一#127
Draft
Mel-906 wants to merge 7 commits intofeature/unit-of-workfrom
Draft
refactor: Repository内トランザクションをrunInTransaction/getClient経由に統一#127Mel-906 wants to merge 7 commits intofeature/unit-of-workfrom
Mel-906 wants to merge 7 commits intofeature/unit-of-workfrom
Conversation
複数の集約をまたぐDB操作をトランザクションで安全にまとめるための仕組みを追加。 AsyncLocalStorageによりRepository側のコード変更なしでトランザクションを共有できる。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ient にリネーム 生成・取得しているのはDBそのものではなくDrizzleクライアントであるため、 実態に合った命名に修正。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
PgTransactionはPgDatabaseを継承しているため、DrizzleClientを PgDatabase<NodePgQueryResultHKT, typeof schema>で定義することで DB接続とトランザクションを同じ型として扱えるようになった。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
|
CICDも通せると嬉しいです |
Member・DiscordAccount・KarteのRepository.save()でdb.transaction(tx)を 直接使っていた箇所をrunInTransaction + getClient()に置き換え、 UoWトランザクションが開始されていればそれに乗るようにした。 EventRepository.persistEvent()はトランザクションなしだった5つのDB操作を runInTransaction()で囲み、集約単位の原子性を確保した。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
b74198a to
44cd950
Compare
Author
eb138a9 to
a90e539
Compare
…tory-transaction # Conflicts: # src/infrastructure/drizzle/client.ts
…re/uow-repository-transaction # Conflicts: # src/infrastructure/drizzle/DrizzleDiscordAccountRepository.ts # src/infrastructure/drizzle/DrizzleEventRepository.ts # src/infrastructure/drizzle/DrizzleKarteRepository.ts # src/infrastructure/drizzle/DrizzleMemberRepository.ts # src/infrastructure/drizzle/client.ts
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
PR1(#126)で導入したUoW基盤(
runInTransaction/getClient)を活用し、各Repositoryのトランザクション処理を統一します。変更内容
DrizzleMemberRepository.save()db.transaction(tx => ...)でtxを直接操作runInTransaction(() => { getClient()... })DrizzleDiscordAccountRepository.save()DrizzleKarteRepository.save()DrizzleEventRepository.persistEvent()runInTransaction()で囲み原子性を確保背景
db.transaction(tx)パターンでは、UseCaseレベルのUnitOfWork.run()で開始したトランザクションに乗ることができない。runInTransaction+getClient()に統一することで、UoWトランザクション内で呼ばれた場合はそれに乗り、単独で呼ばれた場合は自前でトランザクションを開始するpersistEvent()は event upsert → 不要exhibit削除 → exhibit upsert → memberEvents同期 → memberExhibits同期 の5操作がトランザクションなしで実行されており、途中失敗時にデータ不整合が起きうる状態だった確認
テストは現在未実行です。挑戦してみます
🤖 Generated with Claude Code