DB: Add source column to relationships table #44

Closed
opened 2026-02-09 00:16:21 +00:00 by forbes · 0 comments
Owner

Summary

Add a dedicated source column to the relationships table to distinguish assembly-derived BOM entries from manually-added ones. This is required by the BOM merge feature — the merge endpoint needs to know which entries came from an assembly commit (and can be auto-updated) vs. which were added manually via the web UI or CSV import (and must never be auto-deleted).

Current State

The source field is currently stored inside the metadata JSONB column on the relationships table. The BOM CSV import writes it as metadata["source"], and the frontend BOMTab reads/displays m.source. This is a free-form text field with no enforced values.

Migration

-- 012_bom_source.sql
ALTER TABLE relationships
    ADD COLUMN source VARCHAR(20) NOT NULL DEFAULT 'manual'
    CHECK (source IN ('manual', 'assembly'));

-- Migrate existing metadata.source values
UPDATE relationships
SET source = 'manual'
WHERE source = 'manual';

All existing entries default to manual. The BOM merge endpoint will set source = 'assembly' for entries it creates.

Code Changes

internal/db/relationships.go

  • Add Source string field to Relationship struct
  • Add Source string field to BOMEntry struct
  • Update Create() to include source in INSERT
  • Update GetBOM(), GetWhereUsed(), GetExpandedBOM() queries to SELECT source

internal/api/bom_handlers.go

  • Add Source string to BOMEntryResponse (json:"source")
  • Add Source string to AddBOMEntryRequest (json:"source,omitempty") — defaults to manual if omitted
  • Update CSV import to write source column instead of metadata["source"]
  • Update CSV export to read from source column

Frontend (web/src/components/items/BOMTab.tsx)

  • Update to read/write source as a top-level field instead of from metadata
  • No visible change to the user

Merge Rule

The BOM merge endpoint (separate issue) relies on this column:

  • source = 'assembly' entries can be auto-added, quantity-updated, or flagged as unreferenced by assembly merges
  • source = 'manual' entries are never touched by assembly merges
## Summary Add a dedicated `source` column to the `relationships` table to distinguish assembly-derived BOM entries from manually-added ones. This is required by the BOM merge feature — the merge endpoint needs to know which entries came from an assembly commit (and can be auto-updated) vs. which were added manually via the web UI or CSV import (and must never be auto-deleted). ## Current State The `source` field is currently stored inside the `metadata` JSONB column on the `relationships` table. The BOM CSV import writes it as `metadata["source"]`, and the frontend BOMTab reads/displays `m.source`. This is a free-form text field with no enforced values. ## Migration ```sql -- 012_bom_source.sql ALTER TABLE relationships ADD COLUMN source VARCHAR(20) NOT NULL DEFAULT 'manual' CHECK (source IN ('manual', 'assembly')); -- Migrate existing metadata.source values UPDATE relationships SET source = 'manual' WHERE source = 'manual'; ``` All existing entries default to `manual`. The BOM merge endpoint will set `source = 'assembly'` for entries it creates. ## Code Changes ### `internal/db/relationships.go` - Add `Source string` field to `Relationship` struct - Add `Source string` field to `BOMEntry` struct - Update `Create()` to include `source` in INSERT - Update `GetBOM()`, `GetWhereUsed()`, `GetExpandedBOM()` queries to SELECT `source` ### `internal/api/bom_handlers.go` - Add `Source string` to `BOMEntryResponse` (`json:"source"`) - Add `Source string` to `AddBOMEntryRequest` (`json:"source,omitempty"`) — defaults to `manual` if omitted - Update CSV import to write `source` column instead of `metadata["source"]` - Update CSV export to read from `source` column ### Frontend (`web/src/components/items/BOMTab.tsx`) - Update to read/write `source` as a top-level field instead of from metadata - No visible change to the user ## Merge Rule The BOM merge endpoint (separate issue) relies on this column: - `source = 'assembly'` entries can be auto-added, quantity-updated, or flagged as unreferenced by assembly merges - `source = 'manual'` entries are never touched by assembly merges
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: kindred/silo#44