refactor: migrate ingredient aliases/categories to dedicated tables, drop spoonacular_id

- migration 012: create ingredient_categories + ingredient_category_translations
  tables (7 slugs, Russian names); add ingredient_aliases (ingredient_id, lang,
  alias) with GIN trigram index; migrate aliases JSONB from ingredient_mappings
  and ingredient_translations; drop aliases columns and spoonacular_id; add
  UNIQUE (canonical_name) as conflict key
- ingredient/model: remove SpoonacularID, add CategoryName for localized display
- ingredient/repository: conflict on canonical_name; GetByID/Search join category
  translations and aliases lateral; new UpsertAliases (pgx batch),
  UpsertCategoryTranslation; remove GetBySpoonacularID; split scan helpers into
  scanMappingWrite / scanMappingRead
- gemini/recognition: add IngredientTranslation type; IngredientClassification
  now carries Translations []IngredientTranslation instead of CanonicalNameRu;
  update ClassifyIngredient prompt to English with structured translations array
- recognition/handler: update ingredientRepo interface; saveClassification uses
  UpsertAliases and iterates Translations
- recipe/model: remove SpoonacularID from RecipeIngredient
- integration tests: remove SpoonacularID fixtures, replace GetBySpoonacularID
  tests with GetByID, add UpsertAliases and UpsertCategoryTranslation tests
- Flutter: remove canonicalNameRu from IngredientMapping, add categoryName;
  displayName returns server-resolved canonicalName; regenerate .g.dart

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-10 14:40:07 +02:00
parent c9ddb708b1
commit a225f6c47a
9 changed files with 373 additions and 152 deletions

View File

