Skip to content

学内募金周りのdb設計修正。#1068

Merged
hikahana merged 10 commits intodevelopfrom
fix/hikahana/campus-donation-db-fix
Apr 7, 2026
Merged

学内募金周りのdb設計修正。#1068
hikahana merged 10 commits intodevelopfrom
fix/hikahana/campus-donation-db-fix

Conversation

@hikahana
Copy link
Copy Markdown
Collaborator

概要

学内募金(campus_donations)機能の改善に伴い、建物・部屋管理テーブルの構造を最適化し、不要なカラムを削除しました。

変更内容

📋 データベーススキーマの変更

1. buildingsテーブルの変更

  • unit_numbersカラム(VARCHAR(255))を追加
  • building_unitsテーブルを削除し、号棟情報を統合

2. roomsテーブルの変更

  • building_idカラムを追加(buildingsテーブルへの直接参照)
  • floor_numberカラム(VARCHAR(255))を追加
  • floorsテーブルを削除し、階層情報を統合

3. リレーションの簡素化

変更前: buildingsbuilding_unitsfloorsroomsroom_teachers
変更後: buildingsroomsroom_teachers

4. campus_donationsテーブルのクリーンアップ

  • is_last_checkカラムを削除
  • is_first_checkカラムを削除
  • remarkカラムを削除

🔧 マイグレーションファイル

  • 000004_restructure_buildings_and_rooms.up.sql: スキーマ変更の実行
  • 000004_restructure_buildings_and_rooms.down.sql: ロールバック用

