ryuk 0.1.0

A Laravel-style full-stack web framework for Rust: axum, SeaORM and Inertia, with auth, queues, mail, storage, cache, scheduling and an MCP server.
Documentation
//! SeaORM connection and migrations.
use sea_orm::{Database, DatabaseConnection, DbBackend, DbErr};
use sea_orm_migration::MigratorTrait;

/// Connect: `let db = ryuk::db::connect(&cfg.database_url).await?;`
///
/// The URL scheme (`sqlite:`, `postgres:`, `mysql:`) picks the driver; a scheme whose
/// driver feature is not compiled in gives an error naming the feature to enable.
pub async fn connect(url: &str) -> Result<DatabaseConnection, DbErr> {
    Database::connect(url).await.map_err(|e| {
        if !e.to_string().contains("no supporting driver") {
            return e;
        }
        let feature = match url.split(':').next().unwrap_or("") {
            "postgres" | "postgresql" => "postgres",
            "mysql" | "mariadb" => "mysql",
            "sqlite" => "sqlite",
            _ => {
                return DbErr::Custom(
                    "unsupported DATABASE_URL scheme (use sqlite:, postgres: or mysql:)".into(),
                );
            }
        };
        DbErr::Custom(format!(
            "DATABASE_URL uses `{feature}` but ryuk was built without the `{feature}` feature"
        ))
    })
}

/// `"sqlite"`, `"postgres"` or `"mysql"` for the connection's backend.
pub fn backend(db: &DatabaseConnection) -> &'static str {
    match db.get_database_backend() {
        DbBackend::Sqlite => "sqlite",
        DbBackend::Postgres => "postgres",
        _ => "mysql",
    }
}

/// Apply all pending migrations: `ryuk::db::migrate::<Migrator>(&db).await?;`
pub async fn migrate<M: MigratorTrait>(db: &DatabaseConnection) -> Result<(), DbErr> {
    M::up(db, None).await
}

#[cfg(all(test, feature = "sqlite"))]
mod tests {
    #[tokio::test]
    async fn backend_and_bad_scheme() {
        let db = super::connect("sqlite::memory:").await.unwrap();
        assert_eq!(super::backend(&db), "sqlite");
        let e = super::connect("oracle://x").await.unwrap_err().to_string();
        assert!(e.contains("unsupported"), "{e}");
    }
}