Update
This commit is contained in:
1 parent
41f1a7dbe4
commit
e7964c58c8
13 files changed
+1215
-24
No files matched your search
@@ -38,6 +38,10 @@ type pendingProduct struct {
|
||||
RawData map[string]string
|
||||
MappedData map[string]any
|
||||
ContentHash string
|
||||
// Cross-feed merge result (primary locale only); empty means "leave as is".
|
||||
Sources string // raw_products.feed_field_sources JSON
|
||||
FeedIDsJSON string // raw_products.feed_ids JSON array
|
||||
PrimaryFeed string // richest feed, becomes raw_products.feed_id
|
||||
}
|
||||
|
||||
// Sync downloads the feed URL, parses CSV/XML, applies mappings, and upserts raw_products
|
||||
@@ -468,34 +472,50 @@ const (
|
||||
upsertSQLInsertSet = `
|
||||
INSERT INTO raw_products (
|
||||
company_id, gtin, feed_id, raw_data, mapped_data, sync_job_id,
|
||||
is_processed, processing_status, updated_at, source_localizations
|
||||
is_processed, processing_status, updated_at, source_localizations,
|
||||
feed_ids, feed_field_sources
|
||||
)
|
||||
SELECT $1, v.gtin, $2, v.raw_data::jsonb, v.mapped_data::jsonb, $3, false, 'unprocessed', now(),
|
||||
jsonb_build_object($7::text, v.mapped_data::jsonb)
|
||||
FROM unnest($4::text[], $5::text[], $6::text[]) AS v(gtin, raw_data, mapped_data)
|
||||
SELECT $1, v.gtin, COALESCE(NULLIF(v.primary_feed, '')::uuid, $2), v.raw_data::jsonb, v.mapped_data::jsonb, $3, false, 'unprocessed', now(),
|
||||
jsonb_build_object($7::text, v.mapped_data::jsonb),
|
||||
NULLIF(v.feed_ids, '')::jsonb, NULLIF(v.sources, '')::jsonb
|
||||
FROM unnest($4::text[], $5::text[], $6::text[], $9::text[], $10::text[], $11::text[])
|
||||
AS v(gtin, raw_data, mapped_data, primary_feed, feed_ids, sources)
|
||||
ON CONFLICT (company_id, gtin) DO UPDATE SET
|
||||
feed_id = EXCLUDED.feed_id,
|
||||
raw_data = CASE WHEN $8 THEN EXCLUDED.raw_data ELSE raw_products.raw_data END,
|
||||
mapped_data = CASE WHEN $8 THEN EXCLUDED.mapped_data ELSE raw_products.mapped_data END,
|
||||
source_localizations = jsonb_set(COALESCE(raw_products.source_localizations,'{}'::jsonb),ARRAY[$7::text],EXCLUDED.mapped_data,true),
|
||||
feed_ids = CASE WHEN $8 AND EXCLUDED.feed_ids IS NOT NULL THEN EXCLUDED.feed_ids ELSE raw_products.feed_ids END,
|
||||
feed_field_sources = CASE WHEN $8 AND EXCLUDED.feed_field_sources IS NOT NULL THEN EXCLUDED.feed_field_sources ELSE raw_products.feed_field_sources END,
|
||||
sync_job_id = EXCLUDED.sync_job_id,
|
||||
is_processed = CASE WHEN $8 THEN false ELSE raw_products.is_processed END,
|
||||
processing_status = CASE WHEN $8 THEN 'unprocessed' ELSE raw_products.processing_status END,
|
||||
updated_at = now()`
|
||||
// Touch: content unchanged, but the owning feed / contributing feeds / field
|
||||
// sources may still have changed (another feed was added), so refresh those.
|
||||
upsertSQLTouchSet = `
|
||||
UPDATE raw_products SET sync_job_id = $3, feed_id = $4, updated_at = now()
|
||||
WHERE company_id = $1 AND id = ANY($2::uuid[])`
|
||||
UPDATE raw_products AS r SET
|
||||
sync_job_id = $3,
|
||||
feed_id = COALESCE(NULLIF(v.primary_feed, '')::uuid, $4),
|
||||
feed_ids = CASE WHEN v.feed_ids <> '' THEN v.feed_ids::jsonb ELSE r.feed_ids END,
|
||||
feed_field_sources = CASE WHEN v.sources <> '' THEN v.sources::jsonb ELSE r.feed_field_sources END,
|
||||
updated_at = now()
|
||||
FROM unnest($2::uuid[], $5::text[], $6::text[], $7::text[]) AS v(id, primary_feed, feed_ids, sources)
|
||||
WHERE r.company_id = $1 AND r.id = v.id`
|
||||
upsertSQLUpdateSet = `
|
||||
UPDATE raw_products AS r SET
|
||||
feed_id = $2,
|
||||
feed_id = COALESCE(NULLIF(v.primary_feed, '')::uuid, $2),
|
||||
raw_data = CASE WHEN $8 THEN v.raw_data::jsonb ELSE r.raw_data END,
|
||||
mapped_data = CASE WHEN $8 THEN v.mapped_data::jsonb ELSE r.mapped_data END,
|
||||
source_localizations = jsonb_set(COALESCE(r.source_localizations,'{}'::jsonb),ARRAY[$7::text],v.mapped_data::jsonb,true),
|
||||
feed_ids = CASE WHEN $8 AND v.feed_ids <> '' THEN v.feed_ids::jsonb ELSE r.feed_ids END,
|
||||
feed_field_sources = CASE WHEN $8 AND v.sources <> '' THEN v.sources::jsonb ELSE r.feed_field_sources END,
|
||||
sync_job_id = $3,
|
||||
is_processed = CASE WHEN $8 THEN false ELSE r.is_processed END,
|
||||
processing_status = CASE WHEN $8 THEN 'unprocessed' ELSE r.processing_status END,
|
||||
updated_at = now()
|
||||
FROM unnest($4::uuid[], $5::text[], $6::text[]) AS v(id, raw_data, mapped_data)
|
||||
FROM unnest($4::uuid[], $5::text[], $6::text[], $9::text[], $10::text[], $11::text[])
|
||||
AS v(id, raw_data, mapped_data, primary_feed, feed_ids, sources)
|
||||
WHERE r.id = v.id AND r.company_id = $1`
|
||||
)
|
||||
|
||||
@@ -514,6 +534,10 @@ type upsertOp struct {
|
||||
rawJSON []byte
|
||||
mappedJSON []byte
|
||||
changes []string
|
||||
// Cross-feed merge metadata (empty when the merge did not run).
|
||||
primaryFeed string
|
||||
feedIDs string
|
||||
sources string
|
||||
}
|
||||
|
||||
// classifyUpsertOps decides insert / touch / update per row without DB I/O so a chunk
|
||||
@@ -539,7 +563,8 @@ func classifyUpsertOps(chunk []pendingProduct, existing map[string]existingProdu
|
||||
}
|
||||
ops = append(ops, upsertOp{
|
||||
kind: upsertOpInsert, gtin: p.GTIN, rawJSON: rawJSON, mappedJSON: mappedJSON,
|
||||
changes: []string{"new"},
|
||||
changes: []string{"new"},
|
||||
primaryFeed: p.PrimaryFeed, feedIDs: p.FeedIDsJSON, sources: p.Sources,
|
||||
})
|
||||
continue
|
||||
}
|
||||
@@ -562,7 +587,10 @@ func classifyUpsertOps(chunk []pendingProduct, existing map[string]existingProdu
|
||||
continue
|
||||
}
|
||||
if bytesEqualJSON(stripSyncChangesBytes(existingMapped), plainJSON) {
|
||||
ops = append(ops, upsertOp{kind: upsertOpTouch, id: ex.ID})
|
||||
ops = append(ops, upsertOp{
|
||||
kind: upsertOpTouch, id: ex.ID,
|
||||
primaryFeed: p.PrimaryFeed, feedIDs: p.FeedIDsJSON, sources: p.Sources,
|
||||
})
|
||||
continue
|
||||
}
|
||||
changes := detectMappedChanges(existingMapped, cleanMapped)
|
||||
@@ -574,7 +602,8 @@ func classifyUpsertOps(chunk []pendingProduct, existing map[string]existingProdu
|
||||
}
|
||||
ops = append(ops, upsertOp{
|
||||
kind: upsertOpUpdate, id: ex.ID, rawJSON: rawJSON, mappedJSON: mappedJSON,
|
||||
changes: changes,
|
||||
changes: changes,
|
||||
primaryFeed: p.PrimaryFeed, feedIDs: p.FeedIDsJSON, sources: p.Sources,
|
||||
})
|
||||
}
|
||||
return ops, skipped
|
||||
@@ -651,6 +680,13 @@ func (s *Service) upsertChunk(ctx context.Context, companyID, feedID, jobID uuid
|
||||
if !ok {
|
||||
locale = syncLocale{code: company.LoadLanguage(ctx, s.Pool, companyID), primary: true}
|
||||
}
|
||||
// Several vendor feeds can carry the same EAN: store this feed's row and merge all
|
||||
// feeds' data before comparing against the existing product.
|
||||
if locale.primary {
|
||||
if err := s.mergeChunkAcrossFeeds(ctx, companyID, feedID, chunk); err != nil {
|
||||
return 0, 0, 0, deltas, err
|
||||
}
|
||||
}
|
||||
ops, skipped := classifyUpsertOps(chunk, existing, locale)
|
||||
if len(ops) == 0 {
|
||||
deltas.Skipped = skipped
|
||||
@@ -664,34 +700,40 @@ func (s *Service) upsertChunk(ctx context.Context, companyID, feedID, jobID uuid
|
||||
gtinCol := make([]string, len(inserts))
|
||||
rawCol := make([]string, len(inserts))
|
||||
mappedCol := make([]string, len(inserts))
|
||||
primCol, idsCol, srcCol := make([]string, len(inserts)), make([]string, len(inserts)), make([]string, len(inserts))
|
||||
for i, op := range inserts {
|
||||
gtinCol[i] = op.gtin
|
||||
rawCol[i] = string(op.rawJSON)
|
||||
mappedCol[i] = string(op.mappedJSON)
|
||||
primCol[i], idsCol[i], srcCol[i] = op.primaryFeed, op.feedIDs, op.sources
|
||||
deltas.addChanges(op.changes)
|
||||
}
|
||||
batch.Queue(upsertSQLInsertSet, companyID, feedID, jobID, gtinCol, rawCol, mappedCol, locale.code, locale.primary)
|
||||
batch.Queue(upsertSQLInsertSet, companyID, feedID, jobID, gtinCol, rawCol, mappedCol, locale.code, locale.primary, primCol, idsCol, srcCol)
|
||||
queued++
|
||||
}
|
||||
if len(touches) > 0 {
|
||||
ids := make([]uuid.UUID, len(touches))
|
||||
primCol, idsCol, srcCol := make([]string, len(touches)), make([]string, len(touches)), make([]string, len(touches))
|
||||
for i, op := range touches {
|
||||
ids[i] = op.id
|
||||
primCol[i], idsCol[i], srcCol[i] = op.primaryFeed, op.feedIDs, op.sources
|
||||
}
|
||||
batch.Queue(upsertSQLTouchSet, companyID, ids, jobID, feedID)
|
||||
batch.Queue(upsertSQLTouchSet, companyID, ids, jobID, feedID, primCol, idsCol, srcCol)
|
||||
queued++
|
||||
}
|
||||
if len(updates) > 0 {
|
||||
ids := make([]uuid.UUID, len(updates))
|
||||
rawCol := make([]string, len(updates))
|
||||
mappedCol := make([]string, len(updates))
|
||||
primCol, idsCol, srcCol := make([]string, len(updates)), make([]string, len(updates)), make([]string, len(updates))
|
||||
for i, op := range updates {
|
||||
ids[i] = op.id
|
||||
rawCol[i] = string(op.rawJSON)
|
||||
mappedCol[i] = string(op.mappedJSON)
|
||||
primCol[i], idsCol[i], srcCol[i] = op.primaryFeed, op.feedIDs, op.sources
|
||||
deltas.addChanges(op.changes)
|
||||
}
|
||||
batch.Queue(upsertSQLUpdateSet, companyID, feedID, jobID, ids, rawCol, mappedCol, locale.code, locale.primary)
|
||||
batch.Queue(upsertSQLUpdateSet, companyID, feedID, jobID, ids, rawCol, mappedCol, locale.code, locale.primary, primCol, idsCol, srcCol)
|
||||
queued++
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user