|
2 | 2 | //! |
3 | 3 | //! This module provides an implementation of the [`Database`] trait for |
4 | 4 | //! `PostgreSQL` using the `r2d2_postgres` connection pool. It configures the |
5 | | -//! PostgreSQL connection based on a URL, creates the necessary tables (for |
| 5 | +//! `PostgreSQL` connection based on a URL, creates the necessary tables (for |
6 | 6 | //! torrent metrics, torrent whitelist, and authentication keys), and implements |
7 | 7 | //! all CRUD operations required by the persistence layer. |
8 | 8 | //! |
@@ -344,15 +344,15 @@ impl Database for Postgres { |
344 | 344 | let info_hash_str = info_hash.to_string(); |
345 | 345 | self.with_connection(move |conn| { |
346 | 346 | let rows_affected = conn.execute("INSERT INTO whitelist (info_hash) VALUES ($1)", &[&info_hash_str])?; |
347 | | - Ok(rows_affected as usize) |
| 347 | + Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize")) |
348 | 348 | }) |
349 | 349 | } |
350 | 350 |
|
351 | 351 | fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> { |
352 | 352 | let info_hash_str = info_hash.to_string(); |
353 | 353 | self.with_connection(move |conn| { |
354 | 354 | let rows_affected = conn.execute("DELETE FROM whitelist WHERE info_hash = $1", &[&info_hash_str])?; |
355 | | - Ok(rows_affected as usize) |
| 355 | + Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize")) |
356 | 356 | }) |
357 | 357 | } |
358 | 358 |
|
@@ -384,32 +384,29 @@ impl Database for Postgres { |
384 | 384 | let key_str = auth_key.key.to_string(); |
385 | 385 | let valid_until = auth_key.valid_until; |
386 | 386 | self.with_connection(move |conn| { |
387 | | - let rows_affected = match valid_until { |
388 | | - Some(valid_until) => { |
389 | | - let valid_until_i64 = i64::try_from(valid_until.as_secs()).unwrap(); |
390 | | - conn.execute( |
391 | | - "INSERT INTO keys (key, valid_until) VALUES ($1, $2)", |
392 | | - &[&key_str, &valid_until_i64], |
393 | | - )? |
394 | | - } |
395 | | - None => { |
396 | | - let null_value: Option<i64> = None; |
397 | | - conn.execute( |
398 | | - "INSERT INTO keys (key, valid_until) VALUES ($1, $2)", |
399 | | - &[&key_str, &null_value], |
400 | | - )? |
401 | | - } |
| 387 | + let rows_affected = if let Some(valid_until) = valid_until { |
| 388 | + let valid_until_i64 = i64::try_from(valid_until.as_secs()).unwrap(); |
| 389 | + conn.execute( |
| 390 | + "INSERT INTO keys (key, valid_until) VALUES ($1, $2)", |
| 391 | + &[&key_str, &valid_until_i64], |
| 392 | + )? |
| 393 | + } else { |
| 394 | + let null_value: Option<i64> = None; |
| 395 | + conn.execute( |
| 396 | + "INSERT INTO keys (key, valid_until) VALUES ($1, $2)", |
| 397 | + &[&key_str, &null_value], |
| 398 | + )? |
402 | 399 | }; |
403 | 400 |
|
404 | | - Ok(rows_affected as usize) |
| 401 | + Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize")) |
405 | 402 | }) |
406 | 403 | } |
407 | 404 |
|
408 | 405 | fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error> { |
409 | 406 | let key_str = key.to_string(); |
410 | 407 | self.with_connection(move |conn| { |
411 | 408 | let rows_affected = conn.execute("DELETE FROM keys WHERE key = $1", &[&key_str])?; |
412 | | - Ok(rows_affected as usize) |
| 409 | + Ok(usize::try_from(rows_affected).expect("rows affected should fit in usize")) |
413 | 410 | }) |
414 | 411 | } |
415 | 412 | } |
@@ -524,7 +521,7 @@ mod tests { |
524 | 521 | driver |
525 | 522 | } |
526 | 523 |
|
527 | | - /// Runs the full PostgreSQL driver test suite using testcontainers. |
| 524 | + /// Runs the full `PostgreSQL` driver test suite using testcontainers. |
528 | 525 | /// |
529 | 526 | /// Enable with: |
530 | 527 | /// `TORRUST_TRACKER_CORE_RUN_POSTGRES_DRIVER_TEST=true cargo test` |
@@ -555,19 +552,16 @@ mod tests { |
555 | 552 | Ok(()) |
556 | 553 | } |
557 | 554 |
|
558 | | - /// Runs the full PostgreSQL driver test suite against a local PostgreSQL |
| 555 | + /// Runs the full `PostgreSQL` driver test suite against a local `PostgreSQL` |
559 | 556 | /// instance specified via environment variable. |
560 | 557 | /// |
561 | 558 | /// Enable with: |
562 | 559 | /// `TORRUST_TRACKER_CORE_POSTGRES_DATABASE_URL="postgresql://user:pass@host:port/db" cargo test` |
563 | 560 | #[tokio::test] |
564 | 561 | async fn run_postgres_driver_tests_local() -> Result<(), Box<dyn std::error::Error + 'static>> { |
565 | | - let db_url = match std::env::var("TORRUST_TRACKER_CORE_POSTGRES_DATABASE_URL") { |
566 | | - Ok(url) => url, |
567 | | - Err(_) => { |
568 | | - println!("Skipping the local PostgreSQL driver tests."); |
569 | | - return Ok(()); |
570 | | - } |
| 562 | + let Ok(db_url) = std::env::var("TORRUST_TRACKER_CORE_POSTGRES_DATABASE_URL") else { |
| 563 | + println!("Skipping the local PostgreSQL driver tests."); |
| 564 | + return Ok(()); |
571 | 565 | }; |
572 | 566 |
|
573 | 567 | let mut config = Core::default(); |
|
0 commit comments