2020-07-14 18:00:09 +02:00
|
|
|
use std::process::Command;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2019-06-02 13:35:01 +02:00
|
|
|
use chrono::prelude::*;
|
2020-07-14 18:00:09 +02:00
|
|
|
use diesel::{r2d2, r2d2::ConnectionManager, Connection as DieselConnection, ConnectionError};
|
|
|
|
use rocket::{
|
|
|
|
http::Status,
|
2020-07-19 21:01:31 +02:00
|
|
|
request::{FromRequest, Outcome},
|
|
|
|
Request, State,
|
2020-07-14 18:00:09 +02:00
|
|
|
};
|
2019-05-03 15:46:29 +02:00
|
|
|
|
2020-07-14 18:00:09 +02:00
|
|
|
use crate::{error::Error, CONFIG};
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
/// An alias to the database connection used
|
2019-05-26 23:02:41 +02:00
|
|
|
#[cfg(feature = "sqlite")]
|
2019-05-27 22:58:52 +02:00
|
|
|
type Connection = diesel::sqlite::SqliteConnection;
|
2019-05-26 23:02:41 +02:00
|
|
|
#[cfg(feature = "mysql")]
|
2019-05-27 22:58:52 +02:00
|
|
|
type Connection = diesel::mysql::MysqlConnection;
|
2019-09-12 22:12:22 +02:00
|
|
|
#[cfg(feature = "postgresql")]
|
|
|
|
type Connection = diesel::pg::PgConnection;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2019-05-27 22:58:52 +02:00
|
|
|
/// An alias to the type for a pool of Diesel connections.
|
2018-02-10 01:00:55 +01:00
|
|
|
type Pool = r2d2::Pool<ConnectionManager<Connection>>;
|
|
|
|
|
|
|
|
/// Connection request guard type: a wrapper around an r2d2 pooled connection.
|
|
|
|
pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<Connection>>);
|
|
|
|
|
|
|
|
pub mod models;
|
2019-05-26 23:02:41 +02:00
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
#[path = "schemas/sqlite/schema.rs"]
|
2018-12-30 23:34:31 +01:00
|
|
|
pub mod schema;
|
2019-05-26 23:02:41 +02:00
|
|
|
#[cfg(feature = "mysql")]
|
|
|
|
#[path = "schemas/mysql/schema.rs"]
|
|
|
|
pub mod schema;
|
2019-09-12 22:12:22 +02:00
|
|
|
#[cfg(feature = "postgresql")]
|
|
|
|
#[path = "schemas/postgresql/schema.rs"]
|
|
|
|
pub mod schema;
|
2019-05-26 23:02:41 +02:00
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
/// Initializes a database pool.
|
|
|
|
pub fn init_pool() -> Pool {
|
2019-01-25 18:23:51 +01:00
|
|
|
let manager = ConnectionManager::new(CONFIG.database_url());
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
r2d2::Pool::builder().build(manager).expect("Failed to create pool")
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_connection() -> Result<Connection, ConnectionError> {
|
2019-06-02 13:35:01 +02:00
|
|
|
Connection::establish(&CONFIG.database_url())
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 15:46:29 +02:00
|
|
|
/// Creates a back-up of the database using sqlite3
|
|
|
|
pub fn backup_database() -> Result<(), Error> {
|
2019-10-11 12:08:40 +02:00
|
|
|
use std::path::Path;
|
|
|
|
let db_url = CONFIG.database_url();
|
|
|
|
let db_path = Path::new(&db_url).parent().unwrap();
|
|
|
|
|
2019-05-03 15:46:29 +02:00
|
|
|
let now: DateTime<Utc> = Utc::now();
|
2019-07-09 17:26:34 +02:00
|
|
|
let file_date = now.format("%Y%m%d").to_string();
|
2019-05-03 15:46:29 +02:00
|
|
|
let backup_command: String = format!("{}{}{}", ".backup 'db_", file_date, ".sqlite3'");
|
|
|
|
|
|
|
|
Command::new("sqlite3")
|
2019-10-11 12:08:40 +02:00
|
|
|
.current_dir(db_path)
|
2019-05-03 15:46:29 +02:00
|
|
|
.args(&["db.sqlite3", &backup_command])
|
|
|
|
.output()
|
|
|
|
.expect("Can't open database, sqlite3 is not available, make sure it's installed and available on the PATH");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
/// Attempts to retrieve a single connection from the managed database pool. If
|
|
|
|
/// no pool is currently managed, fails with an `InternalServerError` status. If
|
|
|
|
/// no connections are available, fails with a `ServiceUnavailable` status.
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
|
|
|
|
type Error = ();
|
|
|
|
|
2020-07-19 21:01:31 +02:00
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<DbConn, ()> {
|
2020-03-16 16:36:44 +01:00
|
|
|
// https://github.com/SergioBenitez/Rocket/commit/e3c1a4ad3ab9b840482ec6de4200d30df43e357c
|
|
|
|
let pool = try_outcome!(request.guard::<State<Pool>>());
|
2018-02-10 01:00:55 +01:00
|
|
|
match pool.get() {
|
|
|
|
Ok(conn) => Outcome::Success(DbConn(conn)),
|
2018-12-30 23:34:31 +01:00
|
|
|
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the convenience of using an &DbConn as a &Database.
|
2020-07-14 18:00:09 +02:00
|
|
|
impl std::ops::Deref for DbConn {
|
2018-02-10 01:00:55 +01:00
|
|
|
type Target = Connection;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|