✅ データ移行

  • 既存のbuilding_units.unit_numberbuildings.unit_numbers
  • 既存のfloors.floor_numberrooms.floor_number
  • 外部キー制約の適切な更新(rooms_ibfk_1rooms_building_id_foreign_key

テスト項目

影響範囲

  • 建物・部屋管理機能
  • 学内募金機能
  • 教員配置機能(room_teachers)

備考

  • 既存データは適切に移行済み
  • 外部キー制約名の修正により、MySQLの自動生成制約名に対応
  • 不要なカラムの削除により、データベースの整合性が向上

@github-actions github-actions Bot added the bug Something isn't working label Mar 20, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

このプルリクエストは、学内募金機能に関連する建物・部屋管理のデータベース設計を最適化することを目的としています。既存のbuilding_unitsとfloorsテーブルをbuildingsとroomsテーブルに統合することで、データ構造を簡素化し、リレーションシップを合理化しました。これにより、データベースの整合性が向上し、不要なカラムが削除され、全体的な保守性が高まります。

Highlights

  • buildingsテーブルの変更: buildingsテーブルにunit_numbersカラムが追加され、building_unitsテーブルのデータが統合されました。
  • roomsテーブルの変更: roomsテーブルにbuilding_idとfloor_numberカラムが追加され、floorsテーブルのデータが統合されました。
  • リレーションの簡素化: 建物・部屋関連のリレーションがbuildings → rooms → room_teachersへと簡素化されました。
  • campus_donationsテーブルのクリーンアップ: campus_donationsテーブルからis_last_check、is_first_check、remarkカラムが削除されました。
  • マイグレーションファイルの追加: データベーススキーマ変更用の000004_restructure_buildings_and_rooms.up.sqlとロールバック用の000004_restructure_buildings_and_rooms.down.sqlが追加されました。
  • データ移行と外部キー制約の修正: 既存の建物・部屋データが新しいスキーマに移行され、外部キー制約名が修正されました。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

このプルリクエストは、学内募金機能に関連するデータベーススキーマの最適化と、不要なカラムの削除を行うものです。buildingsbuilding_unitsfloorsrooms間のリレーションを簡素化し、buildingsroomsの直接的な関連付けに変更されています。また、campus_donationsテーブルから不要なカラムが削除されました。全体として、データベース構造の改善とクリーンアップに貢献する変更です。

いくつかのマイグレーションスクリプトにおいて、データ移行時の潜在的なデータ損失や整合性の問題、および外部キー制約の変更による挙動の変化について懸念点があります。特に、unit_numbersの移行ロジックと、ロールバック時のデータ再構築ロジックは注意深く確認する必要があります。

Comment on lines +5 to +11
UPDATE buildings b
SET unit_numbers = (
SELECT bu.unit_number
FROM building_units bu
WHERE bu.building_id = b.id
LIMIT 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.

high

buildingsテーブルのunit_numbersカラムへのデータ移行において、LIMIT 1を使用しているため、もし元のbuilding_unitsテーブルで1つのbuilding_idに対して複数のunit_numberが存在した場合、そのうちの1つしか移行されず、データが失われる可能性があります。また、unit_numbersVARCHAR(255)であるため、複数の号棟情報を1つのカラムに格納する意図であれば、データ型や格納方法(例: JSON形式の文字列)を再検討するか、複数の号棟情報をどのように扱うか明確にする必要があります。

Comment on lines +51 to +52
JOIN building_units bu ON bu.building_id = b.id
WHERE r.floor_number IS NOT NULL;
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.

high

roomsテーブルのfloor_idを更新する際のJOIN条件 (JOIN building_units bu ON bu.building_id = b.id) は、1つのbuildingに複数のbuilding_unitsが存在する場合、どのbuilding_unitroomsに紐づけるべきか曖昧になる可能性があります。元のroomsテーブルがfloor_idを介してfloorsbuilding_unitsに紐付いていた構造を正確に再構築するためには、より具体的なJOIN条件やデータ移行ロジックが必要かもしれません。これにより、floor_idが誤って割り当てられるリスクがあります。

Comment thread AGENTS.md
- No manual dependency wiring needed in `main.go`
- Run `make run-eslint` and `make format` before committing frontend changes

コミットメッセージは日本語で生成してください
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.

medium

コミットメッセージに関する指示がファイルの最後に配置されていますが、Git Workflow & PR Rulesセクション内に移動することで、関連情報がまとまり、より見つけやすくなるかと思います。

Comment on lines +35 to +36
ALTER TABLE rooms ADD CONSTRAINT rooms_building_id_foreign_key
FOREIGN KEY (building_id) REFERENCES buildings (id) ON DELETE CASCADE;
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.

medium

roomsテーブルの新しい外部キーrooms_building_id_foreign_keyON DELETE CASCADEが追加されています。これにより、buildingsテーブルのレコードが削除された際に、関連するroomsテーブルのレコードも自動的に削除されるようになります。この挙動変更が意図されたものであるか、またデータ整合性に問題がないか確認してください。

-- building_unitsテーブルを復元
CREATE TABLE building_units (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
building_id INT(10) UNSIGNED NOT NULL DEFAULT 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.

medium

building_unitsテーブルの再作成時、building_idカラムにDEFAULT 1が設定されています。もしbuildingsテーブルにid=1のレコードが存在しない場合や、このデフォルト値が意図しないデータ整合性の問題を引き起こす可能性があるため、注意が必要です。ロールバック時のデータ復元において、適切なbuilding_idが割り当てられることを確認してください。

INSERT INTO building_units (building_id, unit_number, created_at, updated_at)
SELECT DISTINCT
b.id,
COALESCE(b.unit_numbers, '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.

medium

building_unitsテーブルへのデータ挿入時にCOALESCE(b.unit_numbers, '1')を使用しています。これは、buildings.unit_numbersNULLの場合に'1'を挿入することを意味します。もし元のbuilding_units.unit_numberNULLであったり、'1'以外の値であったりした場合、ロールバック時にデータが正しく復元されない可能性があります。元のデータがどのように扱われるべきか再確認してください。

JOIN floors f ON f.building_unit_id = bu.id AND f.floor_number = r.floor_number
SET r.floor_id = f.id;

-- floor_idにNOT NULL制約を追加
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.

medium

floor_idカラムにNOT NULL制約を追加する前に、その前のUPDATE文(55-59行目)で全てのroomsレコードのfloor_idが正しく設定されていることを保証する必要があります。もし何らかの理由でfloor_idNULLのままのレコードが存在する場合、このALTER TABLE文は失敗します。データ移行の完全性を確認してください。

@hikahana hikahana mentioned this pull request Mar 20, 2026
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Mar 20, 2026

Deploying finansu with  Cloudflare Pages  Cloudflare Pages

Latest commit: 58ca9d5
Status: ✅  Deploy successful!
Preview URL: https://bc329e5b.finansu.pages.dev
Branch Preview URL: https://fix-hikahana-campus-donation.finansu.pages.dev

View logs

Comment on lines +10 to +16
UPDATE buildings b
SET unit_numbers = (
SELECT bu.unit_number
FROM building_units bu
WHERE bu.building_id = b.id
LIMIT 1
);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

この辺のデータ操作はマイグレーションファイルでやりたくないな
シードデータ更新では対応できないかな、、?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

おそらく修正done

Comment on lines +26 to +31
UPDATE rooms r
JOIN floors f ON r.floor_id = f.id
JOIN building_units bu ON f.building_unit_id = bu.id
SET
r.building_id = bu.building_id,
r.floor_number = f.floor_number;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

こちらも

@hikahana
Copy link
Copy Markdown
Collaborator Author

tinyintにするの忘れてたからそれも変更しました。 @Kubosaka

Comment on lines +9 to +11
-- room系マスタはシードで再投入する前提で空にする
DELETE FROM room_teachers;
DELETE FROM rooms;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[imo] これもDDLじゃないからマイグレーションで書かないほうがいい気がする
別コマンドで、空にしてseed投入するとかのが正しい気がする

@hikahana hikahana requested a review from Kubosaka March 31, 2026 06:50
@Chikuwa0141 Chikuwa0141 self-requested a review April 7, 2026 08:58
Copy link
Copy Markdown
Collaborator

@Chikuwa0141 Chikuwa0141 left a comment

Choose a reason for hiding this comment

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

LGTM

@hikahana hikahana merged commit 2d90fcc into develop Apr 7, 2026
3 checks passed
@hikahana hikahana deleted the fix/hikahana/campus-donation-db-fix branch April 7, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants