Setup rotom_database crate
This commit is contained in:
36
rotom_database/src/lib.rs
Normal file
36
rotom_database/src/lib.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
#[cfg(feature = "mysql")]
|
||||
pub mod mysql;
|
||||
#[cfg(feature = "postgres")]
|
||||
pub mod postgres;
|
||||
#[cfg(feature = "sqlite")]
|
||||
pub mod sqlite;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum DatabaseDialect {
|
||||
#[cfg(feature = "mysql")]
|
||||
Mysql,
|
||||
#[cfg(feature = "postgres")]
|
||||
Postgres,
|
||||
#[cfg(feature = "sqlite")]
|
||||
Sqlite,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum BackendError {
|
||||
#[error(transparent)]
|
||||
DieselConnectionError(#[from] diesel::ConnectionError),
|
||||
#[error(transparent)]
|
||||
DieselQueryError(#[from] diesel::result::Error),
|
||||
#[error(transparent)]
|
||||
DieselMigrationError(Box<dyn std::error::Error + Send + Sync>),
|
||||
}
|
||||
|
||||
impl From<diesel_async::pooled_connection::PoolError> for BackendError {
|
||||
fn from(value: diesel_async::pooled_connection::PoolError) -> Self {
|
||||
use diesel_async::pooled_connection::PoolError as E;
|
||||
match value {
|
||||
E::ConnectionError(connection_error) => Self::from(connection_error),
|
||||
E::QueryError(error) => Self::from(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
29
rotom_database/src/mysql/mod.rs
Normal file
29
rotom_database/src/mysql/mod.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use diesel::Connection;
|
||||
use diesel::MysqlConnection;
|
||||
use diesel_async::pooled_connection::bb8::Pool;
|
||||
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
|
||||
use diesel_async::AsyncMysqlConnection;
|
||||
use diesel_migrations::embed_migrations;
|
||||
use diesel_migrations::EmbeddedMigrations;
|
||||
use diesel_migrations::MigrationHarness;
|
||||
|
||||
use crate::BackendError;
|
||||
|
||||
mod schema;
|
||||
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations/mysql");
|
||||
|
||||
pub fn run_pending_migrations(database_url: &str) -> Result<(), BackendError> {
|
||||
let mut connection = MysqlConnection::establish(database_url)?;
|
||||
connection.run_pending_migrations(MIGRATIONS)
|
||||
.map_err(BackendError::DieselMigrationError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn establish_async_pool(
|
||||
database_url: &str,
|
||||
) -> Result<Pool<AsyncMysqlConnection>, BackendError> {
|
||||
let config = AsyncDieselConnectionManager::new(database_url);
|
||||
let pool = Pool::builder().build(config).await?;
|
||||
Ok(pool)
|
||||
}
|
||||
0
rotom_database/src/mysql/schema.rs
Normal file
0
rotom_database/src/mysql/schema.rs
Normal file
29
rotom_database/src/postgres/mod.rs
Normal file
29
rotom_database/src/postgres/mod.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use diesel::Connection;
|
||||
use diesel::PgConnection;
|
||||
use diesel_async::pooled_connection::bb8::Pool;
|
||||
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
|
||||
use diesel_async::AsyncPgConnection;
|
||||
use diesel_migrations::embed_migrations;
|
||||
use diesel_migrations::EmbeddedMigrations;
|
||||
use diesel_migrations::MigrationHarness;
|
||||
|
||||
use crate::BackendError;
|
||||
|
||||
mod schema;
|
||||
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations/postgres");
|
||||
|
||||
pub fn run_pending_migrations(database_url: &str) -> Result<(), BackendError> {
|
||||
let mut connection = PgConnection::establish(database_url)?;
|
||||
connection.run_pending_migrations(MIGRATIONS)
|
||||
.map_err(BackendError::DieselMigrationError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn establish_async_pool(
|
||||
database_url: &str,
|
||||
) -> Result<Pool<AsyncPgConnection>, BackendError> {
|
||||
let config = AsyncDieselConnectionManager::new(database_url);
|
||||
let pool = Pool::builder().build(config).await?;
|
||||
Ok(pool)
|
||||
}
|
||||
0
rotom_database/src/postgres/schema.rs
Normal file
0
rotom_database/src/postgres/schema.rs
Normal file
29
rotom_database/src/sqlite/mod.rs
Normal file
29
rotom_database/src/sqlite/mod.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use diesel::Connection;
|
||||
use diesel::SqliteConnection;
|
||||
use diesel_async::pooled_connection::bb8::Pool;
|
||||
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
|
||||
use diesel_async::sync_connection_wrapper::SyncConnectionWrapper;
|
||||
use diesel_migrations::embed_migrations;
|
||||
use diesel_migrations::EmbeddedMigrations;
|
||||
use diesel_migrations::MigrationHarness;
|
||||
|
||||
use crate::BackendError;
|
||||
|
||||
mod schema;
|
||||
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations/sqlite");
|
||||
|
||||
pub fn run_pending_migrations(database_url: &str) -> Result<(), BackendError> {
|
||||
let mut connection = SqliteConnection::establish(database_url)?;
|
||||
connection.run_pending_migrations(MIGRATIONS)
|
||||
.map_err(BackendError::DieselMigrationError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn establish_async_pool(
|
||||
database_url: &str,
|
||||
) -> Result<Pool<SyncConnectionWrapper<SqliteConnection>>, BackendError> {
|
||||
let config = AsyncDieselConnectionManager::new(database_url);
|
||||
let pool = Pool::builder().build(config).await?;
|
||||
Ok(pool)
|
||||
}
|
||||
0
rotom_database/src/sqlite/schema.rs
Normal file
0
rotom_database/src/sqlite/schema.rs
Normal file
Reference in New Issue
Block a user