Skip to content

DBを分離する - PostgreSQL移行設計書#114

Merged
notfolder merged 5 commits intomainfrom
copilot/separate-db-for-task-info
Nov 30, 2025
Merged

DBを分離する - PostgreSQL移行設計書#114
notfolder merged 5 commits intomainfrom
copilot/separate-db-for-task-info

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 30, 2025

  • リポジトリの構造を理解する
  • 現在のデータベース実装(SQLite)を分析する
  • PostgreSQLへの移行とSQLAlchemy導入の詳細設計書を作成する
  • 設計書をmarkdown形式で出力する
  • マイグレーション関連の記述を削除する(要件更新)
  • コードレビューを実施する
  • フィードバックに基づき設計書を簡素化する
  • レビューコメントに従い設計書を更新する
    • TaskモデルをDBTaskに改名
    • TaskDBをTaskDBManagerに改名
    • task_keyをjsonから各フィールドに分解
    • task_key分解フィールドのインデックスを追加
    • DBTaskモデルにget_task_key()メソッドを追加
    • TaskDBManagerにget_task_by_key()メソッドを追加
    • update系メソッドを廃止し、save_task()メソッドに変更
  • 追加のレビューコメントに従い設計書を更新する
    • project_idをString(255)からInteger型に変更
    • create_taskメソッドの説明を拡充(DBへのINSERT処理であることを明記)
    • create_task_from_task(task: Task)メソッドを追加
    • テスト設計の章を削除
Original prompt

This section details on the original issue you should resolve

<issue_title>DBを分離する</issue_title>
<issue_description>task情報のDBをpostgresにしたい
SQLAlchemyでDBアクセスを汎用的に出来るようにしておいてください。
修正箇所について詳細設計を日本語でしてください。

上記の様な仕様を日本語で詳細設計してください。設計はプログラムを書くのではなく、日本語で詳細を記述してください。
markdown形式で仕様を出力して。実装スケジュールは必要ありません。
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ith SQLAlchemy

Co-authored-by: notfolder <20558197+notfolder@users.noreply.github.com>
Copilot AI changed the title [WIP] Update task information database to PostgreSQL DBを分離する - PostgreSQL移行設計書 Nov 30, 2025
Copilot AI requested a review from notfolder November 30, 2025 00:44
Copy link
Copy Markdown
Owner

@notfolder notfolder left a comment

Choose a reason for hiding this comment

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

全体的にtaskモデルのみにして単純化、レポジトリ無しで単純な DBアクセスロジッククラスを作って


### 2.1 アーキテクチャ設計

#### 2.1.1 レイヤー構成
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

出来るだけ簡素に実装して下さい。3層でなくても良く、tasksのテーブルクラス1ファイルだけで読みやすく実装出来ませんか?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

1ファイル(db/task_db.py)にTaskモデルとTaskDBクラスをまとめる構成に変更しました (5504583)。


#### 現在のデータベース構成
- **タスク情報DB**: SQLite(`contexts/tasks.db`)
- **メッセージ履歴**: JSONLファイル(`messages.jsonl`、`current.jsonl`)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

対象はtask DBのみで

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

対象をtasks DBのみに限定しました (5504583)。

- 接続URLの構築とバリデーションを行う

**設定項目**:
| 環境変数名 | config.yaml キー | 説明 | デフォルト値 |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

デフォルトはpostgresにして下さい

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

デフォルトをPostgreSQLに変更しました (5504583)。


### 2.3 モデル設計

#### 2.3.1 Baseクラス(db/models/base.py)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

DBモデルは一つのファイルにまとめて、taskテーブルだけで良い
taskの操作をするロジッククラスを一つ用意して使う設計にして

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

db/task_db.py 1ファイルにTaskモデルとTaskDBロジッククラスをまとめる構成に変更しました (5504583)。

