toasty_cli/
migration.rs

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