roadster 0.7.0-beta

A "Batteries Included" web framework for rust designed to get you moving fast.
Documentation
//! This module provides pre-built SeaORM migrations for table schemas that are applicable
//! across many different applications and problem spaces.
//!
//! Additionally, some utilities are provided to create some common column types.

use crate::app::context::AppContext;
use crate::error::RoadsterResult;
use async_trait::async_trait;
use axum_core::extract::FromRef;
use serde_derive::Serialize;
use strum_macros::{EnumString, IntoStaticStr};
use typed_builder::TypedBuilder;

#[cfg(feature = "db-diesel")]
pub mod diesel;
#[cfg(feature = "db-sea-orm")]
pub mod sea_orm;

#[derive(Debug, Serialize, TypedBuilder)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
#[non_exhaustive]
pub struct UpArgs {
    /// The number of pending migration steps to apply.
    #[cfg_attr(feature = "cli", clap(short = 'n', long))]
    #[builder(default, setter(strip_option(fallback = steps_opt)))]
    pub steps: Option<usize>,
}

#[derive(Debug, Serialize, TypedBuilder)]
#[cfg_attr(feature = "cli", derive(clap::Parser))]
#[non_exhaustive]
pub struct DownArgs {
    /// The number of applied migration steps to roll back.
    #[cfg_attr(feature = "cli", clap(short = 'n', long))]
    #[builder(default, setter(strip_option(fallback = steps_opt)))]
    pub steps: Option<usize>,
}

#[derive(Debug, Serialize, TypedBuilder)]
pub struct MigrationInfo {
    pub name: String,
    pub status: MigrationStatus,
}

#[derive(Debug, Serialize, EnumString, IntoStaticStr)]
pub enum MigrationStatus {
    Applied,
    Pending,
}

#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait Migrator<S>: Send + Sync
where
    S: Clone + Send + Sync + 'static,
    AppContext: FromRef<S>,
{
    /// Apply pending migrations. Returns the number of migrations that were successfully
    /// applied.
    async fn up(&self, state: &S, args: &UpArgs) -> RoadsterResult<usize>;

    /// Roll back previous applied migrations. Returns the number of migrations that were
    /// successfully rolled back.
    async fn down(&self, state: &S, args: &DownArgs) -> RoadsterResult<usize>;

    /// Get the status of all migrations in this [`Migrator`].
    async fn status(&self, state: &S) -> RoadsterResult<Vec<MigrationInfo>>;
}