Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nlopes committed Dec 28, 2023
1 parent f0c50be commit 988920e
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 7 deletions.
8 changes: 8 additions & 0 deletions examples/sqlite-sqlx-in-memory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "sqlite-sqlx-in-memory"
version = "0.1.0"
edition = "2021"
authors = ["Norberto Lopes <[email protected]>"]

[dependencies]
database-schema = { path = "../../", features = ["sqlx", "sqlite", "runtime-async-std", "macros"] }
6 changes: 6 additions & 0 deletions examples/sqlite-sqlx-in-memory/README.md
Original file line number Diff line number Diff line change
@@ -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).

Original file line number Diff line number Diff line change
@@ -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'))
);
4 changes: 4 additions & 0 deletions examples/sqlite-sqlx-in-memory/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let s = database_schema::sqlite::fetch_structure("file:./test.db?mode=memory").unwrap();
dbg!(s);
}
Binary file added examples/sqlite-sqlx-in-memory/test.db
Binary file not shown.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down Expand Up @@ -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> {
Expand Down
11 changes: 11 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/sqlite/diesel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) async fn fetch_structure_sql<P: AsRef<std::path::Path>>(
Ok(fetch_structure(&mut conn).await?)
}

async fn fetch_structure(
pub(crate) async fn fetch_structure(
conn: &mut diesel::SqliteConnection,
) -> Result<String, diesel::result::Error> {
use diesel::{sql_query, QueryableByName, RunQueryDsl};
Expand Down
8 changes: 4 additions & 4 deletions src/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ pub(crate) async fn write_structure_sql<P: AsRef<std::path::Path>, Q: AsRef<std:
#[cfg(feature = "sqlx")]
mod sqlx;
#[cfg(feature = "sqlx")]
use crate::sqlite::sqlx::fetch_structure_sql;
#[cfg(feature = "sqlx")]
pub(crate) use crate::sqlite::sqlx::DEFAULT_CONNECTION_URL;
#[cfg(feature = "sqlx")]
pub(crate) use crate::sqlite::sqlx::{fetch_structure, fetch_structure_sql};

#[cfg(feature = "diesel")]
mod diesel;
#[cfg(feature = "diesel")]
use diesel::fetch_structure_sql;
#[cfg(feature = "diesel")]
pub(crate) use diesel::DEFAULT_CONNECTION_URL;
#[cfg(feature = "diesel")]
pub(crate) use diesel::{fetch_structure, fetch_structure_sql};
4 changes: 3 additions & 1 deletion src/sqlite/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pub(crate) async fn fetch_structure_sql<P: AsRef<std::path::Path>>(
fetch_structure(&mut conn).await
}

async fn fetch_structure(conn: &mut sqlx::sqlite::SqliteConnection) -> Result<String, sqlx::Error> {
pub(crate) async fn fetch_structure(
conn: &mut sqlx::sqlite::SqliteConnection,
) -> Result<String, sqlx::Error> {
use sqlx::Row;
let structure_dump = sqlx::query(super::SQLITE_SCHEMA_QUERY)
.fetch_all(conn)
Expand Down

0 comments on commit 988920e

Please sign in to comment.