Skip to content

お住まいの地域が未登録時はnilが保存され、既存のデータベース内の空文字列はnilへと変換する#8446

Merged
komagata merged 8 commits intomainfrom
bug/fix-empty-string-for-country-code-and-subdivision-code
Mar 22, 2025
Merged

お住まいの地域が未登録時はnilが保存され、既存のデータベース内の空文字列はnilへと変換する#8446
komagata merged 8 commits intomainfrom
bug/fix-empty-string-for-country-code-and-subdivision-code

Conversation

@SuzukiShuntarou
Copy link
Copy Markdown
Contributor

@SuzukiShuntarou SuzukiShuntarou commented Mar 14, 2025

Issue

概要

変更確認方法

  • 変更確認方法1→変更確認方法2の順番で実施をお願いいたします。
    • 変更確認方法1でブラウザからデータベースの更新を実施しているのはテストデータにお住まいの地域『country_codesubdivision_code』に空文字列のデータがないため(テストデータに設定がない場合はnilで保存されている)です。
    • 空文字列が登録されたユーザデータは本PRのマージ後はnilとなるため、テストデータでは用意せずに変更確認方法1の手順で手動で作成する手順としております。

変更確認方法1:本番環境のデータベース更新の動作確認

  1. mainブランチでforeman start -f Procfile.devローカル環境を立ち上げる。
  2. ユーザー名 hatsuno パスワード testtest でログインする。
  3. 右上のユーザアイコンをクリックし『登録情報変更』をクリックする。
  4. 登録情報変更ページから『お住まいの地域(都道府県・州まで)』で『登録しない』を選択し、画面下にある『更新する』ボタンをクリックする。
    image
  5. ログアウトし、ユーザー名 hajime パスワード testtest でログインする。
  6. 右上のユーザアイコンをクリックし『登録情報変更』をクリックする。
  7. 『お住まいの地域(都道府県・州まで)』で『登録する』を選択し、『国』だけ選択し『都道府県・州』は『選択してください』のまま画面下にある『更新する』ボタンをクリックする。
    image
  8. 別ターミナルを起動し、ローカル環境を立ち上げているディレクトリへ移動後にrails cを実行してコンソールを立ち上げる。
  9. コンソール上で以下を入力し確認する。
    1.User.where(email: 'hatsuno@fjord.jp')を入力し、country_codesubdivision_codeに空文字列が保存されていることを確認する。
    image
    2.User.where(email: 'hajime@fjord.jp')を入力し、country_codeには選択した国のコード、subdivision_codeには空文字列が保存されていることを確認する。
    image
    3.User.where(email: 'komagata@fjord.jp')を入力し、country_codesubdivision_codeにそれぞれ以下のコードが保存されていることを確認する。
    image
  10. bug/fix-empty-string-for-country-code-and-subdivision-codeをローカルに取り込む。
    1. git fetch origin bug/fix-empty-string-for-country-code-and-subdivision-code
    2. git checkout bug/fix-empty-string-for-country-code-and-subdivision-code

11.rake data:migrate:up VERSION=20250314075905を実施する。

  • db/data/20250314075905_convert_blank_of_country_code_and_subdivision_code_to_nil.rbupメソッドをローカル環境で実施するコマンド。
  • 本番環境ではgem data-migrateで実施する。ローカル環境では上記のコマンドで動作確認をする。Wiki
  • 成功するとターミナル上に以下のようなログが出力される。
    image
  1. コンソール上でcountry_codesubdivision_codeが正しくnilに変更されていることを確認する
    1. User.where(email: 'hatsuno@fjord.jp')はどちらもnil
      image
    2. User.where(email: 'hajime@fjord.jp')subdivision_codeだけがnil
      image
    3. User.where(email: 'komagata@fjord.jp')は変更なし。
      image

変更確認方法2:登録情報のcountry_codesubdivision_codeの保存時に未登録の場合はnilが保存される動作確認

  1. bug/fix-empty-string-for-country-code-and-subdivision-codeをローカルに取り込む。(既に済の場合は省略)
    1. git fetch origin bug/fix-empty-string-for-country-code-and-subdivision-code
    2. git checkout bug/fix-empty-string-for-country-code-and-subdivision-code
  2. foreman start -f Procfile.devローカル環境を立ち上げる。
  3. ユーザー名 komagata パスワード testtest でログインする。
  4. 右上のユーザアイコンをクリックし『登録情報変更』をクリックする。
  5. 別ターミナルを起動し、ローカル環境を立ち上げているディレクトリへ移動後にrails cを実行してコンソールを立ち上げる。
  6. コンソール上で以下を入力しcountry_codesubdivision_codeに登録情報が設定されていることを確認する。
