laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
use crate::support::on_each_database;
use laravel::config::database::ConnectionConfig;
use laravel::db::{self, col, values};
use laravel::testing::prelude::*;

fn app(connection: ConnectionConfig) -> Application {
    Application::configure()
        .for_testing(env!("CARGO_MANIFEST_DIR"))
        .with_config(move |config, _| {
            config.database.default = "app".into();
            config.database.add_connection("app", connection);
        })
        .create()
}

async fn orders() -> laravel::Result<()> {
    let connection = db::default_connection()?;
    connection.schema().drop_if_exists("orders").await?;
    connection
        .schema()
        .create("orders", |table| {
            table.id();
            table.string("status");
            table.big_integer("customer_id").nullable();
        })
        .await?;
    db::query!(
        "INSERT INTO orders (status, customer_id) VALUES
         ('paid', 1), ('paid', 1), ('open', 2), ('paid', NULL), ('cancelled', 2)"
    )
    .insert()
    .await?;

    Ok(())
}

async fn a_locked_aggregate_counts_grouped_and_distinct_rows(app: Application) -> laravel::Result<()> {
    app.scope(async || {
        orders().await?;

        let counts = db::transaction(async |_| {
            let groups = db::table("orders")
                .group_by(["status"])
                .lock_for_update()
                .count()
                .await?;
            let customers = db::table("orders")
                .select(["customer_id"])
                .distinct()
                .shared_lock()
                .count()
                .await?;
            let first_two = db::table("orders")
                .order_by("id")
                .limit(2)
                .lock_for_update()
                .count()
                .await?;
            Ok((groups, customers, first_two))
        })
        .await?;

        assert_eq!(counts, (3, 3, 2));
        Ok(())
    })
    .await
}

/// PostgreSQL and SQLite let an upsert's update read the table through its alias, as Laravel
/// Classic does. MySQL has no alias on an insert's table.
async fn upserts_read_the_tables_alias(app: Application) -> laravel::Result<()> {
    app.scope(async || {
        let connection = db::default_connection()?;
        connection.schema().drop_if_exists("page_views").await?;
        connection
            .schema()
            .create("page_views", |table| {
                table.string("path").primary();
                table.integer("views");
            })
            .await?;
        let record = |views: i64| {
            db::table("page_views as pv").upsert(
                [
                    values! { path: "/", views: views },
                    values! { path: "/about", views: views },
                ],
                ["path"],
                values! { views: col("pv.views") + db::excluded("views") },
            )
        };
        let inserted = record(2).await;
        if matches!(connection.driver(), db::Driver::MySql | db::Driver::MariaDb) {
            assert!(inserted.is_err(), "MySQL has no alias on an insert's table");
            return Ok(());
        }
        inserted?;
        record(3).await?;
        let views: Vec<(String, i64)> = db::table("page_views")
            .select(["path", "views"])
            .order_by("path")
            .get()
            .await?;
        assert_eq!(views, [("/".to_owned(), 5), ("/about".to_owned(), 5)]);

        Ok(())
    })
    .await
}

on_each_database! {
    a_locked_aggregate_counts_grouped_and_distinct_rows => "laravel_writes_locked_aggregate",
    upserts_read_the_tables_alias => "laravel_writes_upsert_alias",
}