Skip to main content

Module seed

Module seed 

Source
Available on crate features model-mysql or model-postgres or model-sqlite only.
Expand description

Seeds written in Rust (18.30.0), run after the seeds/*.sql files by DbPool::seed_with — and by an app’s own db seed command, built on rust_web_server::db_cli::run_with_seeders.

A seeder is a name and an async function of the pool; factories (#[derive(Factory)]) make the rows:

ⓘ
use rust_web_server::model::{DbPool, Seeders};

pub fn seeders() -> Seeders {
    Seeders::new()
        .add("users", |pool: DbPool| async move {
            User::factory().create_many(10, &pool).await?;
            Ok(())
        })
        .add("admin", |pool: DbPool| async move {
            User::factory().with(|u| u.admin = true).create(&pool).await?;
            Ok(())
        })
}

pool.seed_with("seeds", &seeders()).await?; // seeds/*.sql, then users, admin

Like SQL seeds, a seeder runs every time it is asked to — nothing records that it ran — so write it to tolerate the rows being there (look them up first, or use keys that a second insert can’t repeat). It isn’t wrapped in a transaction: use DbPool::transaction in it when its rows must go in together.

Structs§

Seeders
Seeders, run in the order added. Cheap to clone.

Traits§

Seeder
Rows put in a database by Rust code; see the module docs. Seeders::add makes one from a closure.

Type Aliases§

SeedFuture
The future a Seeder answers.