From 988920e65cb7b7b4ea2c7a3ecce93178d5623cd3 Mon Sep 17 00:00:00 2001 From: Norberto Lopes Date: Thu, 28 Dec 2023 18:36:50 +0000 Subject: [PATCH] wip --- examples/sqlite-sqlx-in-memory/Cargo.toml | 8 ++++++++ examples/sqlite-sqlx-in-memory/README.md | 6 ++++++ .../migrations/20230827152633_add_users.sql | 5 +++++ examples/sqlite-sqlx-in-memory/src/main.rs | 4 ++++ examples/sqlite-sqlx-in-memory/test.db | Bin 0 -> 8192 bytes src/lib.rs | 6 +++++- src/macros.rs | 11 +++++++++++ src/sqlite/diesel.rs | 2 +- src/sqlite/mod.rs | 8 ++++---- src/sqlite/sqlx.rs | 4 +++- 10 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 examples/sqlite-sqlx-in-memory/Cargo.toml create mode 100644 examples/sqlite-sqlx-in-memory/README.md create mode 100644 examples/sqlite-sqlx-in-memory/migrations/20230827152633_add_users.sql create mode 100644 examples/sqlite-sqlx-in-memory/src/main.rs create mode 100644 examples/sqlite-sqlx-in-memory/test.db diff --git a/examples/sqlite-sqlx-in-memory/Cargo.toml b/examples/sqlite-sqlx-in-memory/Cargo.toml new file mode 100644 index 0000000..974b7a2 --- /dev/null +++ b/examples/sqlite-sqlx-in-memory/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "sqlite-sqlx-in-memory" +version = "0.1.0" +edition = "2021" +authors = ["Norberto Lopes "] + +[dependencies] +database-schema = { path = "../../", features = ["sqlx", "sqlite", "runtime-async-std", "macros"] } diff --git a/examples/sqlite-sqlx-in-memory/README.md b/examples/sqlite-sqlx-in-memory/README.md new file mode 100644 index 0000000..d84b7e4 --- /dev/null +++ b/examples/sqlite-sqlx-in-memory/README.md @@ -0,0 +1,6 @@ +# sqlite-sqlx-in-memory + +This simple example will use `sqlx` to run the migrations found in +[`./migrations`](./migrations) and then generate an in-memory database schema (as a +string). + diff --git a/examples/sqlite-sqlx-in-memory/migrations/20230827152633_add_users.sql b/examples/sqlite-sqlx-in-memory/migrations/20230827152633_add_users.sql new file mode 100644 index 0000000..c4e2689 --- /dev/null +++ b/examples/sqlite-sqlx-in-memory/migrations/20230827152633_add_users.sql @@ -0,0 +1,5 @@ +CREATE TABLE users ( + id TEXT PRIMARY KEY NOT NULL, + email TEXT NOT NULL, + created_at TEXT NOT NULL DEFAULT(datetime('now', 'utc')) +); diff --git a/examples/sqlite-sqlx-in-memory/src/main.rs b/examples/sqlite-sqlx-in-memory/src/main.rs new file mode 100644 index 0000000..5daa6c8 --- /dev/null +++ b/examples/sqlite-sqlx-in-memory/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + let s = database_schema::sqlite::fetch_structure("file:./test.db?mode=memory").unwrap(); + dbg!(s); +} diff --git a/examples/sqlite-sqlx-in-memory/test.db b/examples/sqlite-sqlx-in-memory/test.db new file mode 100644 index 0000000000000000000000000000000000000000..94798e4a928cc51b1ef0c7e8d5af77564b390655 GIT binary patch literal 8192 zcmeI#u?oU45XSMlB2G%&oP^ud#l;8ED&4xM-6dj$Kt-s1OJBzaFdYir9Ngspkt4Zq z5cq959rDnWc6-!!-`H9vqB`1|iHOEZVs&ZzLD$5+?Y~83b)Jpml)9J1MG!y$0R#|0 z009ILKmY**5J2EJfw>N+&grJ{m#XwW*&nmC$gD_rd1ihQuJ$bJ;Hzc-nf7lk?%luQ Zi~s@%Aba{ literal 0 HcmV?d00001 diff --git a/src/lib.rs b/src/lib.rs index d893297..84d0a6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ compile_error!("At least one of the following features must be enabled: sqlx or #[cfg(feature = "sqlite")] #[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))] -mod sqlite; +pub mod sqlite; #[cfg(feature = "mysql")] #[cfg_attr(docsrs, doc(cfg(feature = "mysql")))] @@ -154,6 +154,10 @@ impl DatabaseSchemaBuilder { } } +#[cfg(all( + any(feature = "sqlite", feature = "postgres", feature = "mysql"), + any(feature = "sqlx", feature = "diesel") +))] impl DatabaseSchema { /// Dump the database schema. pub async fn dump(&self) -> Result<(), Error> { diff --git a/src/macros.rs b/src/macros.rs index 0bf0f3f..3991dc0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -30,6 +30,17 @@ macro_rules! generate_without_runtime_using_defaults { }; } +/// Generate an in-memory schema dump for sqlite. +#[macro_export] +macro_rules! sqlite_dump { + () => { + $crate::macros::__generate_within_runtime( + ::std::path::PathBuf::from("./migrations"), + ::std::path::PathBuf::from("./structure.sql"), + ); + }; +} + /// Generate a structure.sql file using migrations from the `migrations_path` folder. /// DO NOT USE THIS DIRECTLY. Use the `generate!` macro instead. #[doc(hidden)] diff --git a/src/sqlite/diesel.rs b/src/sqlite/diesel.rs index db8a290..5fbdf10 100644 --- a/src/sqlite/diesel.rs +++ b/src/sqlite/diesel.rs @@ -17,7 +17,7 @@ pub(crate) async fn fetch_structure_sql>( Ok(fetch_structure(&mut conn).await?) } -async fn fetch_structure( +pub(crate) async fn fetch_structure( conn: &mut diesel::SqliteConnection, ) -> Result { use diesel::{sql_query, QueryableByName, RunQueryDsl}; diff --git a/src/sqlite/mod.rs b/src/sqlite/mod.rs index b4b1bb2..e45f090 100644 --- a/src/sqlite/mod.rs +++ b/src/sqlite/mod.rs @@ -25,13 +25,13 @@ pub(crate) async fn write_structure_sql, Q: AsRef>( fetch_structure(&mut conn).await } -async fn fetch_structure(conn: &mut sqlx::sqlite::SqliteConnection) -> Result { +pub(crate) async fn fetch_structure( + conn: &mut sqlx::sqlite::SqliteConnection, +) -> Result { use sqlx::Row; let structure_dump = sqlx::query(super::SQLITE_SCHEMA_QUERY) .fetch_all(conn)