Skip to content

Commit

Permalink
Merge pull request #2626 from pksunkara/derive
Browse files Browse the repository at this point in the history
Improve derive syntax
  • Loading branch information
weiznich authored Dec 10, 2021
2 parents c2a5316 + 8e4e7c4 commit 6aadc7f
Show file tree
Hide file tree
Showing 255 changed files with 4,824 additions and 2,788 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: version

- name: Test diesel (nightly)
if: matrix.rust == 'nightly'
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -302,7 +303,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2021-04-01
toolchain: 1.51.0
profile: minimal
override: true
- name: Cache cargo registry
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/

* Added support for the `FROM ONLY <table>` clause in Postgresql

* Added support for all the derive attributes being inside `#[diesel(...)]`

### Removed

* All previously deprecated items have been removed.
Expand Down Expand Up @@ -243,8 +245,20 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
* `diesel::dsl::any` and `diesel::dsl::all` are now deprecated in
favour of `ExpressionMethods::eq_any()` and `ExpressionMethods::ne_all()`

* All the diesel derive attributes that are not inside `#[diesel(...)]`

### Upgrade Notes

### Derive attributes
<a name="2-0-0-derive-attributes"></a>

We have updated all of our diesel derive attributes to follow the patterns that are used
widely in the rust ecosystem. This means that all of them need to be wrapped by `#[diesel()]` now. And you can specify multiple attributes on the same line now separated by `,`.

This is backward compatible and thus all of your old attributes will still work, but with
warnings. The attributes can be upgraded by either looking at the warnings or by reading
diesel derive documentation reference.

#### Replacement of `NonAggregate` with `ValidGrouping`
<a name="2-0-0-upgrade-non-aggregate"></a>