| ix_tasks_user | user | ユーザー別検索の高速化 |
| ix_tasks_task_source_owner_repo | task_source, owner, repo | リポジトリ別検索の高速化 |

### 2.4 リポジトリ設計
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

リポジトリも不要

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

リポジトリパターンを廃止し、TaskDBロジッククラスに変更しました (5504583)。

- ファイルパスは `context_storage.base_dir` と `database.sqlite.path` から構築する

### 4.2 段階的移行
以下の手順で段階的に移行を進めることを推奨する:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

移行は不要です

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

段階的移行の章を削除しました (5504583)。

| test_db_config.py | db/config.py | 設定読み込みのテスト |
| test_db_connection.py | db/connection.py | 接続管理のテスト |
| test_task_model.py | db/models/task.py | Taskモデルのテスト |
| test_task_repository.py | db/repositories/task_repository.py | TaskRepositoryのテスト |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

リポジトリは不要

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

リポジトリ関連のテストを削除しました (5504583)。

### 7.1 バックアップ
PostgreSQLの標準ツール(pg_dump)を使用したバックアップ手順を提供する。

### 7.2 モニタリング
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

この章は不要

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

運用考慮事項の章を削除しました (5504583)。

### 7.3 セキュリティ
以下のセキュリティ対策を実施する:
- パスワードは環境変数で管理する(設定ファイルには記載しない)
- SSL接続を推奨する
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

sslは不要

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

SSL関連の記述を削除しました (5504583)。


---

## 9. リスクと対策
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

この章は不要

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

リスクと対策の章を削除しました (5504583)。

@notfolder
Copy link
Copy Markdown
Owner

@copilot 全体的にtaskモデルのみにして単純化、レポジトリ無しで単純な DBアクセスロジッククラスを作って

… class

Co-authored-by: notfolder <20558197+notfolder@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 30, 2025

@copilot 全体的にtaskモデルのみにして単純化、レポジトリ無しで単純な DBアクセスロジッククラスを作って

設計書を簡素化しました (5504583)。

  • 1ファイル(db/task_db.py)にTaskモデルとTaskDBクラスをまとめる構成に変更
  • リポジトリパターンを廃止し、単純なDBアクセスロジッククラスに変更
  • 対象をtasks DBのみに限定

Copy link
Copy Markdown
Owner

@notfolder notfolder left a comment

Choose a reason for hiding this comment

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

@copilot レビュー内容に従って修正して。
検討事項としては、task_idの代わりにtask_keyを分解したフィールドに出来るか、無理がありそうならtask_keyという名前に変えて、jsonbにして

Comment thread docs/spec/DB_SEPARATION_SPECIFICATION.md
|-----------|------|--------|------|
| create_task | task_data: dict | Task | 新規タスクを作成する |
| get_task | uuid: str | Task または None | UUIDでタスクを取得する |
| update_task | uuid: str, updates: dict | bool | タスクを更新する |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

更新用 DBtaskオブジェクトを受け取るので良くない?
他のupdateは不要では?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

update系メソッドを廃止し、save_task(db_task: DBTask)でDBTaskオブジェクトを受け取って保存する方式に変更しました (c24a844)。

| owner | String(255) | VARCHAR(255) | NOT NULL | リポジトリオーナー |
| repo | String(255) | VARCHAR(255) | NOT NULL | リポジトリ名 |
| task_type | String(50) | VARCHAR(50) | NOT NULL | タスクタイプ |
| task_id | String(50) | VARCHAR(50) | NOT NULL | タスクID |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

これを機にtaskkeyをjsonから各フィールドに分解したい
githubとgitlabの違いとissueと mergerequestの違いを吸収できる構成を検討して。無理がなさそうなら、分解したフィールドにDBのスキーマを変えて。
無理がありそうなら、task_keyをjsonbにして

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

