Skip to content

Commit c696cb0

Browse files
authored
Merge pull request #224 from codeforjapan/fix/helpfulness-level-nullable
Fix/helpfulness level nullable
2 parents 69d90f0 + e9f9731 commit c696cb0

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,6 @@ cython_debug/
170170
# accounts db for BirdXplorer
171171
accounts.db
172172
.claude/settings.local.json
173+
*/**/CLAUDE.md
174+
.env.*
175+
*/**/.env.*

common/birdxplorer_common/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class RowNoteRatingRecord(Base):
263263
disagree: Mapped[BinaryBool] = mapped_column(nullable=False)
264264
helpful: Mapped[BinaryBool] = mapped_column(nullable=False)
265265
not_helpful: Mapped[BinaryBool] = mapped_column(nullable=False)
266-
helpfulness_level: Mapped[String] = mapped_column(nullable=False)
266+
helpfulness_level: Mapped[Optional[String]] = mapped_column(nullable=True)
267267
helpful_other: Mapped[BinaryBool] = mapped_column(nullable=False)
268268
helpful_informative: Mapped[BinaryBool] = mapped_column(nullable=False)
269269
helpful_clear: Mapped[BinaryBool] = mapped_column(nullable=False)

etl/src/birdxplorer_etl/extract_ecs.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,18 +567,28 @@ def _process_rating_rows(reader, postgresql: Session, existing_row_note_ids: set
567567

568568
# 5000件ごとにバッチ処理(1000 → 5000に拡大)
569569
if len(rows_to_add) >= 5000:
570-
postgresql.execute(insert(RowNoteRatingRecord).on_conflict_do_nothing(), rows_to_add)
571-
postgresql.commit()
572-
logging.info(
573-
f"Saved {len(rows_to_add)} rating records (batch at index {index}, file {file_index:05d})"
574-
)
570+
try:
571+
postgresql.execute(insert(RowNoteRatingRecord).on_conflict_do_nothing(), rows_to_add)
572+
postgresql.commit()
573+
logging.info(
574+
f"Saved {len(rows_to_add)} rating records (batch at index {index}, file {file_index:05d})"
575+
)
576+
except Exception as e:
577+
logging.error(
578+
f"Failed to save rating batch at index {index}, file {file_index:05d}: {e}"
579+
)
580+
postgresql.rollback()
575581
rows_to_add = []
576582

577583
# 最後のバッチを処理
578584
if rows_to_add:
579-
postgresql.execute(insert(RowNoteRatingRecord).on_conflict_do_nothing(), rows_to_add)
580-
postgresql.commit()
581-
logging.info(f"Saved final batch of {len(rows_to_add)} rating records (file {file_index:05d})")
585+
try:
586+
postgresql.execute(insert(RowNoteRatingRecord).on_conflict_do_nothing(), rows_to_add)
587+
postgresql.commit()
588+
logging.info(f"Saved final batch of {len(rows_to_add)} rating records (file {file_index:05d})")
589+
except Exception as e:
590+
logging.error(f"Failed to save final rating batch, file {file_index:05d}: {e}")
591+
postgresql.rollback()
582592

583593

584594
def extract_ratings(postgresql: Session, dateString: str, existing_row_note_ids: set):
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""make helpfulness_level nullable in row_note_ratings
2+
3+
Community Notes の 2021年1月〜6月のレーティングデータには
4+
helpfulness_level フィールドが存在しない(空文字列)。
5+
NOT NULL 制約があると INSERT 時に NotNullViolation が発生し、
6+
ファイル全体のデータがロストするため nullable に変更する。
7+
8+
Revision ID: make_helpfulness_level_nullable
9+
Revises: add_collab_note_column
10+
Create Date: 2026-02-23
11+
"""
12+
13+
from typing import Sequence, Union
14+
15+
from alembic import op
16+
import sqlalchemy as sa
17+
18+
19+
# revision identifiers, used by Alembic.
20+
revision: str = "make_helpfulness_level_nullable"
21+
down_revision: Union[str, None] = "add_collab_note_column"
22+
branch_labels: Union[str, Sequence[str], None] = None
23+
depends_on: Union[str, Sequence[str], None] = None
24+
25+
26+
def upgrade() -> None:
27+
op.alter_column(
28+
"row_note_ratings",
29+
"helpfulness_level",
30+
existing_type=sa.String(),
31+
nullable=True,
32+
)
33+
34+
35+
def downgrade() -> None:
36+
op.alter_column(
37+
"row_note_ratings",
38+
"helpfulness_level",
39+
existing_type=sa.String(),
40+
nullable=False,
41+
)

0 commit comments

Comments
 (0)