Expand Down
2 changes: 1 addition & 1 deletion diesel/src/associations/belongs_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait BelongsTo<Parent> {
/// #
/// # #[derive(Debug, PartialEq)]
/// # #[derive(Identifiable, Queryable, Associations)]
/// # #[belongs_to(User)]
/// # #[diesel(belongs_to(User))]
/// # pub struct Post {
/// # id: i32,
/// # user_id: i32,
Expand Down
34 changes: 17 additions & 17 deletions diesel/src/associations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
//! Traits related to relationships between multiple tables.
//!
//! Associations in Diesel are always child-to-parent.
//! You can declare an association between two records with `#[belongs_to]`.
//! You can declare an association between two records with `#[diesel(belongs_to)]`.
//! Unlike other ORMs, Diesel has no concept of `has many`
//!
//! ```rust
//! # include!("../doctest_setup.rs");
//! use schema::{posts, users};
//!
//! #[derive(Identifiable, Queryable, PartialEq, Debug)]
//! #[table_name = "users"]
//! #[diesel(table_name = users)]
//! pub struct User {
//! id: i32,
//! name: String,
//! }
//!
//! #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
//! #[belongs_to(User)]
//! #[table_name = "posts"]
//! #[diesel(belongs_to(User))]
//! #[diesel(table_name = posts)]
//! pub struct Post {
//! id: i32,
//! user_id: i32,
Expand All @@ -40,12 +40,12 @@
//! # }
//! ```
//!
//! Note that in addition to the `#[belongs_to]` annotation, we also need to
//! Note that in addition to the `#[diesel(belongs_to)]` annotation, we also need to
//! `#[derive(Associations)]`
//!
//! `#[belongs_to]` is given the name of the struct that represents the parent.
//! `#[diesel(belongs_to)]` is given the name of the struct that represents the parent.
//! Both the parent and child must implement [`Identifiable`].
//! The struct given to `#[belongs_to]` must be in scope,
//! The struct given to `#[diesel(belongs_to)]` must be in scope,
//! so you will need `use some_module::User` if `User` is defined in another module.
//!
//! If the parent record is generic over lifetimes, they can be written as `'_`.
Expand All @@ -58,15 +58,15 @@
//! # use std::borrow::Cow;
//! #
//! #[derive(Identifiable)]
//! #[table_name = "users"]
//! #[diesel(table_name = users)]
//! pub struct User<'a> {
//! id: i32,
//! name: Cow<'a, str>,
//! }
//!
//! #[derive(Associations)]
//! #[belongs_to(parent = "User<'_>")]
//! #[table_name = "posts"]
//! #[diesel(belongs_to(User<'_>))]
//! #[diesel(table_name = posts)]
//! pub struct Post {
//! id: i32,
//! user_id: i32,
Expand All @@ -79,8 +79,8 @@
//!
//! By default, Diesel assumes that your foreign keys will follow the convention `table_name_id`.
//! If your foreign key has a different name,
//! you can provide the `foreign_key` argument to `#[belongs_to]`.
//! For example, `#[belongs_to(Foo, foreign_key = "mykey")]`.
//! you can provide the `foreign_key` argument to `#[diesel(belongs_to)]`.
//! For example, `#[diesel(belongs_to(Foo, foreign_key = mykey))]`.
//!
//! Associated data is typically loaded in multiple queries (one query per table).
//! This is usually more efficient than using a join,
Expand All @@ -106,7 +106,7 @@
//! # }
//! #
//! # #[derive(Debug, PartialEq, Identifiable, Queryable, Associations)]
//! # #[belongs_to(User)]
//! # #[diesel(belongs_to(User))]
//! # pub struct Post {
//! # id: i32,
//! # user_id: i32,
Expand Down Expand Up @@ -160,7 +160,7 @@
//! #
//! # #[derive(Debug, PartialEq)]
//! # #[derive(Identifiable, Queryable, Associations)]
//! # #[belongs_to(User)]
//! # #[diesel(belongs_to(User))]
//! # pub struct Post {
//! # id: i32,
//! # user_id: i32,
Expand Down Expand Up @@ -214,7 +214,7 @@
//! #
//! # #[derive(Debug, PartialEq)]
//! # #[derive(Identifiable, Queryable, Associations)]
//! # #[belongs_to(User)]
//! # #[diesel(belongs_to(User))]
//! # pub struct Post {
//! # id: i32,
//! # user_id: i32,
Expand Down Expand Up @@ -274,15 +274,15 @@
//! # }
//! #
//! # #[derive(Debug, PartialEq, Identifiable, Queryable, Associations)]
//! # #[belongs_to(User)]
//! # #[diesel(belongs_to(User))]
//! # pub struct Post {
//! # id: i32,
//! # user_id: i32,
//! # title: String,
//! # }
//! #
//! # #[derive(Debug, PartialEq, Identifiable, Queryable, Associations)]
//! # #[belongs_to(Post)]
//! # #[diesel(belongs_to(Post))]
//! # pub struct Comment {
//! # id: i32,
//! # post_id: i32,
Expand Down
8 changes: 4 additions & 4 deletions diesel/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub type Result<T> = result::Result<T, Box<dyn Error + Send + Sync>>;
/// #[derive(Queryable, PartialEq, Debug)]
/// struct User {
/// id: i32,
/// #[diesel(deserialize_as = "LowercaseString")]
/// #[diesel(deserialize_as = LowercaseString)]
/// name: String,
/// }
///
Expand Down Expand Up @@ -273,7 +273,7 @@ pub use diesel_derives::Queryable;
/// # use diesel::sql_query;
/// #
/// #[derive(QueryableByName, PartialEq, Debug)]
/// #[table_name = "users"]
/// #[diesel(table_name = users)]
/// struct User {
/// id: i32,
/// name: String,
Expand Down Expand Up @@ -323,10 +323,10 @@ pub use diesel_derives::Queryable;
/// }
///
/// #[derive(QueryableByName, PartialEq, Debug)]
/// #[table_name = "users"]
/// #[diesel(table_name = users)]
/// struct User {
/// id: i32,
/// #[diesel(deserialize_as = "LowercaseString")]
/// #[diesel(deserialize_as = LowercaseString)]
/// name: String,
/// }
///
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
/// in order, therefore two different instances of this type
/// must be sortable
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)]
#[sql_type = "Text"]
#[diesel(sql_type = Text)]
pub struct MigrationVersion<'a>(Cow<'a, str>);

