From 197e7be2a875f4cb1349e3d3c107b7d2b4ac7b7d Mon Sep 17 00:00:00 2001
From: ryan <ryannganga13325@gmail.com>
Date: Tue, 25 Feb 2025 02:18:19 +0300
Subject: [PATCH] feat(db): replace item insertion with upsert operation.

---
 src/db.rs | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/src/db.rs b/src/db.rs
index 41d5cd7..196f765 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -248,10 +248,15 @@ mod db_impl {
             };
         
             // 4. Item insertion
-            log!("[DB] Inserting item into items table");
-            match tx.execute(
-                "INSERT INTO items (id, url_id, name, description, wikidata_id) 
-                VALUES (?, ?, ?, ?, ?)",
+            log!("[DB] Upserting item");
+            tx.execute(
+                "INSERT INTO items (id, url_id, name, description, wikidata_id)
+                VALUES (?, ?, ?, ?, ?)
+                ON CONFLICT(id) DO UPDATE SET
+                    url_id = excluded.url_id,
+                    name = excluded.name,
+                    description = excluded.description,
+                    wikidata_id = excluded.wikidata_id",
                 rusqlite::params![
                     &item.id,
                     url_id,
@@ -259,13 +264,8 @@ mod db_impl {
                     &item.description,
                     &item.wikidata_id
                 ],
-            ) {
-                Ok(_) => log!("[DB] Item inserted successfully"),
-                Err(e) => {
-                    log!("[DB] Item insert error: {:?}", e);
-                    return Err(e.into());
-                }
-            }
+            )?;
+            log!("[DB] Item upserted successfully");
             // Property handling with enhanced logging
             log!("[DB] Processing {} properties", item.custom_properties.len());
             for (prop, value) in &item.custom_properties {