User.where(email: 'komagata@fjord.jp')
  • 登録情報(コンソール上)
    image
  1. ブラウザで登録情報変更ページから『お住まいの地域(都道府県・州まで)』で『登録しない』を選択し、画面下にある『更新する』ボタンをクリックする。
    image

  2. 再度コンソール上でUser.where(email: 'komagata@fjord.jp')を入力し、country_codesubdivision_codenilが保存されていることを確認する。
    image

  3. ブラウザで右上のユーザアイコンをクリック後に『登録情報変更』をクリックし『登録情報変更』ページへアクセスする。

  4. 『お住まいの地域(都道府県・州まで)』で『登録する』を選択し、『国』だけ選択し『都道府県・州』は『選択してください』のまま画面下にある『更新する』ボタンをクリックする。
    image

  5. コンソール上でUser.where(email: 'komagata@fjord.jp')を実行し、country_codeには選択した国のコード、subdivision_codeにはnilが保存されていることを確認する。
    image

  6. ブラウザで右上のユーザアイコンをクリック後に『登録情報変更』をクリックし『登録情報変更』ページへアクセスする。

  7. 『お住まいの地域(都道府県・州まで)』で『登録する』を選択し、『国』と『都道府県・州』を選択して画面下にある『更新する』ボタンをクリックする。
    image

  8. コンソール上でUser.where(email: 'komagata@fjord.jp')を実行し、country_codeには選択した国のコード、subdivision_codeには選択した都道府県・州のコードが保存されていることを確認する。
    image

Screenshot

変更前:コンソール上で確認(画面の変更はなし)

  • country_codesubdivision_codeが共に未登録の場合はともに空文字列
    image
  • country_codeだけ登録、subdivision_codeは未登録の場合はsubdivision_codeだけ空文字列
    image

変更後:コンソール上で確認(画面の変更はなし)

  • country_codesubdivision_codeが共に未登録の場合はともにnil
    image
  • country_codeだけ登録、subdivision_codeは未登録の場合はsubdivision_codeだけnil
    image

@SuzukiShuntarou SuzukiShuntarou changed the title country_codeとsubdivision_codeをallow_nilにしバリデーションチェック前に空文字列をnilに変更するようにした お住まいの地域が未登録時はnilが保存され、既存のデータベース内の空文字列はnilへと変換する Mar 14, 2025
@SuzukiShuntarou SuzukiShuntarou self-assigned this Mar 14, 2025
@SuzukiShuntarou SuzukiShuntarou marked this pull request as ready for review March 16, 2025 01:15
@SuzukiShuntarou SuzukiShuntarou requested a review from mousu-a March 16, 2025 01:17
@SuzukiShuntarou
Copy link
Copy Markdown
Contributor Author

@mousu-a
お疲れ様です!もし可能であればこちらのissueのレビューをしていただけないでしょうか?🙏
難しい場合は仰ってください。🙇‍♀️どうぞよろしくお願いいたします!

@mousu-a
Copy link
Copy Markdown
Contributor

mousu-a commented Mar 16, 2025

@SuzukiShuntarou
レビューご依頼ありがとうございますー!
ぜひ引き受けさせていただきます🙆‍♂️
しばしお待ちください🙇‍♂️

Copy link
Copy Markdown
Contributor

@mousu-a mousu-a left a comment

Choose a reason for hiding this comment

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

@SuzukiShuntarou
お疲れ様です🍵
変更確認方法とてもわかりやすかったです!ありがとうございました😊
二点ほど気になる点にコメントさせていただきました!
ご確認のほどよろしくお願いいたします🙏

end

def convert_blank_of_country_code_to_nil
self.country_code = nil if country_code.blank?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

提案です!
好みの部分だと思いますが、他のほとんどのメソッドはselfをつけていないので既存の実装に合わせてselfをなくしてもいいかも?と思いました。

Copy link
Copy Markdown
Contributor Author

@SuzukiShuntarou SuzukiShuntarou Mar 16, 2025

Choose a reason for hiding this comment

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

ご指摘ありがとうございます!実はこのselfについては自分も悩んでいたところで、実装時に理由までちゃんと調べきれていなかったので助かりました。今回のご指摘を踏まえて調べた内容を共有いたします。

動作

  • selfを付けない場合、『登録しない』を選択して保存するとバリデーションエラーとなります。
    image

理由

  • こちらチェリー本の『selfの付け忘れで不具合が発生するケース』が該当となります。(もしお持ちでしたらチェリー本も読んでいただければ分かりやすいかと思います。)結論としては selfを付けない場合は『country_codeというローカル変数として解釈され、ローカル変数への代入と判断される』ことがバリデーションエラーとなる原因みたいです。
  • 一方、if country_code.blank?の場合はモデルの属性のcontry_codeを参照されるため、selfを省略しても問題がなくモデルの属性に対してblank?メソッドを呼び出す形となります。

