From 38f6f24f368a9431e41d2e9b35ceb5678fe7f2f6 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 8 Feb 2024 14:43:40 +0100 Subject: [PATCH 1/4] docs(README): update for next version release - fixes errors that were forgotten to change since 0.0.16 - update text to be more readable --- README.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e9aa97f3..5e6aef8d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ diesel::table! { We run: ```sh -cargo dsync -i schema.rs -o models +dsync -i schema.rs -o models ``` Now we have everything we need! @@ -45,9 +45,9 @@ async fn demo(db: Connection) { } ``` -For a complete example, see [`test/simple_table/schema.rs`](test/simple_table/schema.rs) which generates all the code in [`test/simple_schema/models`](test/simple_table/models). +For a complete example, see [`test/simple_table_sqlite/schema.rs`](test/simple_table_sqlite/schema.rs) which generates all the code in [`test/simple_schema_sqlite/models`](test/simple_table_sqlite/models). -## Usage +## Usage as a library 1. Add this crate: @@ -67,7 +67,10 @@ For a complete example, see [`test/simple_table/schema.rs`](test/simple_table/sc dsync::generate_files( PathBuf::from_iter([dir, "src/schema.rs"]), PathBuf::from_iter([dir, "src/models"]), - GenerationConfig { /* ... your generation options ... */ } + GenerationConfig { + connection_type: "diesel::sqlite::SqliteConnection", + options: Default::default(), + } ); } ``` @@ -104,11 +107,11 @@ cargo install dsync **CLI Usage** -* `-i`: input argument: path to schema file -* `-o`: output argument: path to directory where generated code should be written -* `-c`: connection type (for example: `diesel::r2d2::PooledConnection>`) +* `-i`: path to the diesel schema file +* `-o`: model output directory +* `-c`: connection type (for example: `diesel::sqlite::SqliteConnection`) * `-g`: (optional, repeatable) list of columns that are automatically generated by create/update triggers (for example, `created_at`, `updated_at`) -* `--tsync`: (optional) adds `#[tsync]` attribute to generated structs (see ) +* `--tsync`: (optional) adds `#[tsync]` attribute to generated structs for the [`tsync` crate](https://github.com/Wulf/tsync) * `--model-path`: (optional) set a custom model import path, default `crate::models::` * `--schema-path`: (optional) set a custom schema import path, default `crate::schema::` * `--no-serde`: (optional) if set, does not output any serde related code @@ -119,7 +122,6 @@ cargo install dsync * `--readonly-prefix`: (optional, repeatable) A prefix to treat a table matching this as readonly *2 * `--readonly-suffix`: (optional, repeatable) A suffix to treat a table matching this as readonly *2 * `--diesel-backend`: (when the "advanced-queries" feature is enabled) The diesel backend in use (possible values include `diesel::pg::Pg`, `diesel::sqlite::Sqlite`, `diesel::mysql::Mysql`, or your custom backend type) -* note: the CLI has fail-safes to prevent accidental file overwriting ```sh dsync -i src/schema.rs -o src/models @@ -127,7 +129,8 @@ dsync -i src/schema.rs -o src/models Notes: -- *2: "readonly" tables dont have `Update*` & `Create*` structs, only `*`(no suffix / prefix) structs. +- the CLI has fail-safes to prevent accidental file overwriting +- *2: "readonly" tables dont have `Update*`(`UpdateTodos`) & `Create*`(`CreateTodos`) structs, only `*`(`Todos`, no suffix / prefix) structs. For example this is useful for Sqlite views, which are read-only (cannot be written to, but can be read) ## Experimental API @@ -140,7 +143,9 @@ Feel free to open an issue to discuss these API and provide your feeedback. ## Docs -See `dsync --help` for more information. +See `dsync --help` for all CLI arguments and documentation. + +See [docs.rs](https://docs.rs/dsync/latest/dsync/) for library documentation. Feel free to open tickets for support or feature requests. From df13980aa24dc62be37ee05a605a2434ec4688eb Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 8 Feb 2024 14:45:53 +0100 Subject: [PATCH 2/4] docs(CHANGELOG): add notes for #114 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1d2539..5e97de18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - add new experimental filters (behind the `advanced-queries` feature flag) - move function `paginate` behind `advanced-queries` feature flag - split `GenerationConfig` into required and optional parts (`GenerationConfigOpts`) (fixes #92) +- added compile tests to actually verify all options compiling ## 0.0.17 (yanked) From 3795b528e9c5f52fc87f4b26001353da8a25b3b6 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 8 Feb 2024 14:49:14 +0100 Subject: [PATCH 3/4] style(lib): add features added since 0.0.16 --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 920dce00..d2f50bbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,8 +7,10 @@ //! - `async`: enable support for [diesel_async](https://github.com/weiznich/diesel_async) //! - `tsync`: enable support for [tsync](https://github.com/Wulf/tsync) //! - `backtrace`: enable attaching backtraces to dsync errors +//! - `derive-queryablebyname`: enable `diesel::QueryableByName` derives on READ structs +//! - `advanced-queries`: enable experimental pagination and filter functions ([examples](https://github.com/Wulf/dsync/tree/a44afdd08f4447e367aa47ecb91fae88b57f8944/test/advanced_queries)) //! -//! default features: `tsync`, `backtrace` +//! default features: `tsync`, `backtrace`, `derive-queryablebyname` mod code; pub mod error; From 6b451c2d54c820bd03c21503b2a23ee9cfc7164f Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 8 Feb 2024 15:02:14 +0100 Subject: [PATCH 4/4] fix: throw error if "connection_type" is a empty string similar to "diesel_backend" --- src/global.rs | 36 +++++++++++++++++++++++++++--------- src/lib.rs | 1 - 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/global.rs b/src/global.rs index 6b8b31e7..9c951bac 100644 --- a/src/global.rs +++ b/src/global.rs @@ -482,21 +482,39 @@ impl<'a> GenerationConfig<'a> { } } -#[cfg(feature = "advanced-queries")] pub fn validate_config(config: &GenerationConfig) -> crate::Result<()> { use crate::error::{Error, ErrorEnum}; - const VALID_BACKENDS: [&str; 3] = [ - "diesel::pg::Pg", - "diesel::sqlite::Sqlite", - "diesel::mysql::Mysql", + // NOTE: the following arrays should likely be joined at compile-time instead of at runtime, but rust does not provide such a thing in std + + #[cfg(feature = "advanced-queries")] + { + const VALID_BACKENDS: [&str; 3] = [ + "diesel::pg::Pg", + "diesel::sqlite::Sqlite", + "diesel::mysql::Mysql", + ]; + + if config.diesel_backend.is_empty() { + return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!( + "Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).", + &config.diesel_backend, + VALID_BACKENDS.join(", ") + )))); + } + } + + const KNOWN_CONNECTIONS: [&str; 4] = [ + "diesel::pg::PgConnection", + "diesel::sqlite::SqliteConnection", + "diesel::mysql::MysqlConnection", + "diesel::r2d2::PooledConnection>", ]; - if config.diesel_backend.is_empty() { + if config.connection_type.is_empty() { return Err(Error::new(ErrorEnum::InvalidGenerationConfig(format!( - "Invalid diesel_backend '{}', please use one of the following: {:?}; or, a custom diesel backend type (a struct which implements `diesel::backend::Backend`).", - &config.diesel_backend, - VALID_BACKENDS.join(", ") + "option \"connection_type\" cannot be empty. Known Connections types are:\n[{}]", + KNOWN_CONNECTIONS.join(", ") )))); } diff --git a/src/lib.rs b/src/lib.rs index d2f50bbb..9274e2f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,7 +112,6 @@ pub fn generate_files( output_models_dir: &Path, config: GenerationConfig, ) -> Result> { - #[cfg(feature = "advanced-queries")] global::validate_config(&config)?; let generated = generate_code(