Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
This commit adds documentation, a changelog entry and a test to the new
feature
  • Loading branch information
weiznich committed Dec 5, 2024
1 parent 932c73e commit 986ede8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Increasing the minimal supported Rust version will always be coupled at least wi
* Added `limit()` and `offset()` DSL to combination clauses such as `UNION`
* Fixed `#[derive(Identifiable)]` ignoring attribute `#[diesel(serialize_as)]` on primary keys
* Added embedded struct support for `AsChangeset` via `#[diesel(embed)]`
* Added a `#[diesel(skip_update)]` attribute for the `AsChangeset` derive to skip updating a field present in the struct
* Support for libsqlite3-sys 0.30.0
* Add support for built-in PostgreSQL range operators and functions
* Support for postgres multirange type
Expand Down
2 changes: 2 additions & 0 deletions diesel_derives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ mod valid_grouping;
/// the actual field type.
/// * `#[diesel(treat_none_as_null = true/false)]`, overrides the container-level
/// `treat_none_as_null` attribute for the current field.
/// * `#[diesel(skip_update)]`, skips updating this field. Useful for working with
/// generated columns.
#[cfg_attr(
all(not(feature = "without-deprecated"), feature = "with-deprecated"),
proc_macro_derive(
Expand Down
37 changes: 37 additions & 0 deletions diesel_derives/tests/as_changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,3 +891,40 @@ fn embedded_struct() {
let actual = users::table.order(users::id).load(connection);
assert_eq!(Ok(expected), actual);
}

#[test]
fn skip_update() {
let connection = &mut connection_with_sean_and_tess_in_users_table();

#[derive(AsChangeset)]
#[diesel(table_name = users)]
#[diesel(primary_key(name))]
#[allow(dead_code)]
struct UserForm<'a> {
#[diesel(skip_update)]
name: &'a str,
hair_color: &'a str,
r#type: &'a str,
#[diesel(skip_update)]
non_existing: i32,
}

diesel::update(users::table.find(1))
.set(UserForm {
name: "John",
hair_color: "green",
r#type: "admin",
non_existing: 42,
})
.execute(connection)
.unwrap();

let res = users::table
.find(1)
.first::<(i32, String, Option<String>, Option<String>)>(connection)
.unwrap();

assert_eq!(res.1, "Sean");
assert_eq!(res.2, Some("green".into()));
assert_eq!(res.3, Some("admin".into()));
}

0 comments on commit 986ede8

Please sign in to comment.