補足

  • こちらで似たようなことをしているので参考になりそうです。

Copy link
Copy Markdown
Contributor

@mousu-a mousu-a Mar 16, 2025

Choose a reason for hiding this comment

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

返信ありがとうございます!

selfを付けない場合、『登録しない』を選択して保存するとバリデーションエラーとなります。

あーすみません!気づきませんでした🙇‍♂️

実装時に理由までちゃんと調べきれていなかったので助かりました。

そういっていただけると助かります🙏
参考まで詳細にありがとうございます🙇‍♂️
とてもわかりやすく、腹落ちできました!

assert_equal 2, user.latest_micro_report_page
end

test 'convert to nil during saving when country_code is empty string' do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

質問です!
このテストと下のテストですが、ストーリーが似通っているので一緒にしてしまってもいいのかなと思いました。(1つのテスト内でcountry_code,subdivision_code どちらもテストする)
何か分けた理由があれば教えていただきたいです🙏

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つのテストで実装しようと検討していたのですが、モデル側でメソッドを別々に定義したため、対応するテストも別々にすべきかと思い、分割いたしました。度々恐れ入りますが、アドバイスいただければ嬉しいです🙏

Copy link
Copy Markdown
Contributor

@mousu-a mousu-a Mar 16, 2025

Choose a reason for hiding this comment

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

なるほどです。
各メソッドに対応したテストのため対応するテストも別々に、ということですね👀

以下それに関して自分の考えを言語化してみました!ご参考までに🙇‍♂️

個人的には、a_method, b_methodに対するtest #a_method,test #b_methodという形のテストであれば、"対応するテストもそれぞれ用意する"、というロジックがすんなり入ってくるのですが、今回のような場合はどうなんでしょうか🤔

今回の場合はバリデーションで実行されるメソッドのテストで、やっていること(中身)はほとんどシステムテストに近いように思います。なのでパッと見た時に、「メソッドごとに分割せずまとめてしまっても良いのかな?」と感じたのだと思います。


パッと見た時に感じた違和感を言語化してみたのですが、ちょっとハッキリ「コレ!」って確信が持てないです😅
このあたりの判断はkomagataさんにお任せしたいと思います🙇‍♂️

お答えいただきありがとうございました!!🙏

@@ -0,0 +1,19 @@
# frozen_string_literal: true

class ConvertBlankOfCountryCodeAndSubdivisionCodeToNil < ActiveRecord::Migration[6.1]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

勉強になります!!🙏

@SuzukiShuntarou
Copy link
Copy Markdown
Contributor Author

@mousu-a
早速のレビューありがとうございます!頂いたコメントに直接返信いたしましたので、ご確認をお願いいたします🙏

変更確認方法とてもわかりやすかったです!ありがとうございました😊

  • 動作確認が複雑になってしまったかも?と思っていたので嬉しいです。ありがとうございます!

Copy link
Copy Markdown
Contributor

@mousu-a mousu-a left a comment

Choose a reason for hiding this comment

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

@SuzukiShuntarou
お疲れ様です🍵
コメント返信させていただきました!

LGTMです✨
疑問点が解消できましたのでこちらからはApproveとさせていただきます〜!

@SuzukiShuntarou
Copy link
Copy Markdown
Contributor Author

@mousu-a
レビューありがとうございました!私も勉強になりました。

@komagata
メンバーレビューが完了いたしました。レビューをよろしくお願いいたします。

self.country_code = nil if country_code.blank?
end

def convert_blank_of_subdivision_code_to_nil
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

この二つ、やっていることは簡単なのに名前が長くて複雑な感じが増しちゃうので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.

@komagata
レビューありがとうございます!一つのメソッドにまとめ、メソッド名も短くいたしました。

@SuzukiShuntarou SuzukiShuntarou force-pushed the bug/fix-empty-string-for-country-code-and-subdivision-code branch from cbead86 to 1025a12 Compare March 21, 2025 02:22
@SuzukiShuntarou
Copy link
Copy Markdown
Contributor Author

@komagata
レビューありがとうございます!ご指摘いただいた箇所を修正いたしました。
ご確認をよろしくお願いいたします。

Copy link
Copy Markdown
Member

@komagata komagata left a comment

Choose a reason for hiding this comment

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

確認させて頂きました。OKです〜🙆‍♂️

@komagata komagata merged commit 755208a into main Mar 22, 2025
3 checks passed
@komagata komagata deleted the bug/fix-empty-string-for-country-code-and-subdivision-code branch March 22, 2025 21:48
@github-actions github-actions bot mentioned this pull request Mar 22, 2025
15 tasks
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.

3 participants