This commit is contained in:
greeneclipse committed 2026-11-15 10:04:44 +01:00
1 parent 41f1a7dbe4
commit e7964c58c8
13 files changed
+1215 -24

No files matched your search

@@ -0,0 +1,36 @@
-- +goose Up
-- One row per (company, EAN, feed): the feed's own mapped data, kept separately so a
-- product that appears in several vendor feeds can be merged instead of the last
-- synced feed overwriting the others. raw_products.mapped_data stays the merged view.
CREATE TABLE IF NOT EXISTS raw_product_feed_rows (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
company_id UUID NOT NULL REFERENCES companies(id) ON DELETE CASCADE,
gtin TEXT NOT NULL,
feed_id UUID NOT NULL REFERENCES input_feeds(id) ON DELETE CASCADE,
raw_data JSONB NOT NULL DEFAULT '{}'::jsonb,
mapped_data JSONB NOT NULL DEFAULT '{}'::jsonb,
content_hash TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (company_id, gtin, feed_id)
);
CREATE INDEX IF NOT EXISTS raw_product_feed_rows_company_gtin_idx
ON raw_product_feed_rows (company_id, gtin);
CREATE INDEX IF NOT EXISTS raw_product_feed_rows_feed_idx
ON raw_product_feed_rows (feed_id);
-- Which feed supplied each merged field: {"feeds":[…],"fields":{"description":{…}}}.
ALTER TABLE raw_products ADD COLUMN IF NOT EXISTS feed_field_sources JSONB;
-- Existing products were written by exactly one feed (raw_products.feed_id); keep that
-- feed's data as its row so the first sync of another feed merges with it.
INSERT INTO raw_product_feed_rows (company_id, gtin, feed_id, raw_data, mapped_data)
SELECT company_id, gtin, feed_id, COALESCE(raw_data, '{}'::jsonb), COALESCE(mapped_data, '{}'::jsonb) - '_sync_changes'
FROM raw_products
WHERE feed_id IS NOT NULL
ON CONFLICT (company_id, gtin, feed_id) DO NOTHING;
-- +goose Down
ALTER TABLE raw_products DROP COLUMN IF EXISTS feed_field_sources;
DROP TABLE IF EXISTS raw_product_feed_rows;