toasty-cli 0.7.0

Command-line interface for Toasty schema management
Documentation
mod apply;
mod config;
mod drop;
mod generate;
mod reset;
mod snapshot;

pub use apply::ApplyCommand;
pub use config::{MigrationConfig, MigrationPrefixStyle};
pub use drop::DropCommand;
pub use generate::GenerateCommand;
pub use reset::ResetCommand;
pub use snapshot::SnapshotCommand;

use crate::Config;
use anyhow::Result;
use clap::Parser;
use toasty::Db;

/// Top-level `migration` subcommand.
///
/// Groups all migration-related subcommands: apply, generate, snapshot, drop,
/// and reset. This struct is used by clap to parse `toasty migration <sub>`.
#[derive(Parser, Debug)]
pub struct MigrationCommand {
    #[command(subcommand)]
    subcommand: MigrationSubcommand,
}

#[derive(Parser, Debug)]
enum MigrationSubcommand {
    /// Apply pending migrations to the database
    Apply(ApplyCommand),

    /// Generate a new migration based on schema changes
    Generate(GenerateCommand),

    /// Print the current schema snapshot file
    Snapshot(SnapshotCommand),

    /// Drop a migration from the history
    Drop(DropCommand),

    /// Reset the database (drop all tables) and optionally re-apply migrations
    Reset(ResetCommand),
}

impl MigrationCommand {
    pub(crate) async fn run(self, db: &Db, config: &Config) -> Result<()> {
        self.subcommand.run(db, config).await
    }
}

impl MigrationSubcommand {
    async fn run(self, db: &Db, config: &Config) -> Result<()> {
        match self {
            Self::Apply(cmd) => cmd.run(db, config).await,
            Self::Generate(cmd) => cmd.run(db, config),
            Self::Snapshot(cmd) => cmd.run(db, config),
            Self::Drop(cmd) => cmd.run(db, config),
            Self::Reset(cmd) => cmd.run(db, config).await,
        }
    }
}