Add database transaction instead of multiple connections
This commit is contained in:
@@ -147,7 +147,8 @@ impl Database {
|
|||||||
song: &str,
|
song: &str,
|
||||||
rating: i64,
|
rating: i64,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut conn = self.pool.acquire().await?;
|
// Begin the transaction
|
||||||
|
let mut tx = self.pool.begin().await?;
|
||||||
|
|
||||||
// Add interpret
|
// Add interpret
|
||||||
let _ = sqlx::query(
|
let _ = sqlx::query(
|
||||||
@@ -157,7 +158,7 @@ impl Database {
|
|||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(interpret)
|
.bind(interpret)
|
||||||
.execute(&mut *conn)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Get the interpret ID
|
// Get the interpret ID
|
||||||
@@ -168,7 +169,7 @@ impl Database {
|
|||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(interpret)
|
.bind(interpret)
|
||||||
.fetch_one(&mut *conn)
|
.fetch_one(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let interpret_id: i64 = record.try_get("id")?;
|
let interpret_id: i64 = record.try_get("id")?;
|
||||||
@@ -182,7 +183,7 @@ impl Database {
|
|||||||
)
|
)
|
||||||
.bind(song)
|
.bind(song)
|
||||||
.bind(interpret_id)
|
.bind(interpret_id)
|
||||||
.execute(&mut *conn)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Get the song ID
|
// Get the song ID
|
||||||
@@ -193,7 +194,7 @@ impl Database {
|
|||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(song)
|
.bind(song)
|
||||||
.fetch_one(&mut *conn)
|
.fetch_one(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let song_id: i64 = record.try_get("id")?;
|
let song_id: i64 = record.try_get("id")?;
|
||||||
@@ -208,9 +209,12 @@ impl Database {
|
|||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.bind(song_id)
|
.bind(song_id)
|
||||||
.bind(rating)
|
.bind(rating)
|
||||||
.execute(&mut *conn)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// Commit the transaction to save everything!
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user