impl<'a> MigrationVersion<'a> {
Expand Down
8 changes: 4 additions & 4 deletions diesel/src/mysql/types/date_and_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use crate::sql_types::{Date, Datetime, Time, Timestamp};
#[repr(C)]
#[derive(Debug, Clone, Copy, AsExpression, FromSqlRow)]
#[non_exhaustive]
#[sql_type = "Timestamp"]
#[sql_type = "Time"]
#[sql_type = "Date"]
#[sql_type = "Datetime"]
#[diesel(sql_type = Timestamp)]
#[diesel(sql_type = Time)]
#[diesel(sql_type = Date)]
#[diesel(sql_type = Datetime)]
pub struct MysqlTime {
/// [Year field](https://dev.mysql.com/doc/dev/mysql-server/latest/structMYSQL__TIME.html#af585231d3ed0bc2fa389856e61e15d4e)
pub year: libc::c_uint,
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/mysql/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,5 @@ impl HasSqlType<Unsigned<BigInt>> for Mysql {
doc = " [`chrono::NaiveDateTime`]: https://docs.rs/chrono/0.4.19/chrono/naive/struct.NaiveDateTime.html"
)]
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[mysql_type = "DateTime"]
#[diesel(mysql_type(name = "DateTime"))]
pub struct Datetime;
2 changes: 1 addition & 1 deletion diesel/src/pg/serialize/write_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::serialize::{self, Output};
/// # use std::io::Write;
/// #
/// #[derive(SqlType)]
/// #[postgres(type_name = "my_type")]
/// #[diesel(postgres_type(name = "my_type"))]
/// struct MyType;
///
/// #[derive(Debug)]
Expand Down
10 changes: 5 additions & 5 deletions diesel/src/pg/types/date_and_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ mod quickcheck_impls;
mod std_time;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsExpression, FromSqlRow)]
#[sql_type = "Timestamp"]
#[sql_type = "Timestamptz"]
#[diesel(sql_type = Timestamp)]
#[diesel(sql_type = Timestamptz)]
/// Timestamps are represented in Postgres as a 64 bit signed integer representing the number of
/// microseconds since January 1st 2000. This struct is a dumb wrapper type, meant only to indicate
/// the integer's meaning.
pub struct PgTimestamp(pub i64);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsExpression, FromSqlRow)]
#[sql_type = "Date"]
#[diesel(sql_type = Date)]
/// Dates are represented in Postgres as a 32 bit signed integer representing the number of julian
/// days since January 1st 2000. This struct is a dumb wrapper type, meant only to indicate the
/// integer's meaning.
Expand All @@ -32,15 +32,15 @@ pub struct PgDate(pub i32);
/// microseconds since midnight. This struct is a dumb wrapper type, meant only to indicate the
/// integer's meaning.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsExpression, FromSqlRow)]
#[sql_type = "Time"]
#[diesel(sql_type = Time)]
pub struct PgTime(pub i64);

