@@ -2062,3 +2062,58 @@ async fn test_pg_copy_chunked() -> anyhow::Result<()> {
2062
2062
assert ! ( copy. finish( ) . await . is_ok( ) ) ;
2063
2063
Ok ( ( ) )
2064
2064
}
2065
+
2066
+ async fn test_copy_in_error_case ( query : & str , expected_error : & str ) -> anyhow:: Result < ( ) > {
2067
+ let mut conn = new :: < Postgres > ( ) . await ?;
2068
+ conn. execute ( "CREATE TEMPORARY TABLE IF NOT EXISTS invalid_copy_target (id int4)" )
2069
+ . await ?;
2070
+ // Try the COPY operation
2071
+ match conn. copy_in_raw ( query) . await {
2072
+ Ok ( _) => anyhow:: bail!( "expected error" ) ,
2073
+ Err ( e) => assert ! (
2074
+ e. to_string( ) . contains( expected_error) ,
2075
+ "expected error to contain: {expected_error}, got: {e:?}"
2076
+ ) ,
2077
+ }
2078
+ // Verify connection is still usable
2079
+ let value = sqlx:: query ( "select 1 + 1" )
2080
+ . try_map ( |row : PgRow | row. try_get :: < i32 , _ > ( 0 ) )
2081
+ . fetch_one ( & mut conn)
2082
+ . await ?;
2083
+ assert_eq ! ( 2i32 , value) ;
2084
+ Ok ( ( ) )
2085
+ }
2086
+ #[ sqlx_macros:: test]
2087
+ async fn it_can_recover_from_copy_in_to_missing_table ( ) -> anyhow:: Result < ( ) > {
2088
+ test_copy_in_error_case (
2089
+ r#"
2090
+ COPY nonexistent_table (id) FROM STDIN WITH (FORMAT CSV, HEADER);
2091
+ "# ,
2092
+ "does not exist" ,
2093
+ )
2094
+ . await
2095
+ }
2096
+ #[ sqlx_macros:: test]
2097
+ async fn it_can_recover_from_copy_in_empty_query ( ) -> anyhow:: Result < ( ) > {
2098
+ test_copy_in_error_case ( "" , "EmptyQuery" ) . await
2099
+ }
2100
+ #[ sqlx_macros:: test]
2101
+ async fn it_can_recover_from_copy_in_syntax_error ( ) -> anyhow:: Result < ( ) > {
2102
+ test_copy_in_error_case (
2103
+ r#"
2104
+ COPY FROM STDIN WITH (FORMAT CSV);
2105
+ "# ,
2106
+ "syntax error" ,
2107
+ )
2108
+ . await
2109
+ }
2110
+ #[ sqlx_macros:: test]
2111
+ async fn it_can_recover_from_copy_in_invalid_params ( ) -> anyhow:: Result < ( ) > {
2112
+ test_copy_in_error_case (
2113
+ r#"
2114
+ COPY invalid_copy_target FROM STDIN WITH (FORMAT CSV, INVALID_PARAM true);
2115
+ "# ,
2116
+ "invalid_param" ,
2117
+ )
2118
+ . await
2119
+ }
0 commit comments