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