Skip to content

Commit

Permalink
Merge branch 'r/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
emirror-de committed Feb 27, 2022
2 parents 4d9a6c4 + c40b27c commit 1d87b79
Show file tree
Hide file tree
Showing 15 changed files with 497 additions and 68 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## v0.5.0

### New features

- `diesel::Pg` connection is now implemented

### Changes

* Updated examples to be able to use feature flags for the database selection

## v0.4.1

### New features
Expand Down Expand Up @@ -70,8 +80,11 @@ None
* Added `barrel` integration for `SQLite3`

* Added `SQLite3` support, backed by `diesel`

* Added an example for `SqliteConnection`

* Added possibility for `custom` queries

* Added possibility to change the model before and after updating it in the database by implementing the `DatabaseUpdateHandler` trait.

### Changes
Expand Down
3 changes: 3 additions & 0 deletions Docker/database-pg.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_USER=naphtha
POSTGRES_PASSWORD=naphtha
POSTGRES_DB=naphtha
6 changes: 6 additions & 0 deletions Docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ services:
ports:
- 3306:3306
command: ["mysqld", "--default-authentication-plugin=mysql_native_password"]
database-pg:
image: "postgres:latest"
env_file:
- database-pg.env
ports:
- 5432:5432
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ SPDX-License-Identifier: MIT OR Apache-2.0

# naphtha

*Universal database connection layer*

[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-informational?style=flat-square)](COPYRIGHT.md)

[![Crates.io](https://img.shields.io/crates/v/naphtha.svg)](https://crates.io/crates/naphtha) [![docs.rs](https://img.shields.io/docsrs/naphtha?style=flat-square)](https://docs.rs/naphtha) **naphtha**

[![Crates.io](https://img.shields.io/crates/v/naphtha-proc-macro.svg)](https://crates.io/crates/naphtha-proc-macro) **naphtha-proc-macro**

Please checkout the [documentation page](https://docs.rs/naphtha) for information and examples (also see examples folder in [naphtha](./naphtha/examples))
Please checkout the [documentation page](https://docs.rs/naphtha) for more information (also see examples folder in [naphtha](./naphtha/examples))

If you have questions, want to contribute or have any other type of request, your invited to create an issue or visit the [openprobst.dev](https://openprobst.dev) discord server.

[![](https://img.shields.io/discord/855726181142495242?color=154683&label=discord&style=flat-square)](https://discord.gg/nx7YtsjEbT)

## About

This crate is to simplify the creation and usage of models that require a database connection. If applied correct, changing the database type is only a feature flag away.

The models can also be compiled and used without database connection support for better usage on server and client side.

## Roadmap

- [x] Connect to database using a wrapper, defining the base for interchangeable Databases
Expand All @@ -27,8 +35,13 @@ If you have questions, want to contribute or have any other type of request, you
- [x] Thread safe sharing of the database connection
- [x] Integrate `barrel` crate for writing migrations in Rust, available at runtime
- [x] Implement support for `diesel::MySqlConnection`
- [ ] Implement support for `diesel::PgConnection`
- [ ] More databases!!!
- [x] Implement support for `diesel::PgConnection`
- [ ] Connection pooling?
- [ ] More databases?

## Troubleshooting

It is very easy to get a whole bunch of `trait bound not satisfied` error messages when your model is not configured correctly. Make sure that your `schema` module and the containing `table!` definition is in line with your `model` definition and that it uses the correct types defined by [barrel](https://docs.rs/barrel).

## Contributing

Expand Down
16 changes: 9 additions & 7 deletions naphtha-proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "naphtha-proc-macro"
version = "0.4.1"
version = "0.5.0"
authors = ["Lewin Probst <[email protected]>"]
edition = "2018"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Supporting macro crate for naphtha"
homepage = "https://github.com/emirror-de/naphtha"
Expand All @@ -17,14 +17,16 @@ proc-macro = true

[features]
default = []
full = ["sqlite", "mysql", "barrel-full"]
barrel-full = ["barrel-sqlite", "barrel-mysql"]
full = ["sqlite", "mysql", "pg", "barrel-full"]
barrel-full = ["barrel-sqlite", "barrel-mysql", "barrel-pg"]
sqlite = []
mysql = []
pg = []
barrel-sqlite = []
barrel-mysql = []
barrel-pg = []

[dependencies]
syn = { version = "^1.0.74", features = ["parsing"] }
proc-macro2 = "^1.0.28"
quote = "^1.0.9"
syn = { version = "1.0.86", features = ["parsing"] }
proc-macro2 = "1.0.36"
quote = "1.0.15"
2 changes: 2 additions & 0 deletions naphtha-proc-macro/src/barrel_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(feature = "barrel-mysql")]
pub(crate) mod mysql;
#[cfg(feature = "barrel-pg")]
pub(crate) mod pg;
#[cfg(feature = "barrel-sqlite")]
pub(crate) mod sqlite;
56 changes: 56 additions & 0 deletions naphtha-proc-macro/src/barrel_impl/pg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use {quote::quote, syn::DeriveInput};

pub(crate) fn impl_pg(ast: &DeriveInput) -> ::proc_macro2::TokenStream {
let name = &ast.ident;

quote! {
impl ::naphtha::barrel::DatabaseSqlMigrationExecutor<::naphtha::diesel::PgConnection, usize> for #name
where
Self: ::naphtha::barrel::DatabaseSqlMigration,
{
fn execute_migration_up(conn: &::naphtha::DatabaseConnection<::naphtha::diesel::PgConnection>) -> Result<usize, String> {
use {
::naphtha::{barrel::Migration, DatabaseConnection, log::error, diesel::RunQueryDsl},
};
let mut m = Migration::new();
Self::migration_up(&mut m);
let m = m.make::<::naphtha::barrel::backend::Pg>();

let c = match conn.lock() {
Ok(c) => c,
Err(msg) => {
error!("Could not aquire lock on DatabaseSqlMigrationExecutor::execute_migration_up: {}", msg.to_string());
return Err(msg.to_string());
}
};

match ::naphtha::diesel::sql_query(m).execute(&*c) {
Ok(u) => Ok(u),
Err(msg) => Err(msg.to_string()),
}
}

fn execute_migration_down(conn: &::naphtha::DatabaseConnection<::naphtha::diesel::PgConnection>) -> Result<usize, String> {
use {
::naphtha::{barrel::Migration, DatabaseConnection, log::error, diesel::RunQueryDsl},
};
let mut m = Migration::new();
Self::migration_down(&mut m);
let m = m.make::<::naphtha::barrel::backend::Pg>();

let c = match conn.lock() {
Ok(c) => c,
Err(msg) => {
error!("Could not aquire lock on DatabaseSqlMigrationExecutor::execute_migration_down for model: {}", msg.to_string());
return Err(msg.to_string());
}
};

match ::naphtha::diesel::sql_query(m).execute(&*c) {
Ok(u) => Ok(u),
Err(msg) => Err(msg.to_string()),
}
}
}
}
}
2 changes: 2 additions & 0 deletions naphtha-proc-macro/src/database_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[cfg(feature = "mysql")]
pub(crate) mod mysql;
#[cfg(feature = "pg")]
pub(crate) mod pg;
#[cfg(feature = "sqlite")]
pub(crate) mod sqlite;
Loading

0 comments on commit 1d87b79

Please sign in to comment.