Setup rotom_database crate
This commit is contained in:
29
rotom_database/Cargo.toml
Normal file
29
rotom_database/Cargo.toml
Normal file
@@ -0,0 +1,29 @@
|
||||
[package]
|
||||
name = "rotom_database"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
diesel = { version = "2.2.6", default_features = false }
|
||||
diesel-async = { version = "0.5.2", features = ["bb8"] }
|
||||
diesel_migrations = "2.2.0"
|
||||
thiserror = "2.0.11"
|
||||
|
||||
[features]
|
||||
default = ["mysql", "postgres", "sqlite"]
|
||||
mysql = [
|
||||
"diesel/mysql",
|
||||
"diesel-async/mysql",
|
||||
"diesel_migrations/mysql",
|
||||
]
|
||||
postgres = [
|
||||
"diesel/postgres",
|
||||
"diesel-async/postgres",
|
||||
"diesel_migrations/postgres",
|
||||
]
|
||||
sqlite = [
|
||||
"diesel/sqlite",
|
||||
"diesel/returning_clauses_for_sqlite_3_35",
|
||||
"diesel-async/sync-connection-wrapper",
|
||||
"diesel_migrations/sqlite",
|
||||
]
|
||||
3
rotom_database/build.rs
Normal file
3
rotom_database/build.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=migrations");
|
||||
}
|
||||
9
rotom_database/diesel_mysql.toml
Normal file
9
rotom_database/diesel_mysql.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/mysql/schema.rs"
|
||||
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations/mysql/"
|
||||
9
rotom_database/diesel_postgres.toml
Normal file
9
rotom_database/diesel_postgres.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/postgres/schema.rs"
|
||||
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations/postgres/"
|
||||
9
rotom_database/diesel_sqlite.toml
Normal file
9
rotom_database/diesel_sqlite.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
# For documentation on how to configure this file,
|
||||
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||
|
||||
[print_schema]
|
||||
file = "src/sqlite/schema.rs"
|
||||
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||
|
||||
[migrations_directory]
|
||||
dir = "migrations/sqlite/"
|
||||
0
rotom_database/migrations/mysql/.keep
Normal file
0
rotom_database/migrations/mysql/.keep
Normal file
0
rotom_database/migrations/postgres/.keep
Normal file
0
rotom_database/migrations/postgres/.keep
Normal file
@@ -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();
|
||||
@@ -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;
|
||||
0
rotom_database/migrations/sqlite/.keep
Normal file
0
rotom_database/migrations/sqlite/.keep
Normal file
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