|
1 |
| -use futures::{StreamExt, TryStreamExt}; |
| 1 | +use futures::{Stream, StreamExt, TryStreamExt}; |
| 2 | + |
2 | 3 | use sqlx::postgres::types::Oid;
|
3 | 4 | use sqlx::postgres::{
|
4 | 5 | PgAdvisoryLock, PgConnectOptions, PgConnection, PgDatabaseError, PgErrorPosition, PgListener,
|
5 | 6 | PgPoolOptions, PgRow, PgSeverity, Postgres,
|
6 | 7 | };
|
7 | 8 | use sqlx::{Column, Connection, Executor, Row, Statement, TypeInfo};
|
| 9 | +use sqlx_core::bytes::Bytes; |
8 | 10 | use sqlx_test::{new, pool, setup_if_needed};
|
9 | 11 | use std::env;
|
| 12 | +use std::pin::Pin; |
10 | 13 | use std::sync::Arc;
|
11 | 14 | use std::time::Duration;
|
12 | 15 |
|
@@ -382,6 +385,67 @@ async fn it_can_query_all_scalar() -> anyhow::Result<()> {
|
382 | 385 | Ok(())
|
383 | 386 | }
|
384 | 387 |
|
| 388 | +#[sqlx_macros::test] |
| 389 | +async fn copy_can_work_with_failed_transactions() -> anyhow::Result<()> { |
| 390 | + let mut conn = new::<Postgres>().await?; |
| 391 | + |
| 392 | + // We're using a (local) statement_timeout to simulate a runtime failure, as opposed to |
| 393 | + // a parse/plan failure. |
| 394 | + let mut tx = conn.begin().await?; |
| 395 | + let _ = sqlx::query("SELECT pg_catalog.set_config($1, $2, true)") |
| 396 | + .bind("statement_timeout") |
| 397 | + .bind("1ms") |
| 398 | + .execute(tx.as_mut()) |
| 399 | + .await?; |
| 400 | + |
| 401 | + let mut copy_out: Pin< |
| 402 | + Box<dyn Stream<Item = Result<Bytes, sqlx::Error>> + Send>, |
| 403 | + > = (&mut tx) |
| 404 | + .copy_out_raw("COPY (SELECT nspname FROM pg_catalog.pg_namespace WHERE pg_sleep(0.001) IS NULL) TO STDOUT") |
| 405 | + .await?; |
| 406 | + |
| 407 | + while copy_out.try_next().await.is_ok() {} |
| 408 | + drop(copy_out); |
| 409 | + |
| 410 | + tx.rollback().await?; |
| 411 | + |
| 412 | + // conn should be usable again, as we explictly rolled back the transaction |
| 413 | + let got: i32 = sqlx::query_scalar("SELECT 1") |
| 414 | + .fetch_one(conn.as_mut()) |
| 415 | + .await?; |
| 416 | + assert_eq!(1, got); |
| 417 | + |
| 418 | + Ok(()) |
| 419 | +} |
| 420 | + |
| 421 | +#[sqlx_macros::test] |
| 422 | +async fn it_can_work_with_failed_transactions() -> anyhow::Result<()> { |
| 423 | + let mut conn = new::<Postgres>().await?; |
| 424 | + |
| 425 | + // We're using a (local) statement_timeout to simulate a runtime failure, as opposed to |
| 426 | + // a parse/plan failure. |
| 427 | + let mut tx = conn.begin().await?; |
| 428 | + let _ = sqlx::query("SELECT pg_catalog.set_config($1, $2, true)") |
| 429 | + .bind("statement_timeout") |
| 430 | + .bind("1ms") |
| 431 | + .execute(tx.as_mut()) |
| 432 | + .await?; |
| 433 | + |
| 434 | + assert!(sqlx::query("SELECT 1 WHERE pg_sleep(0.30) IS NULL") |
| 435 | + .fetch_one(tx.as_mut()) |
| 436 | + .await |
| 437 | + .is_err()); |
| 438 | + tx.rollback().await?; |
| 439 | + |
| 440 | + // conn should be usable again, as we explictly rolled back the transaction |
| 441 | + let got: i32 = sqlx::query_scalar("SELECT 1") |
| 442 | + .fetch_one(conn.as_mut()) |
| 443 | + .await?; |
| 444 | + assert_eq!(1, got); |
| 445 | + |
| 446 | + Ok(()) |
| 447 | +} |
| 448 | + |
385 | 449 | #[sqlx_macros::test]
|
386 | 450 | async fn it_can_work_with_transactions() -> anyhow::Result<()> {
|
387 | 451 | let mut conn = new::<Postgres>().await?;
|
|
0 commit comments