Browse Source
Validate sqlx insertions
Check query results whether the affected rows match our expectations
debug-statements
Mariusz Klochowicz
3 years ago
No known key found for this signature in database
GPG Key ID: 470C865699C8D4D
1 changed files with
11 additions and
2 deletions
-
daemon/src/db.rs
|
@ -16,7 +16,7 @@ pub async fn run_migrations(pool: &SqlitePool) -> anyhow::Result<()> { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub async fn insert_order(order: &Order, conn: &mut PoolConnection<Sqlite>) -> anyhow::Result<()> { |
|
|
pub async fn insert_order(order: &Order, conn: &mut PoolConnection<Sqlite>) -> anyhow::Result<()> { |
|
|
sqlx::query( |
|
|
let query_result = sqlx::query( |
|
|
r#"insert into orders ( |
|
|
r#"insert into orders ( |
|
|
uuid, |
|
|
uuid, |
|
|
trading_pair, |
|
|
trading_pair, |
|
@ -61,6 +61,10 @@ pub async fn insert_order(order: &Order, conn: &mut PoolConnection<Sqlite>) -> a |
|
|
.execute(conn) |
|
|
.execute(conn) |
|
|
.await?; |
|
|
.await?; |
|
|
|
|
|
|
|
|
|
|
|
if query_result.rows_affected() != 1 { |
|
|
|
|
|
anyhow::bail!("failed to insert order"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Ok(()) |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -112,7 +116,7 @@ pub async fn load_order_by_id( |
|
|
|
|
|
|
|
|
pub async fn insert_cfd(cfd: &Cfd, conn: &mut PoolConnection<Sqlite>) -> anyhow::Result<()> { |
|
|
pub async fn insert_cfd(cfd: &Cfd, conn: &mut PoolConnection<Sqlite>) -> anyhow::Result<()> { |
|
|
let state = serde_json::to_string(&cfd.state)?; |
|
|
let state = serde_json::to_string(&cfd.state)?; |
|
|
sqlx::query( |
|
|
let query_result = sqlx::query( |
|
|
r#" |
|
|
r#" |
|
|
insert into cfds ( |
|
|
insert into cfds ( |
|
|
order_id, |
|
|
order_id, |
|
@ -143,6 +147,11 @@ pub async fn insert_cfd(cfd: &Cfd, conn: &mut PoolConnection<Sqlite>) -> anyhow: |
|
|
.execute(conn) |
|
|
.execute(conn) |
|
|
.await?; |
|
|
.await?; |
|
|
|
|
|
|
|
|
|
|
|
// Should be 2 because we insert into cfds and cfd_states
|
|
|
|
|
|
if query_result.rows_affected() != 2 { |
|
|
|
|
|
anyhow::bail!("failed to insert cfd"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Ok(()) |
|
|
Ok(()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|