-
Notifications
You must be signed in to change notification settings - Fork 0
経歴書ドラフトPDF生成の非同期タスク化(ADR-0020) #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
60 changes: 60 additions & 0 deletions
60
backend/alembic_migrations/versions/0047_add_resume_draft_cache_table.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """経歴書ドラフト生成の非同期タスク用キャッシュテーブルを追加する(ADR-0018 / 非同期化) | ||
|
|
||
| - resume_draft_cache: ユーザーごとに最新のドラフト生成 1 件(状態 + 生成 payload)を保持する | ||
|
|
||
| 新規テーブル作成のみ(op.create_table)で、既存テーブルの再作成は伴わない。 | ||
| FK は users を親に持つ。``resumes`` テーブルとは無関係(確定した職務経歴書とは別ドメイン)。 | ||
|
|
||
| Revision ID: 0047_add_resume_draft_cache_table | ||
| Revises: 0046_add_manifest_path_to_github_skill_evidence | ||
| Create Date: 2026-07-05 00:00:00.000000 | ||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| revision: str = "0047_add_resume_draft_cache_table" | ||
| down_revision: Union[str, None] = "0046_add_manifest_path_to_github_skill_evidence" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| "resume_draft_cache", | ||
| sa.Column("id", sa.String(length=36), primary_key=True), | ||
| sa.Column( | ||
| "user_id", | ||
| sa.String(length=36), | ||
| sa.ForeignKey("users.id"), | ||
| nullable=False, | ||
| unique=True, | ||
| ), | ||
| sa.Column("result", sa.JSON(), nullable=True), | ||
| sa.Column( | ||
| "status", sa.String(length=20), nullable=False, server_default="completed" | ||
| ), | ||
| sa.Column("error_message", sa.Text(), nullable=True), | ||
| sa.Column("retry_count", sa.Integer(), nullable=False, server_default="0"), | ||
| sa.Column("max_retries", sa.Integer(), nullable=False, server_default="3"), | ||
| sa.Column("started_at", sa.DateTime(timezone=True), nullable=True), | ||
| sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True), | ||
| sa.Column( | ||
| "created_at", | ||
| sa.DateTime(timezone=True), | ||
| server_default=sa.func.now(), | ||
| nullable=False, | ||
| ), | ||
| sa.Column( | ||
| "updated_at", | ||
| sa.DateTime(timezone=True), | ||
| server_default=sa.func.now(), | ||
| nullable=False, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_table("resume_draft_cache") |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """経歴書ドラフト生成キャッシュ(``ResumeDraftCache``)のデータアクセス。 | ||
|
|
||
| ``ResumeDraftCache`` はユーザーあたり 1 件のレコードで、``user_id`` を一意境界とする。 | ||
| 取得・作成クエリを本リポジトリへ集約し、router / handler / task_runner からの直クエリ散在を防ぐ。 | ||
| ``user_id`` スコープは IDOR 防止の認可境界であり、1 箇所に閉じ込めることで条件追加時の漏れを防ぐ | ||
| (``GitHubLinkCacheRepository`` と同形)。 | ||
| """ | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy.exc import IntegrityError | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from ..models import ResumeDraftCache | ||
|
|
||
|
|
||
| class ResumeDraftCacheRepository: | ||
| """ユーザーの経歴書ドラフト生成キャッシュの読み取り・作成。 | ||
|
|
||
| セッションはコンストラクタで受け取る。ドラフト生成の実行経路では libSQL の | ||
| idle stream timeout 対策でフェーズごとにセッションを開閉するため、本リポジトリは | ||
| セッションを保持せず呼び出し側が渡したものをそのまま使う。 | ||
| """ | ||
|
|
||
| def __init__(self, db: Session): | ||
| self.db = db | ||
|
|
||
| def get_by_user(self, user_id: str) -> ResumeDraftCache | None: | ||
| """ユーザーのキャッシュを取得する。存在しなければ ``None``。""" | ||
| return self.db.scalar( | ||
| select(ResumeDraftCache).where(ResumeDraftCache.user_id == user_id) | ||
| ) | ||
|
|
||
| def get_or_create(self, user_id: str) -> ResumeDraftCache: | ||
| """ユーザーのキャッシュを取得し、存在しなければ作成して flush する。 | ||
|
|
||
| 並列リクエストが ``user_id`` の一意制約で衝突した場合は rollback して再取得する。 | ||
| 再 SELECT が ``None`` を返したら ``RuntimeError`` を上げて non-Optional な戻り値契約を守る | ||
| (.claude/rules/backend/database.md「IntegrityError 後の再 SELECT は None を判定する」)。 | ||
| """ | ||
| cache = self.get_by_user(user_id) | ||
| if cache is not None: | ||
| return cache | ||
|
|
||
| cache = ResumeDraftCache(user_id=user_id) | ||
| self.db.add(cache) | ||
| try: | ||
| self.db.flush() | ||
| except IntegrityError: | ||
| self.db.rollback() | ||
| existing = self.get_by_user(user_id) | ||
| if existing is None: | ||
| raise RuntimeError( | ||
| f"ResumeDraftCache の作成と再取得に失敗しました (user_id={user_id})" | ||
| ) from None | ||
| return existing | ||
| return cache |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.