Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to PostgreSQL/Diesel #458

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 269 additions & 51 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ tower-http = { version = "0.6", features = [
"sensitive-headers",
"cors",
] }
chrono = "0.4.38"
chrono = { version = "0.4", features = ["serde"] }
async-trait = "0.1.81"
# Investigate if wither::bson can be used instead and activate this feature.
bson = { version = "2.10.0", features = ["serde_with", "chrono-0_4"] }
jsonwebtoken = "9.3.0"
once_cell = "1.20.0"
once_cell = "1.19.0"
bcrypt = "0.15.1"
validator = { version = "0.18.1", features = ["derive"] }
mime = "0.3.17"
diesel = { version = "2.1", features = ["chrono"] }
diesel-async = { version = "0.5", features = ["postgres", "deadpool"] }
bytes = "1.7.2"
axum-extra = { version = "0.9.3", features = ["typed-header"] }

Expand Down
6 changes: 1 addition & 5 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
{
"environment": "development",

"server": {
"port": 8080
},

"database": {
"uri": "mongodb://localhost:27017",
"uri": "postgresql://localhost:5432",
"name": "rustapi"
},

"auth": {
"secret": "secret"
},

"logger": {
"level": "debug"
}
Expand Down
9 changes: 9 additions & 0 deletions diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]

[migrations_directory]
dir = "/Users/ndelvalle/Code/ndelvalle/rustapi/migrations"
Empty file added migrations/.keep
Empty file.
6 changes: 6 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/down.sql
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();
36 changes: 36 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/up.sql
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;
1 change: 1 addition & 0 deletions migrations/2024-07-14-055806_create_users/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE users
11 changes: 11 additions & 0 deletions migrations/2024-07-14-055806_create_users/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
locked_at TIMESTAMP
);

