breaking: fix name collision in FromRow, return Error::ColumnDecode for TryFrom errors (#3356)

* chore: create regression test for #3344

* fix(derives): use a parameter name that's less likely to collide

* breaking(derives): emit `Error::ColumnDecode` when a `TryFrom` conversion fails in `FromRow`

Breaking because `#[sqlx(default)]` on an individual field or the struct itself would have previously suppressed the error. This doesn't seem like good behavior as it could result in some potentially very difficult bugs.

Instead of using `TryFrom` for these fields, just implement `From` and apply the default explicitly.

* fix: run `cargo fmt`

* fix: use correct field in `ColumnDecode`
This commit is contained in:
Austin Bonander
2024-07-19 23:03:47 -07:00
committed by GitHub
parent b37b34bd04
commit 4fc5b30d65
4 changed files with 181 additions and 22 deletions

View File

@@ -769,3 +769,26 @@ async fn test_enum_with_schema() -> anyhow::Result<()> {
Ok(())
}
#[cfg(feature = "macros")]
#[sqlx_macros::test]
async fn test_from_row_hygiene() -> anyhow::Result<()> {
// A field named `row` previously would shadow the `row` parameter of `FromRow::from_row()`:
// https://github.com/launchbadge/sqlx/issues/3344
#[derive(Debug, sqlx::FromRow)]
pub struct Foo {
pub row: i32,
pub bar: i32,
}
let mut conn = new::<Postgres>().await?;
let foo: Foo = sqlx::query_as("SELECT 1234 as row, 5678 as bar")
.fetch_one(&mut conn)
.await?;
assert_eq!(foo.row, 1234);
assert_eq!(foo.bar, 5678);
Ok(())
}