Skip to content

Commit

Permalink
fix: ensures recover from fail with PgCopyIn
Browse files Browse the repository at this point in the history
  • Loading branch information
andyquinterom committed Oct 29, 2022
1 parent 76ae286 commit a406d3b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions sqlx-core/src/postgres/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ impl<C: DerefMut<Target = PgConnection>> PgCopyIn<C> {
conn.wait_until_ready().await?;
conn.stream.send(Query(statement)).await?;

let response: CopyResponse = conn
.stream
.recv_expect(MessageFormat::CopyInResponse)
.await?;
let response = match conn.stream.recv_expect(MessageFormat::CopyInResponse).await {
Ok(res) => res,
Err(e) => {
conn.stream.recv().await?;
return Err(e);
}
};

Ok(PgCopyIn {
conn: Some(conn),
Expand Down Expand Up @@ -278,14 +281,15 @@ impl<C: DerefMut<Target = PgConnection>> PgCopyIn<C> {
.expect("CopyWriter::finish: conn taken illegally");

conn.stream.send(CopyDone).await?;
let cc: CommandComplete = conn
.stream
.recv_expect(MessageFormat::CommandComplete)
.await?;
let cc: CommandComplete = match conn.stream.recv_expect(MessageFormat::CommandComplete).await {
Ok(cc) => cc,
Err(e) => {
conn.stream.recv().await?;
return Err(e);
}
};

conn.stream
.recv_expect(MessageFormat::ReadyForQuery)
.await?;
conn.stream.recv_expect(MessageFormat::ReadyForQuery).await?;

Ok(cc.rows_affected())
}
Expand Down

0 comments on commit a406d3b

Please sign in to comment.