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