Skip to content

ActiveHash/YAML から ActiveRecord/PostgreSQL へのDB移行#14

Merged
en-Barry merged 10 commits into
developfrom
feature/2605/en-Barry/db-migration
May 12, 2026
Merged

ActiveHash/YAML から ActiveRecord/PostgreSQL へのDB移行#14
en-Barry merged 10 commits into
developfrom
feature/2605/en-Barry/db-migration

Conversation

@en-Barry
Copy link
Copy Markdown
Owner

@en-Barry en-Barry commented May 7, 2026

要約

Issue #13(ActiveHash/YAML → ActiveRecord/PostgreSQL へのDB移行)を解決する。

  • 4テーブル(providers / plans / ampere_based_rates / usage_based_rates)をマイグレーションで作成し、モデルを ActiveYaml::BaseApplicationRecord に移行
  • YAML ファイルを seeds.rb で読み込む形式は維持し、find_or_initialize_by(id:) + assign_attributes で再シード可能に
  • active_hash gem を完全に削除

Closes #13

実装してみて発生した追加判断

  • upsert を find_or_initialize_by(id:) + assign_attributes で実装: upsert_all ではなくこの方式を採用した。YAML の id を保持しながら再シードでレコードを更新できること、モデルバリデーションを通過させてデータ品質を保証できることが理由。シード後に PostgreSQL シーケンスを setval で補正し、id 指定 insert 後の自動採番ズレを防ぐ
  • seeds.rb に削除同期を追加: ActiveRecord 移行後は DB が真実の源になるため、YAML から消したレコードが DB に残り続けると古いマスタが API に出続ける。reverse_each で子テーブルから順に「YAML に存在しない id」を destroy_all する3フェーズ構成(削除→upsert→シーケンスリセット)に変更
  • data_consistency_spec から参照整合性テストを削除: FK 制約が DB レベルで保証するため spec での重複チェックは不要と判断。連続性(usage_based_rate の段間に穴・重複がない)と metered_only? の業務的不変条件のみ残した

テストケース一覧

spec/models/ — FactoryBot ベースに書き換え(23 examples)

Provider(2 examples)

ケース 入力 期待
name が空だと invalid name=nil errors[:name]
has_many :plans が引ける plans を 2 件 create plans.size == 2

Plan(7 examples)

ケース 入力 期待
name が空だと invalid name=nil errors[:name]
provider が nil だと invalid provider=nil errors[:provider]
belongs_to :provider が引ける Provider インスタンス
has_many :ampere_based_rates が引ける 3 件 create size == 3
has_many :usage_based_rates が引ける 2 件 create size == 2
ampere_based_rates がない場合 metered_only? = true true
ampere_based_rates がある場合 metered_only? = false 1 件 create false

AmpereBasedRate(6 examples)

ケース 入力 期待
許容外 ampere(25)は invalid ampere=25 errors[:ampere]
plan が nil だと invalid plan=nil errors[:plan]
rate が空だと invalid rate=nil errors[:rate]
rate が負だと invalid rate=-100 errors[:rate]
rate が BigDecimal を返す rate='286.00' BigDecimal
rate が正しい値を返す rate='286.00' BigDecimal('286.00')

UsageBasedRate(8 examples)

ケース 入力 期待
全必須項目が nil だと invalid plan=nil, low=nil, high=nil, rate=nil 4フィールドにエラー
low > high だと invalid low=200, high=100 errors[:kilowatt_hour_low]
low > high のエラーが日本語 low=200, high=100 「は kilowatt_hour_high 以下でなければなりません」
low が 0 以下だと invalid low=0 errors[:kilowatt_hour_low]
high が 0 以下だと invalid high=0 errors[:kilowatt_hour_high]
rate が負だと invalid rate=-10 errors[:rate]
rate が BigDecimal を返す rate='19.88' BigDecimal
rate が正しい値を返す rate='19.88' BigDecimal('19.88')

spec/data_consistency_spec.rb — ActiveRecord 版に書き換え(8 examples)

ケース 期待
Plan は 4 件存在する count == 4
Provider の全レコードが valid valid? == true
Plan の全レコードが valid valid? == true
AmpereBasedRate の全レコードが valid valid? == true
UsageBasedRate の全レコードが valid valid? == true
各プランの usage_based_rates は low=1 から始まる first.kilowatt_hour_low == 1
各プランの usage_based_rates に穴・重複なし next_low == prev_high + 1
metered_only? なプランは「おうちプラン」のみ map(:name) == ['おうちプラン']

@en-Barry en-Barry linked an issue May 7, 2026 that may be closed by this pull request
@en-Barry en-Barry changed the title feat: ActiveHash/YAML から ActiveRecord/PostgreSQL へのDB移行 ActiveHash/YAML から ActiveRecord/PostgreSQL へのDB移行 May 8, 2026
@en-Barry en-Barry linked an issue May 8, 2026 that may be closed by this pull request
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 6b21fba to d0dde37 Compare May 11, 2026 05:01
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from 9da5f9a to 3b7036d Compare May 11, 2026 05:02
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from d0dde37 to 118f057 Compare May 11, 2026 05:57
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from 3b7036d to a9a7304 Compare May 11, 2026 06:01
Repository owner deleted a comment from vercel Bot May 11, 2026
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 118f057 to df370f2 Compare May 11, 2026 08:57
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from a9a7304 to ea6df28 Compare May 11, 2026 08:59
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from df370f2 to 12ed79a Compare May 11, 2026 10:06
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from ea6df28 to 45015a3 Compare May 11, 2026 10:09
Repository owner deleted a comment from vercel Bot May 11, 2026
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 12ed79a to 8270ee7 Compare May 12, 2026 06:08
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from 45015a3 to a0c5cb7 Compare May 12, 2026 07:41
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 8270ee7 to 6f996eb Compare May 12, 2026 10:36
en-Barry and others added 10 commits May 12, 2026 19:37
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ActiveRecord 移行後は DB が真実の源になるため、YAML から消えたレコードが
残り続けないよう child-first で destroy_all する削除フェーズを追加。
また途中失敗時のデータ破損を防ぐため全処理を transaction で囲んだ。
README のセットアップ手順にも db:seed を明示した。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from a0c5cb7 to 66be6ac Compare May 12, 2026 10:40
Base automatically changed from feature/2605/en-Barry/api-endpoint to develop May 12, 2026 10:41
@en-Barry en-Barry merged commit 6001f2e into develop May 12, 2026
@en-Barry en-Barry deleted the feature/2605/en-Barry/db-migration branch May 12, 2026 10:41
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.

ActiveHash/YAML から ActiveRecord/PostgreSQL へのDB移行 電気料金シミュレーションAPI実装(PR1: 基本要件)

1 participant