Skip to main content

rorm_cli/
cli.rs

1use clap::{Parser, Subcommand};
2
3use crate::init::init;
4use crate::make_migrations::{run_make_migrations, MakeMigrationsOptions};
5use crate::migrate::{run_migrate, MigrateOptions};
6use crate::squash_migrations::squash_migrations;
7
8#[derive(Subcommand)]
9pub enum InitDriver {
10    /// Initialize a sqlite configuration
11    #[cfg(feature = "sqlite")]
12    Sqlite {
13        /// Name of the sqlite file.
14        #[clap(long)]
15        #[clap(default_value = "db.sqlite3")]
16        filename: String,
17    },
18
19    /// Initialize a postgres configuration
20    #[cfg(feature = "postgres")]
21    Postgres {
22        /// The address to use to connect to the database.
23        #[clap(long)]
24        #[clap(default_value = "127.0.0.1")]
25        host: String,
26
27        /// The port to use to connect to the database.
28        #[clap(long)]
29        #[clap(default_value = "5432")]
30        port: u16,
31
32        /// The user to use to connect to the database.
33        #[clap(long)]
34        #[clap(default_value = "dbuser")]
35        user: String,
36
37        /// Set the password. To minimize the risk of exposing your password, use --ask-password instead.
38        #[clap(long)]
39        password: Option<String>,
40
41        /// Ask for the password for the database. If specified with the --password option, this value will be prevalent.
42        #[clap(long)]
43        ask_password: bool,
44
45        /// The name of the database to connect to.
46        #[clap(long)]
47        #[clap(default_value = "dbname")]
48        name: String,
49    },
50}
51
52#[derive(Subcommand)]
53pub enum Command {
54    /// Create the database configuration file
55    Init {
56        /// Path to the database configuration file that should be created.
57        #[clap(long)]
58        #[clap(default_value = "./database.toml")]
59        database_config: String,
60
61        /// Overwrite the database configuration if it is existent already
62        #[clap(short, long)]
63        force: bool,
64
65        #[clap(subcommand)]
66        driver: InitDriver,
67    },
68
69    /// Tool to create migrations
70    MakeMigrations {
71        /// Location of the intermediate representation of models.
72        #[clap(long)]
73        #[clap(default_value = "./.models.json")]
74        models_file: String,
75
76        /// Destination to / from which migrations are written / read.
77        #[clap(short, long)]
78        #[clap(default_value = "./migrations/")]
79        migration_dir: String,
80
81        /// Use this name as migration name instead of generating one.
82        name: Option<String>,
83
84        /// If set, no questions will be asked.
85        #[clap(long)]
86        non_interactive: bool,
87
88        /// If set, no warnings will be printed.
89        #[clap(long)]
90        disable_warnings: bool,
91    },
92
93    /// Apply migrations
94    Migrate {
95        /// Destination from which migrations are read.
96        #[clap(short, long)]
97        #[clap(default_value = "./migrations/")]
98        migration_dir: String,
99
100        /// Path to the database configuration file.
101        #[clap(long)]
102        #[clap(default_value = "./database.toml")]
103        database_config: String,
104
105        /// Only apply the migrations to (inclusive) the given migration.
106        #[clap(long)]
107        #[clap(id = "MIGRATION_ID")]
108        apply_until: Option<u16>,
109    },
110
111    /// Squash migrations
112    SquashMigrations {
113        /// Destination to / from which migrations are written / read.
114        #[clap(short, long)]
115        #[clap(default_value = "./migrations/")]
116        migration_dir: String,
117
118        /// First migration to start squashing from.
119        first_migration: u16,
120
121        /// Last migration to squash.
122        last_migration: u16,
123    },
124}
125
126/// CLI tool for rorm
127#[derive(Parser)]
128#[clap(version)]
129#[clap(arg_required_else_help = true)]
130#[clap(name = "rorm-cli")]
131pub struct Cli {
132    #[clap(subcommand)]
133    pub command: Command,
134}
135
136impl Cli {
137    /// Executes the parsed cli arguments
138    pub async fn run(self) -> anyhow::Result<()> {
139        match self.command {
140            Command::Init {
141                force,
142                driver,
143                database_config,
144            } => init(database_config, driver, force)?,
145            Command::MakeMigrations {
146                models_file,
147                migration_dir,
148                name,
149                non_interactive,
150                disable_warnings,
151            } => {
152                run_make_migrations(MakeMigrationsOptions {
153                    models_file,
154                    migration_dir,
155                    name,
156                    non_interactive,
157                    warnings_disabled: disable_warnings,
158                })?;
159            }
160            Command::Migrate {
161                migration_dir,
162                database_config,
163                apply_until,
164            } => {
165                run_migrate(MigrateOptions {
166                    migration_dir,
167                    database_config,
168                    apply_until,
169                })
170                .await?;
171            }
172            Command::SquashMigrations {
173                migration_dir,
174                first_migration,
175                last_migration,
176            } => {
177                squash_migrations(migration_dir, first_migration, last_migration).await?;
178            }
179        }
180        Ok(())
181    }
182}