Kaynağa Gözat

INSERT with ON CONFLICT

losfair 2 yıl önce
ebeveyn
işleme
99c66e273b
1 değiştirilmiş dosya ile 5 ekleme ve 2 silme
  1. 5 2
      rustpad-server/src/database.rs

+ 5 - 2
rustpad-server/src/database.rs

@@ -49,10 +49,13 @@ impl Database {
     pub async fn store(&self, document_id: &str, document: &PersistedDocument) -> Result<()> {
     pub async fn store(&self, document_id: &str, document: &PersistedDocument) -> Result<()> {
         let result = sqlx::query(
         let result = sqlx::query(
             r#"
             r#"
-INSERT OR REPLACE INTO
+INSERT INTO
     document (id, text, language)
     document (id, text, language)
 VALUES
 VALUES
-    ($1, $2, $3)"#,
+    ($1, $2, $3)
+ON CONFLICT(id) DO UPDATE SET
+    text = excluded.text,
+    language = excluded.language"#,
         )
         )
         .bind(document_id)
         .bind(document_id)
         .bind(&document.text)
         .bind(&document.text)