Skip to main content

toasty_cli/
migration.rs

1mod apply;
2mod config;
3mod drop;
4mod generate;
5mod reset;
6mod snapshot;
7
8pub use apply::ApplyCommand;
9pub use config::{MigrationConfig, MigrationPrefixStyle};
10pub use drop::DropCommand;
11pub use generate::GenerateCommand;
12pub use reset::ResetCommand;
13pub use snapshot::SnapshotCommand;
14
15use crate::Config;
16use anyhow::Result;
17use clap::Parser;
18use toasty::Db;
19
20/// Top-level `migration` subcommand.
21///
22/// Groups all migration-related subcommands: apply, generate, snapshot, drop,
23/// and reset. This struct is used by clap to parse `toasty migration <sub>`.
24#[derive(Parser, Debug)]
25pub struct MigrationCommand {
26    #[command(subcommand)]
27    subcommand: MigrationSubcommand,
28}
29
30#[derive(Parser, Debug)]
31enum MigrationSubcommand {
32    /// Apply pending migrations to the database
33    Apply(ApplyCommand),
34
35    /// Generate a new migration based on schema changes
36    Generate(GenerateCommand),
37
38    /// Print the current schema snapshot file
39    Snapshot(SnapshotCommand),
40
41    /// Drop a migration from the history
42    Drop(DropCommand),
43
44    /// Reset the database (drop all tables) and optionally re-apply migrations
45    Reset(ResetCommand),
46}
47
48impl MigrationCommand {
49    pub(crate) async fn run(self, db: &Db, config: &Config) -> Result<()> {
50        self.subcommand.run(db, config).await
51    }
52}
53
54impl MigrationSubcommand {
55    async fn run(self, db: &Db, config: &Config) -> Result<()> {
56        match self {
57            Self::Apply(cmd) => cmd.run(db, config).await,
58            Self::Generate(cmd) => cmd.run(db, config),
59            Self::Snapshot(cmd) => cmd.run(db, config),
60            Self::Drop(cmd) => cmd.run(db, config),
61            Self::Reset(cmd) => cmd.run(db, config).await,
62        }
63    }
64}