Skip to main content

toasty_cli/
migration.rs

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