Skip to content

Commit

Permalink
Merge pull request #92 from adwhit/v2.1
Browse files Browse the repository at this point in the history
v2.1
  • Loading branch information
adwhit authored May 31, 2023
2 parents 33d0052 + 3f63346 commit b90b235
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 211 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
matrix:
rust:
- stable
- 1.57
- 1.65

services:
postgres:
Expand Down Expand Up @@ -79,10 +79,11 @@ jobs:
cd tests_with_diesel_cli
# with default schema
diesel setup
diesel migration run
cargo test
rm -rf src/schema.rs
diesel migration revert
rm src/schema.rs diesel.toml
# create a custom schema
diesel migration run --config-file custom.diesel.toml
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "diesel-derive-enum"
version = "2.0.1"
version = "2.1.0"
description = "Derive diesel boilerplate for using enums in databases"
authors = ["Alex Whitney <[email protected]>"]
repository = "http://github.com/adwhit/diesel-derive-enum"
Expand All @@ -12,7 +12,7 @@ edition = "2021"

[dependencies]
quote = "1"
syn = "1"
syn = "2"
heck = "0.4.0"
proc-macro2 = "1"

Expand Down
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ Unfortunately, it won't work out of the box, because any type which
we wish to use with Diesel must implement various traits.
Tedious to do by hand, easy to do with a `derive` macro - enter `diesel-derive-enum`.

The latest release, `2.0.1`, is tested against `diesel 2.0.2` and `rustc 1.57`.
For earlier versions of `diesel`, check out the 1.X releases of this crate.
The latest release, `2.1.0`, is tested against `diesel 2.1.0` and `rustc 1.65` (we try to keep in lock-step with `diesel`).
For earlier versions of `diesel`, check out the `2.0.1` and `1.*` releases of this crate.

## Upgrading from 2.0.0-rc.0
## Upgrading from `2.0.x` -> `2.1.0`

There is a single breaking change. If you are using `postgres` and `diesel-cli`, you _must_
now add an `ExistingTypePath` annotation to your enum (see below). This annotation is renamed
from the (previously optional) `DieselTypePath`.
Using `diesel-cli`? Due to an upstream change, you may need to modify your existing `diesel.toml` file.
As of version `2.1.0`, it **must** contain the following lines:
```
[print-schema]
# ... other config ...
custom_type_derives = ["diesel::query_builder::QueryId"]
```
So if it doesn't - add it!

## Setup with Diesel CLI

Expand All @@ -48,7 +53,7 @@ or if not using Diesel CLI, see the next section.
Cargo.toml:
```toml
[dependencies]
diesel-derive-enum = { version = "2.0.1", features = ["postgres"] }
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
```

Suppose our project has the following `diesel.toml`:
Expand Down Expand Up @@ -119,7 +124,7 @@ your schema, the setup is a little different.
Cargo.toml:
```toml
[dependencies]
diesel-derive-enum = { version = "2.0.1", features = ["postgres"] }
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
```

SQL:
Expand Down Expand Up @@ -157,7 +162,7 @@ table! {
Cargo.toml:
```toml
[dependencies]
diesel-derive-enum = { version = "2.0.1", features = ["mysql"] }
diesel-derive-enum = { version = "2.1.0", features = ["mysql"] }
```

SQL:
Expand Down Expand Up @@ -194,7 +199,7 @@ table! {
Cargo.toml:
```toml
[dependencies]
diesel-derive-enum = { version = "2.0.1", features = ["sqlite"] }
diesel-derive-enum = { version = "2.1.0", features = ["sqlite"] }
```

SQL:
Expand Down
24 changes: 9 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ pub fn derive(input: TokenStream) -> TokenStream {

fn val_from_attrs(attrs: &[Attribute], attrname: &str) -> Option<String> {
for attr in attrs {
if attr.path.is_ident(attrname) {
match attr.parse_meta().ok()? {
if attr.path().is_ident(attrname) {
match &attr.meta {
Meta::NameValue(MetaNameValue {
lit: Lit::Str(lit_str),
value:
Expr::Lit(ExprLit {
lit: Lit::Str(lit_str),
..
}),
..
}) => return Some(lit_str.value()),
_ => panic!(
Expand Down Expand Up @@ -232,7 +236,7 @@ fn generate_derive_enum_impls(
deserialize::{self, FromSql},
expression::AsExpression,
internal::derives::as_expression::Bound,
query_builder::{bind_collector::RawBytesBindCollector, QueryId},
query_builder::{bind_collector::RawBytesBindCollector},
row::Row,
serialize::{self, IsNull, Output, ToSql},
sql_types::*,
Expand Down Expand Up @@ -300,7 +304,7 @@ fn generate_new_diesel_mapping(
// Note - we only generate a new mapping for mysql and sqlite, postgres
// should already have one
quote! {
#[derive(SqlType, Clone)]
#[derive(Clone, SqlType, diesel::query_builder::QueryId)]
#[diesel(mysql_type(name = "Enum"))]
#[diesel(sqlite_type(name = "Text"))]
#[diesel(postgres_type(name = #pg_internal_type))]
Expand All @@ -313,16 +317,6 @@ fn generate_common_impls(
enum_ty: &Ident,
) -> proc_macro2::TokenStream {
quote! {

// NOTE: at some point this impl will no longer be necessary
// for diesel-cli schemas
// See https://github.com/adwhit/diesel-derive-enum/issues/10
// and https://github.com/adwhit/diesel-derive-enum/pull/79
impl QueryId for #diesel_mapping {
type QueryId = #diesel_mapping;
const HAS_STATIC_QUERY_ID: bool = true;
}

impl AsExpression<#diesel_mapping> for #enum_ty {
type Expression = Bound<#diesel_mapping, Self>;

Expand Down
Loading

0 comments on commit b90b235

Please sign in to comment.