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 Feb 5, 2023
1 parent ff0b0fc commit 724b60d
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions sqlx-postgres/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,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 @@ -299,14 +302,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 724b60d

Please sign in to comment.