Fix dedup: use execute() not execute_batch(), propagate errors
execute_batch() silently failed on multi-statement dedup SQL. Split into 4 separate execute() calls with map_err — errors now propagate to JS and show to user instead of being swallowed.
This commit is contained in:
parent
b2b6b6a7ef
commit
01427941d4
1 changed files with 14 additions and 13 deletions
|
|
@ -160,8 +160,8 @@ fn import_seed_products(app: tauri::AppHandle) -> Result<String, String> {
|
|||
);
|
||||
|
||||
// Dedup existing products by (name_lower, source, store_id).
|
||||
// Reassign food_log and recipe_ingredients refs to kept (MIN id) product, then delete dupes.
|
||||
let _ = main_db.execute_batch(
|
||||
// Each statement separate — execute_batch can fail silently on multi-statement strings.
|
||||
main_db.execute(
|
||||
"UPDATE food_log SET product_id = (
|
||||
SELECT MIN(p2.id) FROM products p2
|
||||
WHERE p2.name_lower = (SELECT p3.name_lower FROM products p3 WHERE p3.id = food_log.product_id)
|
||||
|
|
@ -170,9 +170,10 @@ fn import_seed_products(app: tauri::AppHandle) -> Result<String, String> {
|
|||
) WHERE product_id NOT IN (
|
||||
SELECT MIN(id) FROM products WHERE name_lower IS NOT NULL
|
||||
GROUP BY name_lower, source, COALESCE(store_id,'')
|
||||
);
|
||||
)", []).map_err(|e| format!("dedup food_log: {}", e))?;
|
||||
|
||||
UPDATE recipe_ingredients SET product_id = (
|
||||
main_db.execute(
|
||||
"UPDATE recipe_ingredients SET product_id = (
|
||||
SELECT MIN(p2.id) FROM products p2
|
||||
WHERE p2.name_lower = (SELECT p3.name_lower FROM products p3 WHERE p3.id = recipe_ingredients.product_id)
|
||||
AND p2.source = (SELECT p3.source FROM products p3 WHERE p3.id = recipe_ingredients.product_id)
|
||||
|
|
@ -180,21 +181,21 @@ fn import_seed_products(app: tauri::AppHandle) -> Result<String, String> {
|
|||
) WHERE product_id NOT IN (
|
||||
SELECT MIN(id) FROM products WHERE name_lower IS NOT NULL
|
||||
GROUP BY name_lower, source, COALESCE(store_id,'')
|
||||
);
|
||||
)", []).map_err(|e| format!("dedup recipe_ingredients: {}", e))?;
|
||||
|
||||
DELETE FROM products WHERE name_lower IS NOT NULL
|
||||
main_db.execute(
|
||||
"DELETE FROM products WHERE name_lower IS NOT NULL
|
||||
AND id NOT IN (
|
||||
SELECT MIN(id) FROM products WHERE name_lower IS NOT NULL
|
||||
GROUP BY name_lower, source, COALESCE(store_id,'')
|
||||
);"
|
||||
);
|
||||
)", []).map_err(|e| format!("dedup delete: {}", e))?;
|
||||
|
||||
// Create UNIQUE index to prevent future duplicates
|
||||
let _ = main_db.execute_batch(
|
||||
// UNIQUE index to prevent future duplicates
|
||||
main_db.execute(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS idx_products_dedup
|
||||
ON main.products(name_lower, source, COALESCE(store_id, ''))
|
||||
WHERE name_lower IS NOT NULL;"
|
||||
);
|
||||
ON products(name_lower, source, COALESCE(store_id, ''))
|
||||
WHERE name_lower IS NOT NULL", [])
|
||||
.map_err(|e| format!("unique index: {}", e))?;
|
||||
|
||||
// Bulk INSERT new products from seed (INSERT OR IGNORE now works with unique index)
|
||||
let inserted = main_db.execute(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue