Fix dedup: PRAGMA foreign_keys=OFF before delete, alert on seed error

FOREIGN KEY constraint failed on desktop (rusqlite enables FK by default).
Android was silently failing too. Fix: disable FK during dedup, re-enable after.

Tested: desktop 16675 → 8330 products, idx_products_dedup created.
Also: alert() instead of boot-label for seed errors (visible to user).
This commit is contained in:
4aiman 2026-06-01 21:25:23 +03:00
commit 46ad9022c2
2 changed files with 6 additions and 2 deletions

View file

@ -96,7 +96,7 @@ async function init() {
const result = await window.__TAURI__.core.invoke("import_seed_products");
if (unlisten) unlisten();
} catch (e) { console.error("[SEED]", e); document.getElementById("boot-label").textContent = "Seed: " + e; }
} catch (e) { console.error("[SEED]", e); alert("Seed error: " + e); }
// Apply safe area insets (Android status bar / nav bar)
try {

View file

@ -140,7 +140,9 @@ fn import_seed_products(app: tauri::AppHandle) -> Result<String, String> {
}
// Dedup ALWAYS runs (even if seed version matches) — fixes legacy duplicates.
// Fast: <0.2s with index. No-op if no dupes exist.
// Disable FK checks during dedup (refs reassigned before delete, but rusqlite enforces FK by default)
let _ = main_db.execute("PRAGMA foreign_keys = OFF", []);
let _ = main_db.execute(
"CREATE INDEX IF NOT EXISTS idx_products_name_lower ON products(name_lower)", []);
@ -178,6 +180,8 @@ fn import_seed_products(app: tauri::AppHandle) -> Result<String, String> {
ON products(name_lower, source, COALESCE(store_id, ''))
WHERE name_lower IS NOT NULL", []);
let _ = main_db.execute("PRAGMA foreign_keys = ON", []);
// Check seed version — skip import if already up to date
let current_version: i64 = main_db.query_row(
"SELECT COALESCE((SELECT value FROM profile WHERE key='seed_version'), '0')",