SELECT diesel_manage_updated_at('users');
1 change: 1 addition & 0 deletions migrations/2024-07-14-134620_create_cats/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE cats
9 changes: 9 additions & 0 deletions migrations/2024-07-14-134620_create_cats/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE cats (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

SELECT diesel_manage_updated_at('cats ');
40 changes: 26 additions & 14 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
use mongodb::Database;
use tokio::sync::OnceCell;
use wither::mongodb;
use diesel_async::pooled_connection::deadpool::{self, Pool};
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
use diesel_async::AsyncPgConnection;
use once_cell::sync::OnceCell;

use crate::settings::SETTINGS;

static CONNECTION: OnceCell<Database> = OnceCell::const_new();
type PooledConnection = deadpool::Object<AsyncPgConnection>;

pub async fn connection() -> &'static Database {
CONNECTION
.get_or_init(|| async {
let db_uri = SETTINGS.database.uri.as_str();
let db_name = SETTINGS.database.name.as_str();
static CONNECTION: OnceCell<Pool<AsyncPgConnection>> = OnceCell::new();

mongodb::Client::with_uri_str(db_uri)
.await
.expect("Failed to initialize MongoDB connection")
.database(db_name)
})
pub async fn get_connection() -> PooledConnection {
let conn = get_connection_pool()
.await
.get()
.await
.expect("Failed to get connection from the Pool");

conn
}

async fn get_connection_pool() -> &'static Pool<AsyncPgConnection> {
CONNECTION.get_or_init(|| {
let db_uri = SETTINGS.database.uri.as_str();
let db_name = SETTINGS.database.name.as_str();
let db_url = format!("{}/{}", db_uri, db_name);

let config = AsyncDieselConnectionManager::<AsyncPgConnection>::new(db_url);
Pool::builder(config)
.build()
.expect("Failed to initialize PostgreSQL connection pool")
})
}
5 changes: 5 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use bcrypt::BcryptError;
use diesel::result::Error as DieselError;
use serde_json::json;
use tokio::task::JoinError;
use wither::bson;
Expand All @@ -14,6 +15,9 @@ pub enum Error {
#[error("{0}")]
Wither(#[from] WitherError),

#[error("{0}")]
Diesel(#[from] DieselError),

#[error("{0}")]
Mongo(#[from] MongoError),

Expand Down Expand Up @@ -59,6 +63,7 @@ impl Error {
(StatusCode::INTERNAL_SERVER_ERROR, 5001)
}
Error::Wither(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5002),
Error::Diesel(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5002),
Error::Mongo(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5003),
Error::SerializeMongoResponse(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5004),
Error::RunSyncTask(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5005),
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod errors;
mod logger;
mod models;
mod routes;
mod schema;
mod settings;
mod utils;

Expand Down
4 changes: 2 additions & 2 deletions src/models/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use wither::Model as WitherModel;

use crate::utils::date;
use crate::utils::date::Date;
use crate::utils::models::ModelExt;
// use crate::utils::models::ModelExt;

impl ModelExt for Cat {}
// impl ModelExt for Cat {}

#[derive(Debug, Clone, Serialize, Deserialize, WitherModel, Validate)]
#[model(index(keys = r#"doc!{ "user": 1, "created_at": 1 }"#))]
Expand Down
7 changes: 3 additions & 4 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
pub mod cat;
pub mod user;

use crate::utils::models::ModelExt;
use crate::utils::models::QueryModelExt;
use crate::Error;

pub async fn sync_indexes() -> Result<(), Error> {
user::User::sync_indexes().await?;
cat::Cat::sync_indexes().await?;

// user::User::sync_indexes().await?;
// cat::Cat::sync_indexes().await?;
Ok(())
}
99 changes: 58 additions & 41 deletions src/models/user.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
use bson::serde_helpers::bson_datetime_as_rfc3339_string;
use bson::serde_helpers::serialize_object_id_as_hex_string;
use chrono::{NaiveDateTime, Utc};
use diesel::associations::Identifiable;
use diesel::prelude::{Insertable, Queryable, Selectable};
use diesel::query_dsl::methods::FilterDsl;
use diesel_async::RunQueryDsl;
use serde::{Deserialize, Serialize};
use tokio::task;
use validator::Validate;
use wither::bson::{doc, oid::ObjectId};
use wither::Model as WitherModel;

use crate::database;
use crate::errors::Error;
use crate::utils::date;
use crate::utils::date::Date;
use crate::utils::models::ModelExt;
use crate::schema::users;

impl ModelExt for User {}

#[derive(Debug, Clone, Serialize, Deserialize, WitherModel, Validate)]
#[model(index(keys = r#"doc!{ "email": 1 }"#, options = r#"doc!{ "unique": true }"#))]
#[derive(Debug, Clone, Serialize, Deserialize, Identifiable, Queryable, Selectable)]
#[diesel(table_name = crate::schema::users)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct User {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
#[validate(length(min = 1))]
pub id: i32,
pub name: String,
#[validate(email)]
pub email: String,
pub password: String,
pub updated_at: Date,
pub created_at: Date,
pub locked_at: Option<Date>,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub locked_at: Option<NaiveDateTime>,
}

#[derive(Debug, Insertable, Validate)]
#[diesel(table_name = crate::schema::users)]
pub struct NewUser<'a> {
#[validate(length(min = 5))]
pub name: &'a str,
#[validate(email)]
pub email: &'a str,
pub password: &'a str,
}

impl User {
pub fn new<A, B, C>(name: A, email: B, password_hash: C) -> Self
where
A: Into<String>,
B: Into<String>,
C: Into<String>,
{
let now = date::now();
Self {
id: None,
name: name.into(),
email: email.into(),
password: password_hash.into(),
updated_at: now,
created_at: now,
locked_at: None,
}
pub async fn create(name: &str, email: &str, password_hash: &str) -> User {
let now = Utc::now().naive_utc();
let new_user = NewUser {
name,
email,
password: password_hash,
};

let mut conn = database::get_connection().await;
let user = diesel::insert_into(users::table)
.values(&new_user)
.get_result::<Self>(&mut conn)
.await
.expect("Error saving new post");

user
}

pub async fn find_by_email(value: &str) -> Result<Option<Self>, Error> {
use crate::schema::users::dsl::*;
use diesel::ExpressionMethods;
use diesel::OptionalExtension;

let mut conn = database::get_connection().await;
users
.filter(email.eq(value))
.first::<User>(&mut conn)
.await
.optional()
.map_err(Error::Diesel)
}

pub fn is_password_match(&self, password: &str) -> bool {
Expand All @@ -54,20 +74,17 @@ impl User {

#[derive(Debug, Serialize, Deserialize)]
pub struct PublicUser {
#[serde(alias = "_id", serialize_with = "serialize_object_id_as_hex_string")]
pub id: ObjectId,
pub id: i32,
pub name: String,
pub email: String,
#[serde(with = "bson_datetime_as_rfc3339_string")]
pub updated_at: Date,
#[serde(with = "bson_datetime_as_rfc3339_string")]
pub created_at: Date,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
}

impl From<User> for PublicUser {
fn from(user: User) -> Self {
Self {
id: user.id.unwrap(),
id: user.id,
name: user.name.clone(),
email: user.email.clone(),
updated_at: user.updated_at,
Expand Down
1 change: 0 additions & 1 deletion src/routes/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::errors::Error;
use crate::models::cat::{Cat, PublicCat};
use crate::utils::custom_response::CustomResponseResult as Response;
use crate::utils::custom_response::{CustomResponse, CustomResponseBuilder, ResponsePagination};
use crate::utils::models::ModelExt;
use crate::utils::pagination::Pagination;
use crate::utils::to_object_id::to_object_id;
use crate::utils::token::TokenUser;
Expand Down
7 changes: 3 additions & 4 deletions src/routes/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::models::user;
use crate::models::user::{PublicUser, User};
use crate::settings::SETTINGS;
use crate::utils::custom_response::{CustomResponse, CustomResponseBuilder};
use crate::utils::models::ModelExt;
use crate::utils::token;

pub fn create_route() -> Router {
Expand All @@ -20,8 +19,8 @@ pub fn create_route() -> Router {

async fn create_user(Json(body): Json<CreateBody>) -> Result<CustomResponse<PublicUser>, Error> {
let password_hash = user::hash_password(body.password).await?;
let user = User::new(body.name, body.email, password_hash);
let user = User::create(user).await?;
// let user = User::new(body.name, body.email, password_hash);
let user = User::create(&body.name, &body.email, &password_hash).await;
let res = PublicUser::from(user);

let res = CustomResponseBuilder::new()
Expand All @@ -48,7 +47,7 @@ async fn authenticate_user(
return Err(Error::bad_request());
}

let user = User::find_one(doc! { "email": email }, None).await?;
let user = User::find_by_email(email).await?;

let user = match user {
Some(user) => user,
Expand Down
Loading
Loading