@@ -22,18 +22,16 @@ func NewRepository(pool *pgxpool.Pool) *Repository {
}
// Upsert inserts or updates an ingredient mapping (English canonical content).
// Conflict is resolved on spoonacular_id when set.
// Conflict is resolved on canonical_name.
func (r *Repository) Upsert(ctx context.Context, m *IngredientMapping) (*IngredientMapping, error) {
query := `
INSERT INTO ingredient_mappings (
canonical_name, spoonacular_id, aliases,
canonical_name,
category, default_unit,
calories_per_100g, protein_per_100g, fat_per_100g, carbs_per_100g, fiber_per_100g,
storage_days
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ON CONFLICT (spoonacular_id) DO UPDATE SET
canonical_name = EXCLUDED.canonical_name,
aliases = EXCLUDED.aliases,
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (canonical_name) DO UPDATE SET
category = EXCLUDED.category,
default_unit = EXCLUDED.default_unit,
calories_per_100g = EXCLUDED.calories_per_100g,
@@ -43,62 +41,47 @@ func (r *Repository) Upsert(ctx context.Context, m *IngredientMapping) (*Ingredi
fiber_per_100g = EXCLUDED.fiber_per_100g,
storage_days = EXCLUDED.storage_days,
updated_at = now()
RETURNING id, canonical_name, spoonacular_id, aliases,
category, default_unit,
RETURNING id, canonical_name, category, default_unit,
calories_per_100g, protein_per_100g, fat_per_100g, carbs_per_100g, fiber_per_100g,
storage_days, created_at, updated_at`
row := r.pool.QueryRow(ctx, query,
m.CanonicalName, m.SpoonacularID, m.Aliases,
m.CanonicalName,
m.Category, m.DefaultUnit,
m.CaloriesPer100g, m.ProteinPer100g, m.FatPer100g, m.CarbsPer100g, m.FiberPer100g,
m.StorageDays,
)
return scanMapping(row)
}
// GetBySpoonacularID returns an ingredient mapping by Spoonacular ID.
// CanonicalName is resolved for the language stored in ctx.
// Returns nil, nil if not found.
func (r *Repository) GetBySpoonacularID(ctx context.Context, id int) (*IngredientMapping, error) {
lang := locale.FromContext(ctx)
query := `
SELECT im.id,
COALESCE(it.name, im.canonical_name) AS canonical_name,
im.spoonacular_id, im.aliases,
im.category, im.default_unit,
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
im.storage_days, im.created_at, im.updated_at
FROM ingredient_mappings im
LEFT JOIN ingredient_translations it ON it.ingredient_id = im.id AND it.lang = $2
WHERE im.spoonacular_id = $1`
row := r.pool.QueryRow(ctx, query, id, lang)
m, err := scanMapping(row)
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return m, err
return scanMappingWrite(row)
}
// GetByID returns an ingredient mapping by UUID.
// CanonicalName is resolved for the language stored in ctx.
// CanonicalName and aliases are resolved for the language stored in ctx.
// Returns nil, nil if not found.
func (r *Repository) GetByID(ctx context.Context, id string) (*IngredientMapping, error) {
lang := locale.FromContext(ctx)
query := `
SELECT im.id,
COALESCE(it.name, im.canonical_name) AS canonical_name,
im.spoonacular_id, im.aliases,
im.category, im.default_unit,
im.category,
COALESCE(ict.name, im.category) AS category_name,
im.default_unit,
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
im.storage_days, im.created_at, im.updated_at
im.storage_days, im.created_at, im.updated_at,
COALESCE(al.aliases, '[]'::json) AS aliases
FROM ingredient_mappings im
LEFT JOIN ingredient_translations it ON it.ingredient_id = im.id AND it.lang = $2
LEFT JOIN ingredient_translations it
ON it.ingredient_id = im.id AND it.lang = $2
LEFT JOIN ingredient_category_translations ict
ON ict.category_slug = im.category AND ict.lang = $2
LEFT JOIN LATERAL (
SELECT json_agg(ia.alias ORDER BY ia.alias) AS aliases
FROM ingredient_aliases ia
WHERE ia.ingredient_id = im.id AND ia.lang = $2
) al ON true
WHERE im.id = $1`
row := r.pool.QueryRow(ctx, query, id, lang)
m, err := scanMapping(row)
m, err := scanMappingRead(row)
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
@@ -120,8 +103,7 @@ func (r *Repository) FuzzyMatch(ctx context.Context, name string) (*IngredientMa
}
// Search finds ingredient mappings matching the query string.
// Searches English aliases/name and the translated name for the language in ctx.
// Uses a three-level strategy: exact aliases match, ILIKE, and pg_trgm similarity.
// Searches aliases table and translated names for the language in ctx.
func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*IngredientMapping, error) {
if limit <= 0 {
limit = 10
@@ -130,17 +112,31 @@ func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*In
q := `
SELECT im.id,
COALESCE(it.name, im.canonical_name) AS canonical_name,
im.spoonacular_id, im.aliases,
im.category, im.default_unit,
im.category,
COALESCE(ict.name, im.category) AS category_name,
im.default_unit,
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
im.storage_days, im.created_at, im.updated_at
im.storage_days, im.created_at, im.updated_at,
COALESCE(al.aliases, '[]'::json) AS aliases
FROM ingredient_mappings im
LEFT JOIN ingredient_translations it ON it.ingredient_id = im.id AND it.lang = $3
WHERE im.aliases @> to_jsonb(lower($1)::text)
OR it.aliases @> to_jsonb(lower($1)::text)
OR im.canonical_name ILIKE '%' || $1 || '%'
OR it.name ILIKE '%' || $1 || '%'
OR similarity(COALESCE(it.name, im.canonical_name), $1) > 0.3
LEFT JOIN ingredient_translations it
ON it.ingredient_id = im.id AND it.lang = $3
LEFT JOIN ingredient_category_translations ict
ON ict.category_slug = im.category AND ict.lang = $3
LEFT JOIN LATERAL (
SELECT json_agg(ia.alias ORDER BY ia.alias) AS aliases
FROM ingredient_aliases ia
WHERE ia.ingredient_id = im.id AND ia.lang = $3
) al ON true
WHERE EXISTS (
SELECT 1 FROM ingredient_aliases ia
WHERE ia.ingredient_id = im.id
AND (ia.lang = $3 OR ia.lang = 'en')
AND ia.alias ILIKE '%' || $1 || '%'
)
OR im.canonical_name ILIKE '%' || $1 || '%'
OR it.name ILIKE '%' || $1 || '%'
OR similarity(COALESCE(it.name, im.canonical_name), $1) > 0.3
ORDER BY similarity(COALESCE(it.name, im.canonical_name), $1) DESC
LIMIT $2`
@@ -149,7 +145,7 @@ func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*In
return nil, fmt.Errorf("search ingredient_mappings: %w", err)
}
defer rows.Close()
return collectMappings(rows)
return collectMappingsRead(rows)
}
// Count returns the total number of ingredient mappings.
@@ -165,7 +161,7 @@ func (r *Repository) Count(ctx context.Context) (int, error) {
// given language, ordered by id.
func (r *Repository) ListMissingTranslation(ctx context.Context, lang string, limit, offset int) ([]*IngredientMapping, error) {
query := `
SELECT im.id, im.canonical_name, im.spoonacular_id, im.aliases,
SELECT im.id, im.canonical_name,
im.category, im.default_unit,
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
im.storage_days, im.created_at, im.updated_at
@@ -182,53 +178,119 @@ func (r *Repository) ListMissingTranslation(ctx context.Context, lang string, li
return nil, fmt.Errorf("list missing translation (%s): %w", lang, err)
}
defer rows.Close()
return collectMappings(rows)
return collectMappingsWrite(rows)
}
// UpsertTranslation inserts or replaces a translation for an ingredient mapping.
func (r *Repository) UpsertTranslation(ctx context.Context, id, lang, name string, aliases json.RawMessage) error {
// UpsertTranslation inserts or replaces a name translation for an ingredient.
func (r *Repository) UpsertTranslation(ctx context.Context, id, lang, name string) error {
query := `
INSERT INTO ingredient_translations (ingredient_id, lang, name, aliases)
VALUES ($1, $2, $3, $4)
ON CONFLICT (ingredient_id, lang) DO UPDATE SET
name = EXCLUDED.name,
aliases = EXCLUDED.aliases`
INSERT INTO ingredient_translations (ingredient_id, lang, name)
VALUES ($1, $2, $3)
ON CONFLICT (ingredient_id, lang) DO UPDATE SET name = EXCLUDED.name`
if _, err := r.pool.Exec(ctx, query, id, lang, name, aliases); err != nil {
if _, err := r.pool.Exec(ctx, query, id, lang, name); err != nil {
return fmt.Errorf("upsert ingredient translation %s/%s: %w", id, lang, err)
}
return nil
}
// --- helpers ---
// UpsertAliases inserts aliases for a given ingredient and language.
// Each alias is inserted with ON CONFLICT DO NOTHING, so duplicates are skipped.
func (r *Repository) UpsertAliases(ctx context.Context, id, lang string, aliases []string) error {
if len(aliases) == 0 {
return nil
}
batch := &pgx.Batch{}
for _, alias := range aliases {
batch.Queue(
`INSERT INTO ingredient_aliases (ingredient_id, lang, alias) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`,
id, lang, alias,
)
}
results := r.pool.SendBatch(ctx, batch)
defer results.Close()
for range aliases {
if _, err := results.Exec(); err != nil {
return fmt.Errorf("upsert ingredient alias %s/%s: %w", id, lang, err)
}
}
return nil
}
func scanMapping(row pgx.Row) (*IngredientMapping, error) {
// UpsertCategoryTranslation inserts or replaces a localized category name.
func (r *Repository) UpsertCategoryTranslation(ctx context.Context, slug, lang, name string) error {
query := `
INSERT INTO ingredient_category_translations (category_slug, lang, name)
VALUES ($1, $2, $3)
ON CONFLICT (category_slug, lang) DO UPDATE SET name = EXCLUDED.name`
if _, err := r.pool.Exec(ctx, query, slug, lang, name); err != nil {
return fmt.Errorf("upsert category translation %s/%s: %w", slug, lang, err)
}
return nil
}
// --- scan helpers ---
// scanMappingWrite scans rows from Upsert / ListMissingTranslation queries
// (no aliases lateral join, no category_name).
func scanMappingWrite(row pgx.Row) (*IngredientMapping, error) {
var m IngredientMapping
var aliases []byte
err := row.Scan(
&m.ID, &m.CanonicalName, &m.SpoonacularID, &aliases,
&m.Category, &m.DefaultUnit,
&m.ID, &m.CanonicalName, &m.Category, &m.DefaultUnit,
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt,
)
if err != nil {
return nil, err
}
m.Aliases = json.RawMessage("[]")
return &m, nil
}
// scanMappingRead scans rows from GetByID / Search queries
// (includes category_name and aliases lateral join).
func scanMappingRead(row pgx.Row) (*IngredientMapping, error) {
var m IngredientMapping
var aliases []byte
err := row.Scan(
&m.ID, &m.CanonicalName, &m.Category, &m.CategoryName, &m.DefaultUnit,
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt, &aliases,
)
if err != nil {
return nil, err
}
m.Aliases = json.RawMessage(aliases)
return &m, nil
}
func collectMappings(rows pgx.Rows) ([]*IngredientMapping, error) {
func collectMappingsWrite(rows pgx.Rows) ([]*IngredientMapping, error) {
var result []*IngredientMapping
for rows.Next() {
var m IngredientMapping
if err := rows.Scan(
&m.ID, &m.CanonicalName, &m.Category, &m.DefaultUnit,
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt,
); err != nil {
return nil, fmt.Errorf("scan mapping: %w", err)
}
m.Aliases = json.RawMessage("[]")
result = append(result, &m)
}
return result, rows.Err()
}
func collectMappingsRead(rows pgx.Rows) ([]*IngredientMapping, error) {
var result []*IngredientMapping
for rows.Next() {
var m IngredientMapping
var aliases []byte
if err := rows.Scan(
&m.ID, &m.CanonicalName, &m.SpoonacularID, &aliases,
&m.Category, &m.DefaultUnit,
&m.ID, &m.CanonicalName, &m.Category, &m.CategoryName, &m.DefaultUnit,
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt,
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt, &aliases,
); err != nil {
return nil, fmt.Errorf("scan mapping: %w", err)
}