Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Decimal in NUMERIC_TYPES #4102

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions diesel_derives/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ fn is_numeric(ty: &syn::TypePath) -> bool {
"Int8",
"Bigint",
"BigSerial",
"Decimal",
"Float",
"Float4",
"Float8",
Expand Down
2 changes: 2 additions & 0 deletions diesel_tests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ mod limit_offset;
mod macros;
#[cfg(feature = "postgres")]
mod only;
#[cfg(not(feature = "sqlite"))]
mod operations;
mod order;
mod perf_details;
#[cfg(feature = "postgres")]
Expand Down
56 changes: 56 additions & 0 deletions diesel_tests/tests/operations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::str::FromStr;

use crate::schema::*;
use bigdecimal::BigDecimal;
use diesel::{insert_into, prelude::*, update};

diesel::table! {
bigdecimal_table (id) {
id -> Integer,
big_decimal -> Decimal,
}
}

#[derive(Debug, Insertable, Queryable, Selectable)]
#[diesel(table_name = bigdecimal_table)]
struct CustomBigDecimal {
pub id: i32,
pub big_decimal: BigDecimal,
}

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

let custom_value = CustomBigDecimal {
id: 1,
big_decimal: BigDecimal::from_str("0.80").unwrap(),
};

diesel::sql_query(
"
CREATE TEMPORARY TABLE bigdecimal_table (
id SERIAL PRIMARY KEY,
big_decimal DECIMAL(8,2) NOT NULL
);
",
)
.execute(connection)
.unwrap();

insert_into(bigdecimal_table::table)
.values(&custom_value)
.execute(connection)
.unwrap();

let val = BigDecimal::from_str("0.1").unwrap();

update(bigdecimal_table::table)
.set(bigdecimal_table::big_decimal.eq(bigdecimal_table::big_decimal + val))
.execute(connection)
.unwrap();

let updated: CustomBigDecimal = bigdecimal_table::table.first(connection).unwrap();

assert_eq!(BigDecimal::from_str("0.90").unwrap(), updated.big_decimal);
}
Loading