/// Intervals in Postgres are separated into 3 parts. A 64 bit integer representing time in
/// microseconds, a 32 bit integer representing number of days, and a 32 bit integer
/// representing number of months. This struct is a dumb wrapper type, meant only to indicate the
/// meaning of these parts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsExpression, FromSqlRow)]
#[sql_type = "Interval"]
#[diesel(sql_type = Interval)]
pub struct PgInterval {
/// The number of whole microseconds
pub microseconds: i64,
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/types/floats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::sql_types;
mod quickcheck_impls;

#[derive(Debug, Clone, PartialEq, Eq, AsExpression, FromSqlRow)]
#[sql_type = "sql_types::Numeric"]
#[diesel(sql_type = sql_types::Numeric)]
/// Represents a NUMERIC value, closely mirroring the PG wire protocol
/// representation
pub enum PgNumeric {
Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/types/mac_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod foreign_derives {

#[derive(AsExpression, FromSqlRow)]
#[diesel(foreign_derive)]
#[sql_type = "MacAddr"]
#[diesel(sql_type = MacAddr)]
struct ByteArrayProxy([u8; 6]);
}

Expand Down
18 changes: 9 additions & 9 deletions diesel/src/pg/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod sql_types {
/// [`FromSql`]: crate::deserialize::FromSql
/// [`u32`]: https://doc.rust-lang.org/nightly/std/primitive.u32.html
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "26", array_oid = "1018")]
#[diesel(postgres_type(oid = 26, array_oid = 1018))]
pub struct Oid;

/// The "timestamp with time zone" SQL type, which PostgreSQL abbreviates
Expand Down Expand Up @@ -76,7 +76,7 @@ pub mod sql_types {
doc = " [`chrono::DateTime`]: https://docs.rs/chrono/0.4.19/chrono/struct.DateTime.html"
)]
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "1184", array_oid = "1185")]
#[diesel(postgres_type(oid = 1184, array_oid = 1185))]
pub struct Timestamptz;

/// The `Array` SQL type.
Expand Down Expand Up @@ -170,7 +170,7 @@ pub mod sql_types {
///
/// [`WriteTuple`]: super::super::super::serialize::WriteTuple
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "2249", array_oid = "2287")]
#[diesel(postgres_type(oid = 2249, array_oid = 2287))]
pub struct Record<ST: 'static>(ST);

/// Alias for `SmallInt`
Expand All @@ -196,7 +196,7 @@ pub mod sql_types {
/// [`FromSql`]: crate::deserialize::FromSql
/// [Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "2950", array_oid = "2951")]
#[diesel(postgres_type(oid = 2950, array_oid = 2951))]
pub struct Uuid;

/// Alias for `Binary`, to ensure `infer_schema!` works
Expand Down Expand Up @@ -287,7 +287,7 @@ pub mod sql_types {
/// # fn main() {}
/// ```
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "3802", array_oid = "3807")]
#[diesel(postgres_type(oid = 3802, array_oid = 3807))]
pub struct Jsonb;

/// The PostgreSQL [Money](https://www.postgresql.org/docs/9.1/static/datatype-money.html) type.
Expand Down Expand Up @@ -335,7 +335,7 @@ pub mod sql_types {
/// # }
/// ```
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "790", array_oid = "791")]
#[diesel(postgres_type(oid = 790, array_oid = 791))]
pub struct Money;

/// The [`MACADDR`](https://www.postgresql.org/docs/9.6/static/datatype-net-types.html) SQL type.
Expand Down Expand Up @@ -379,7 +379,7 @@ pub mod sql_types {
/// # }
/// ```
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "829", array_oid = "1040")]
#[diesel(postgres_type(oid = 829, array_oid = 1040))]
pub struct MacAddr;

#[doc(hidden)]
Expand Down Expand Up @@ -440,7 +440,7 @@ pub mod sql_types {
/// # fn main() {}
/// ```
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "869", array_oid = "1041")]
#[diesel(postgres_type(oid = 869, array_oid = 1041))]
pub struct Inet;

/// The [`CIDR`](https://www.postgresql.org/docs/9.6/static/datatype-net-types.html) SQL type. This type can only be used with `feature = "network-address"`
Expand Down Expand Up @@ -496,7 +496,7 @@ pub mod sql_types {
/// # fn main() {}
/// ```
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[postgres(oid = "650", array_oid = "651")]
#[diesel(postgres_type(oid = 650, array_oid = 651))]
pub struct Cidr;
}

Expand Down
2 changes: 1 addition & 1 deletion diesel/src/pg/types/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::sql_types::{BigInt, Money};
/// use diesel::data_types::PgMoney as Fils; // 1/1000th unit of Dinar
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsExpression, FromSqlRow)]
#[sql_type = "Money"]
#[diesel(sql_type = Money)]
pub struct PgMoney(pub i64);

impl FromSql<Money, Pg> for PgMoney {
Expand Down
Loading

0 comments on commit 6aadc7f

Please sign in to comment.