@@ -161,13 +161,26 @@ end
161161
162162### フェーズ2: データ移行
163163
164- #### 1. Git履歴からの日付抽出スクリプト(参考実装を活用)
164+ #### 重要: YAMLファイルがマスターデータ
165+
166+ ** db/dojos.yaml がマスターレコードであることに注意** :
167+ - データベースの変更だけでは不十分
168+ - ` rails dojos:update_db_by_yaml ` 実行時にYAMLの内容でDBが上書きされる
169+ - 永続化にはYAMLファイルへの反映が必須
170+
171+ ** データ更新の正しいフロー** :
172+ 1 . Git履歴から日付を抽出
173+ 2 . ** YAMLファイルに ` inactivated_at ` を追加**
174+ 3 . ` rails dojos:update_db_by_yaml ` でDBに反映
175+ 4 . ` rails dojos:migrate_adding_id_to_yaml ` で整合性確認
176+
177+ #### 1. Git履歴からの日付抽出とYAML更新スクリプト
165178
166179参考実装: https://github.com/remote-jp/remote-in-japan/blob/main/docs/upsert_data_by_readme.rb#L28-L44
167180
168181``` ruby
169182# lib/tasks/dojos.rake に追加
170- desc ' Git履歴からinactivated_at日付を抽出して設定 '
183+ desc ' Git履歴からinactivated_at日付を抽出してYAMLファイルに反映 '
171184task extract_inactivated_at_from_git: :environment do
172185 require ' git'
173186
@@ -213,12 +226,34 @@ task extract_inactivated_at_from_git: :environment do
213226 if commit_id && commit_id.match?(/^[0-9a-f] {40} $/ )
214227 # コミット情報を取得
215228 commit = git.gcommit(commit_id)
216- inactived_date = commit.author_date
229+ inactivated_date = commit.author_date
217230
218- # データベースを更新
219- dojo.update!(inactivated_at: inactived_date)
220- puts " ✓ Updated: inactivated_at = #{ inactived_date.strftime(' %Y-%m-%d %H:%M:%S' ) } "
221- puts " Commit: #{ commit_id[0 ..7 ] } by #{ commit.author.name } "
231+ # YAMLファイルのDojoブロックを見つけて更新
232+ yaml_updated = false
233+ yaml_lines.each_with_index do |line , index |
234+ if line.match?(/^- id: #{ dojo.id } $/ )
235+ # 該当Dojoブロックの最後に inactivated_at を追加
236+ insert_index = index + 1
237+ while insert_index < yaml_lines.length && ! yaml_lines[insert_index].match?(/^- id:/ )
238+ insert_index += 1
239+ end
240+
241+ # inactivated_at 行を挿入
242+ yaml_lines.insert(insert_index - 1 ,
243+ " inactivated_at: #{ inactivated_date.strftime(' %Y-%m-%d %H:%M:%S' ) } \n " )
244+ yaml_updated = true
245+ break
246+ end
247+ end
248+
249+ if yaml_updated
250+ # YAMLファイルを書き戻す
251+ File .write(yaml_path, yaml_lines.join)
252+ puts " ✓ Updated YAML: inactivated_at = #{ inactivated_date.strftime(' %Y-%m-%d %H:%M:%S' ) } "
253+ puts " Commit: #{ commit_id[0 ..7 ] } by #{ commit.author.name } "
254+ else
255+ puts " ✗ Failed to update YAML file"
256+ end
222257 else
223258 puts " ✗ Could not find commit information"
224259 end
@@ -229,8 +264,11 @@ task extract_inactivated_at_from_git: :environment do
229264
230265 puts " \n Summary:"
231266 puts " Total inactive dojos: #{ inactive_dojos.count } "
232- puts " Successfully updated: #{ inactive_dojos.reload.where.not(inactivated_at: nil ).count } "
233- puts " Failed to update: #{ inactive_dojos.reload.where(inactivated_at: nil ).count } "
267+ puts " YAML file has been updated with inactivated_at dates"
268+ puts " \n Next steps:"
269+ puts " 1. Review the changes in db/dojos.yaml"
270+ puts " 2. Run: rails dojos:update_db_by_yaml"
271+ puts " 3. Commit the updated YAML file"
234272end
235273
236274# 特定のDojoのみを処理するバージョン
@@ -483,18 +521,19 @@ end
483521
484522## 実装スケジュール案
485523
486- ### Phase 1(1週目)- 基盤整備
487- - [ ] ` inactivated_at ` カラム追加のマイグレーション作成
488- - [ ] ` note ` カラムの型変更マイグレーション作成
489- - [ ] Dojoモデルの基本的な変更(スコープ、メソッド追加)
490- - [ ] 再活性化機能(` reactivate! ` )の実装
491- - [ ] モデルテストの作成
492-
493- ### Phase 2(2週目)- データ移行準備
494- - [ ] Git履歴抽出スクリプトの実装
495- - [ ] ドライラン実行と結果確認
524+ ### Phase 1(1週目)- 基盤整備 ✅ 完了
525+ - [x] ` inactivated_at ` カラム追加のマイグレーション作成
526+ - [x] ` note ` カラムの型変更マイグレーション作成
527+ - [x] Dojoモデルの基本的な変更(スコープ、メソッド追加)
528+ - [x] 再活性化機能(` reactivate! ` )の実装
529+ - [x] モデルテストの作成
530+
531+ ### Phase 2(2週目)- データ移行準備(YAML対応版)
532+ - [ ] Git履歴からYAMLへの inactivated_at 抽出スクリプトの実装
533+ - [ ] YAMLファイルの更新(ドライラン)
534+ - [ ] dojos: update_db_by_yaml タスクの inactivated_at 対応
496535- [ ] 手動調整が必要なケースの特定
497- - [ ] CSVインポート機能の実装
536+ - [ ] YAMLファイルのレビューとコミット
498537
499538### Phase 3(3週目)- 統計機能更新
500539- [ ] Statモデルの更新(` active_at ` スコープの活用)
0 commit comments