task_keyを各フィールドに分解しました (c24a844)。GitHub/GitLab、Issue/PullRequest/MergeRequestの違いを吸収できる構成:

  • task_source: github/gitlab
  • task_type: issue/pull_request/merge_request
  • owner, repo: GitHub用(GitLabはNULL)
  • project_id: GitLab用(GitHubはNULL)
  • number: タスク番号


### 2.2 task_db.py の設計

#### 2.2.1 Task モデル
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Taskオブジェクトと区別するため、 DBTaskとして下さい

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

DBTaskに改名しました (c24a844)。

| ix_tasks_created_at | created_at | 作成日時順ソートの高速化 |
| ix_tasks_user | user | ユーザー別検索の高速化 |

#### 2.2.2 TaskDB クラス
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

TaskDBManagerという名前にして

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TaskDBManagerに改名しました (c24a844)。

| メソッド名 | 引数 | 戻り値 | 説明 |
|-----------|------|--------|------|
| create_task | task_data: dict | Task | 新規タスクを作成する |
| get_task | uuid: str | Task または None | UUIDでタスクを取得する |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

get_taskのtask_key版も欲しい

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

get_task_by_key(task_key: TaskKey)メソッドを追加しました (c24a844)。

…_key fields

Co-authored-by: notfolder <20558197+notfolder@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 30, 2025

@copilot レビュー内容に従って修正して。
検討事項としては、task_idの代わりにtask_keyを分解したフィールドに出来るか、無理がありそうならtask_keyと...

設計書を更新しました (c24a844)。

  • TaskモデルをDBTaskに改名
  • TaskDBをTaskDBManagerに改名
  • task_keyを各フィールドに分解(task_source, task_type, owner, repo, project_id, number)
  • task_key分解フィールドのインデックスを追加
  • DBTaskにget_task_key()メソッド、TaskDBManagerにget_task_by_key()メソッドを追加
  • update系メソッドを廃止し、save_task(db_task)に変更

Copy link
Copy Markdown
Owner

@notfolder notfolder left a comment

Choose a reason for hiding this comment

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

@copilot レビューに従って修正して

| task_type | String(50) | VARCHAR(50) | NOT NULL | タスクタイプ(issue/pull_request/merge_request) |
| owner | String(255) | VARCHAR(255) | NULL可 | GitHubリポジトリオーナー |
| repo | String(255) | VARCHAR(255) | NULL可 | GitHubリポジトリ名 |
| project_id | String(255) | VARCHAR(255) | NULL可 | GitLabプロジェクトID |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

gitlabのproject_idは数値型にしてください

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

project_idをInteger型に変更しました (746f7bf)。


| メソッド名 | 引数 | 戻り値 | 説明 |
|-----------|------|--------|------|
| create_task | task_data: dict | DBTask | 新規タスクを作成する |
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

タスク作成とともに DBに新しいタスクレコードをinsertする旨明記
taskオブジェクト版もあった方が良い

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

create_taskの説明を「新規タスクを作成し、DBにINSERTする」に変更し、create_task_from_task(task: Task)メソッドを追加しました (746f7bf)。

|--------------|------|------|
| test_task_db.py | db/task_db.py | TaskDBManagerクラスのテスト |

### 5.2 テスト用データベース
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

この章は不要

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

テスト設計の章を削除しました (746f7bf)。

…ve test chapter

Co-authored-by: notfolder <20558197+notfolder@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 30, 2025

@copilot レビューに従って修正して

レビューに従って修正しました (746f7bf)。

  • project_idをInteger型に変更
  • create_taskの説明を拡充(DBへのINSERT処理であることを明記)
  • create_task_from_task(task: Task)メソッドを追加
  • テスト設計の章を削除

Copilot AI requested a review from notfolder November 30, 2025 12:35
@notfolder notfolder marked this pull request as ready for review November 30, 2025 12:38
@notfolder notfolder merged commit 09e954b into main Nov 30, 2025
@notfolder notfolder deleted the copilot/separate-db-for-task-info branch November 30, 2025 12:39
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.

DBを分離する

2 participants