From 23065a5aa30d9b370ed36d00fd32b86992a680fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 27 Mar 2020 16:00:40 +0100 Subject: [PATCH] crypto: Use an upsert for the account saving. --- src/crypto/store/sqlite.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/crypto/store/sqlite.rs b/src/crypto/store/sqlite.rs index 97034624..348de1b8 100644 --- a/src/crypto/store/sqlite.rs +++ b/src/crypto/store/sqlite.rs @@ -225,9 +225,15 @@ impl CryptoStore for SqliteStore { let mut connection = self.connection.lock().await; query( - "INSERT OR IGNORE INTO accounts ( + "INSERT INTO accounts ( user_id, device_id, pickle, shared - ) VALUES (?, ?, ?, ?)", + ) VALUES (?1, ?2, ?3, ?4) + ON CONFLICT(user_id, device_id) DO UPDATE SET + pickle = ?3, + shared = ?4 + WHERE user_id = ?1 and + device_id = ?2 + ", ) .bind(&*self.user_id) .bind(&*self.device_id) @@ -236,20 +242,6 @@ impl CryptoStore for SqliteStore { .execute(&mut *connection) .await?; - query( - "UPDATE accounts - SET pickle = ?, - shared = ? - WHERE user_id = ? and - device_id = ?", - ) - .bind(pickle) - .bind(acc.shared) - .bind(&*self.user_id) - .bind(&*self.device_id) - .execute(&mut *connection) - .await?; - let account_id: (i64,) = query_as("SELECT id FROM accounts WHERE user_id = ? and device_id = ?") .bind(&*self.user_id)