Skip to content

Commit

Permalink
remove QueryId derive
Browse files Browse the repository at this point in the history
  • Loading branch information
adwhit committed May 31, 2023
1 parent ee95e28 commit 904365b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 23 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ 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.1.0`, is tested against `diesel 2.1.0` and `rustc 1.65`.
For earlier versions of `diesel`, check out the 1.X releases of this crate.
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.0` -> `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`.
Due to changes in upstream `diesel_cli`, you may need to modify your existing `diesel.toml` file.
As of version `2.1.0`, it **must** contain the following line:
```
custom_type_derives = ["diesel::query_builder::QueryId"]
```
So if it doesn't - add it!

## Setup with Diesel CLI

Expand All @@ -48,7 +51,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 +122,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 +160,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 +197,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
12 changes: 1 addition & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,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 @@ -317,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
15 changes: 15 additions & 0 deletions tests_with_diesel_cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
The purpose of these tests are to make sure the this crate integrates nicely with `diesel_cli`. First we run with default configuration (created by `diesel setup`), then with a custom config `custom.diesel.toml` which instructs `diesel_cli` not to generate the mapping types.

To run these tests locally:

* Make sure you have `libpq` installed
- You may have to mess around with your paths etc, see [stackoverflow](https://stackoverflow.com/questions/44654216/correct-way-to-install-psql-without-full-postgres-on-macos)
* Also install `docker`/`docker-compose` and start daemon
* Install the latest `diesel_cl`
- `cargo install diesel_cli -f --features postgres --no-default-features`

Then:

```bash
./run.sh
```
4 changes: 4 additions & 0 deletions tests_with_diesel_cli/custom.diesel.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[print_schema]
file = "src/custom_schema.rs"

# We stop diesel from generating the enum mapping type
# Instead we will do it through DbEnum
generate_missing_sql_type_definitions = false

import_types = ["diesel::sql_types::*", "crate::with_custom_schema::export::MyEnum"]

[migrations_directory]
Expand Down
2 changes: 0 additions & 2 deletions tests_with_diesel_cli/diesel.toml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
4 changes: 3 additions & 1 deletion tests_with_diesel_cli/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ docker-compose down
docker-compose up -d
sleep 2

rm -f src/schema.rs diesel.toml
diesel setup
diesel migration run
cargo test
diesel migration revert
rm src/schema.rs
rm src/schema.rs diesel.toml

# create a custom schema
diesel migration run --config-file custom.diesel.toml
Expand Down
1 change: 1 addition & 0 deletions tests_with_diesel_cli/src/with_custom_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use diesel::result::Error;
use crate::custom_schema::simple;

#[derive(diesel_derive_enum::DbEnum, Debug, Copy, Clone, PartialEq, Eq)]
// NOTE: no ExistingTypePath, so we generate the mapping type ourselves
pub enum MyEnum {
Foo,
Bar,
Expand Down

0 comments on commit 904365b

Please sign in to comment.