Skip to content

[codex] Skip unknown CSV import columns#1476

Merged
TinyKitten merged 4 commits intodevfrom
fix/csv-import-columns
Apr 11, 2026
Merged

[codex] Skip unknown CSV import columns#1476
TinyKitten merged 4 commits intodevfrom
fix/csv-import-columns

Conversation

@TinyKitten
Copy link
Copy Markdown
Member

@TinyKitten TinyKitten commented Apr 11, 2026

Summary

  • fetch the target table columns from PostgreSQL before importing each CSV
  • generate explicit column-name INSERT statements using only matching CSV headers
  • skip comment columns and warn when the CSV contains headers that are not present in the destination table
  • remove the stray line_color_t column from data/2!lines.csv
  • restore data/2!lines.csv to the original header shape by removing lon, lat, and zoom
  • add a unit test covering header filtering for comment and unknown columns

Why

CSV imports could fail when a file contained comment columns or extra headers that were not present in the destination table because the importer previously emitted positional INSERT INTO ... VALUES statements for every non-comment column. In parallel, data/2!lines.csv had drifted from its intended schema by carrying columns that should not have been present.

Impact

  • CSV imports no longer depend on exact positional column parity with the table schema
  • unexpected headers are ignored with a warning instead of breaking the import
  • data/2!lines.csv now matches the expected header contract again
  • existing supported columns still preserve their original value handling for NULL, DEFAULT, and escaped strings

Validation

  • cargo fmt --all
  • cargo test --lib --package stationapi (fails in this repo root because the build script cannot resolve proto/stationapi.proto via its current relative path)
  • cargo run -p data_validator

Summary by CodeRabbit

リリースノート

バグ修正

  • CSV インポート時に PostgreSQL テーブルメタデータを自動検査し、CSV ヘッダーとテーブル列の正確なマッピングが可能に
  • 不明な CSV ヘッダーは安全にスキップされログに記録
  • 空文字列を NULL として、"DEFAULT" キーワードを SQL DEFAULT として処理

@github-actions github-actions Bot added the fix 直した label Apr 11, 2026
@TinyKitten TinyKitten self-assigned this Apr 11, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e8845bb2-e52e-4b3e-86dc-a47c517f1270

📥 Commits

Reviewing files that changed from the base of the PR and between 36f63c3 and 84d5c08.

⛔ Files ignored due to path filters (1)
  • data/2!lines.csv is excluded by !**/*.csv
📒 Files selected for processing (1)
  • stationapi/src/import.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • stationapi/src/import.rs

📝 Walkthrough

Walkthrough

CSVインポートがPostgreSQLのスキーマ情報(information_schema.columns)を参照して、コメント(#)以外かつ実在するテーブル列のみを抽出し、明示的な列リスト付きのINSERTを生成。未知ヘッダーはログ警告され、マッチする列がないCSVはスキップされるようになった。

Changes

Cohort / File(s) Summary
CSV Import Logic Enhancement
stationapi/src/import.rs
追加: fetch_table_columns(Postgresスキーマ取得)、build_insert_columns(コメント除外・実在列のマッチング)、INSERT文を明示的な列リスト付きに変更。未知ヘッダーを警告ログ、マッチ0列ならCSVスキップ。行値抽出はマッチ列のみ、空文字→NULL、"DEFAULT"→SQL DEFAULT。ユニットテスト追加(コメント・未知ヘッダー除外)。

Sequence Diagram(s)

mermaid
sequenceDiagram
participant CSV as CSVファイル
participant Importer as インポーター
participant PG as PostgreSQL
participant Logger as ロガー

CSV->>Importer: ヘッダー読み取り
Importer->>PG: information_schema.columns を照会(fetch_table_columns)
PG-->>Importer: テーブル列一覧
Importer->>Importer: build_insert_columns(コメント/未知列を除外)
alt マッチ列なし
    Importer->>Logger: 警告ログ(スキップ)
    Importer-->>CSV: ファイルスキップ
else マッチあり
    CSV->>Importer: 行を読み取り(マッチ列インデックスのみ)
    Importer->>Importer: 値マッピング(""→NULL, "DEFAULT"→DEFAULT, others→エスケープ)
    Importer->>PG: INSERT INTO table (matched_columns) VALUES (...)
    PG-->>Importer: 実行結果
    Importer->>Logger: 成功/エラー記録
end

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰📦 CSV持って跳ねるよ、
列名を確かめてぴったり合うよ、
コメントはすり抜け、知らぬは記録、
空はNULLに、"DEFAULT"はそのまま、
インポート、ふわっと完了だよ!

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive プルリクエスト説明は要件セクション、変更理由、影響、検証方法を含むが、リポジトリのテンプレートで必須とされる構造化セクション(概要、変更の種類、変更内容、テスト、関連Issue)は完全には従っていない。 リポジトリテンプレートの構造に従い、概要、変更の種類(チェックボックス)、詳細な変更内容、テスト手順を明確なセクションで整理してください。
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed タイトルは CSV インポート時に未知の列をスキップする主な変更を明確に要約しており、変更セット全体と関連している。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/csv-import-columns

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@stationapi/src/import.rs`:
- Around line 213-237: The new test module duplicates an existing `mod tests`
and causes Rust E0428; rename the new module or merge its test into the existing
one to avoid the collision. Locate the new `mod tests` that contains the
`build_insert_columns_skips_comment_and_unknown_columns` test and either (A)
rename that module to a unique identifier (e.g., `mod import_tests`) or (B) move
the `#[test] fn build_insert_columns_skips_comment_and_unknown_columns()`
function into the already-defined `mod tests` near the existing tests for
`build_insert_columns`, ensuring you keep the `use super::build_insert_columns;`
and test body unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cfe09058-7e9f-4606-97ba-211abc04a8fb

📥 Commits

Reviewing files that changed from the base of the PR and between 4ab073f and 36f63c3.

📒 Files selected for processing (1)
  • stationapi/src/import.rs

Comment thread stationapi/src/import.rs
@TinyKitten TinyKitten merged commit 1a6eff8 into dev Apr 11, 2026
13 checks passed
@TinyKitten TinyKitten deleted the fix/csv-import-columns branch April 11, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix 直した

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant