This commit is contained in:
2026-08-16 17:16:47 +02:00
parent 532d439c41
commit d161b28a10
5 changed files with 170 additions and 21 deletions
+28 -16
View File
@@ -37,13 +37,14 @@ func ClearWeakEnhanceHashes(ctx context.Context, pool *pgxpool.Pool, companyID u
// BackfillCategoriesFromMapped copies mapped_data category unique_ids onto
// processed_products.category when the processed category is empty/"none".
// Uses Go-side categoryUniqueIDFromMaps (nested/array shapes) and validates
// against the company taxonomy when categories exist.
// Uses Go-side categoryUniqueIDFromMaps (nested/array shapes), coerces display
// names onto taxonomy unique_ids, and validates against the company taxonomy
// when categories exist.
func BackfillCategoriesFromMapped(ctx context.Context, pool *pgxpool.Pool, companyID uuid.UUID) (updated int, err error) {
if pool == nil {
return 0, fmt.Errorf("nil pool")
}
valid, err := loadCompanyCategoryUniqueIDs(ctx, pool, companyID)
valid, namesByUID, err := loadCompanyCategoryTaxonomy(ctx, pool, companyID)
if err != nil {
return 0, err
}
@@ -87,10 +88,10 @@ func BackfillCategoriesFromMapped(ctx context.Context, pool *pgxpool.Pool, compa
if cat == "" {
continue
}
if len(valid) > 0 {
if _, ok := valid[cat]; !ok {
continue
}
if resolved := resolveCompanyCategoryUniqueID(cat, namesByUID, valid); resolved != "" {
cat = resolved
} else if len(valid) > 0 {
continue
}
ct, err := pool.Exec(ctx, `
UPDATE processed_products
@@ -121,25 +122,36 @@ func BackfillCategoriesFromMapped(ctx context.Context, pool *pgxpool.Pool, compa
}
func loadCompanyCategoryUniqueIDs(ctx context.Context, pool *pgxpool.Pool, companyID uuid.UUID) (map[string]struct{}, error) {
out := map[string]struct{}{}
valid, _, err := loadCompanyCategoryTaxonomy(ctx, pool, companyID)
return valid, err
}
func loadCompanyCategoryTaxonomy(ctx context.Context, pool *pgxpool.Pool, companyID uuid.UUID) (map[string]struct{}, map[string]string, error) {
valid := map[string]struct{}{}
namesByUID := map[string]string{}
rows, err := pool.Query(ctx, `
SELECT unique_id FROM categories
SELECT unique_id, name FROM categories
WHERE company_id = $1 AND COALESCE(NULLIF(btrim(unique_id), ''), '') <> ''`, companyID)
if err != nil {
return nil, err
return nil, nil, err
}
defer rows.Close()
for rows.Next() {
var uid string
if err := rows.Scan(&uid); err != nil {
return nil, err
var uid, name string
if err := rows.Scan(&uid, &name); err != nil {
return nil, nil, err
}
uid = strings.TrimSpace(uid)
if uid != "" {
out[uid] = struct{}{}
name = strings.TrimSpace(name)
if uid == "" {
continue
}
valid[uid] = struct{}{}
if name != "" {
namesByUID[uid] = name
}
}
return out, rows.Err()
return valid, namesByUID, rows.Err()
}
// FixCatalogHygiene clears poisoned enhance hashes and optionally backfills categories.