1use clap::{ArgGroup, Parser, Subcommand, ValueEnum};
2#[cfg(feature = "codegen")]
3use dotenvy::dotenv;
4
5#[cfg(feature = "codegen")]
6use crate::{handle_error, run_generate_command, run_migrate_command};
7
8#[derive(Parser, Debug)]
9#[command(
10 version,
11 author,
12 help_template = r#"{before-help}{name} {version}
13{about-with-newline}
14
15{usage-heading} {usage}
16
17{all-args}{after-help}
18
19AUTHORS:
20 {author}
21"#,
22 about = r#"
23 ____ ___ ____ __ __ /\
24 / ___| ___ __ _ / _ \ | _ \ | \/ | {.-}
25 \___ \ / _ \ / _` || | | || |_) || |\/| | ;_.-'\
26 ___) || __/| (_| || |_| || _ < | | | | { _.}_
27 |____/ \___| \__,_| \___/ |_| \_\|_| |_| \.-' / `,
28 \ | /
29 An async & dynamic ORM for Rust \ | ,/
30 =============================== \|_/
31
32 Getting Started
33 - Documentation: https://www.sea-ql.org/SeaORM
34 - Tutorial: https://www.sea-ql.org/sea-orm-tutorial
35 - Examples: https://github.com/SeaQL/sea-orm/tree/master/examples
36 - Cookbook: https://www.sea-ql.org/sea-orm-cookbook
37
38 Join our Discord server to chat with others in the SeaQL community!
39 - Invitation: https://discord.com/invite/uCPdDXzbdv
40
41 SeaQL Community Survey 2024
42 - Link: https://sea-ql.org/community-survey
43
44 If you like what we do, consider starring, sharing and contributing!
45"#
46)]
47pub struct Cli {
48 #[arg(global = true, short, long, help = "Show debug messages")]
49 pub verbose: bool,
50
51 #[command(subcommand)]
52 pub command: Commands,
53}
54
55#[derive(Subcommand, PartialEq, Eq, Debug)]
56pub enum Commands {
57 #[command(
58 about = "Codegen related commands",
59 arg_required_else_help = true,
60 display_order = 10
61 )]
62 Generate {
63 #[command(subcommand)]
64 command: GenerateSubcommands,
65 },
66 #[command(about = "Migration related commands", display_order = 20)]
67 Migrate {
68 #[arg(
69 global = true,
70 short = 'd',
71 long,
72 env = "MIGRATION_DIR",
73 help = "Migration script directory.
74If your migrations are in their own crate,
75you can provide the root of that crate.
76If your migrations are in a submodule of your app,
77you should provide the directory of that submodule.",
78 default_value = "./migration"
79 )]
80 migration_dir: String,
81
82 #[arg(
83 global = true,
84 short = 's',
85 long,
86 env = "DATABASE_SCHEMA",
87 long_help = "Database schema\n \
88 - For MySQL and SQLite, this argument is ignored.\n \
89 - For PostgreSQL, this argument is optional with default value 'public'.\n"
90 )]
91 database_schema: Option<String>,
92
93 #[arg(
94 global = true,
95 short = 'u',
96 long,
97 env = "DATABASE_URL",
98 help = "Database URL"
99 )]
100 database_url: Option<String>,
101
102 #[command(subcommand)]
103 command: Option<MigrateSubcommands>,
104 },
105}
106
107#[derive(Subcommand, PartialEq, Eq, Debug)]
108pub enum MigrateSubcommands {
109 #[command(about = "Initialize migration directory", display_order = 10)]
110 Init,
111 #[command(about = "Generate a new, empty migration", display_order = 20)]
112 Generate {
113 #[arg(required = true, help = "Name of the new migration")]
114 migration_name: String,
115
116 #[arg(
117 long,
118 default_value = "true",
119 help = "Generate migration file based on Utc time",
120 conflicts_with = "local_time",
121 display_order = 1001
122 )]
123 universal_time: bool,
124
125 #[arg(
126 long,
127 help = "Generate migration file based on Local time",
128 conflicts_with = "universal_time",
129 display_order = 1002
130 )]
131 local_time: bool,
132 },
133 #[command(
134 about = "Drop all tables from the database, then reapply all migrations",
135 display_order = 30
136 )]
137 Fresh,
138 #[command(
139 about = "Rollback all applied migrations, then reapply all migrations",
140 display_order = 40
141 )]
142 Refresh,
143 #[command(about = "Rollback all applied migrations", display_order = 50)]
144 Reset,
145 #[command(about = "Check the status of all migrations", display_order = 60)]
146 Status,
147 #[command(about = "Apply pending migrations", display_order = 70)]
148 Up {
149 #[arg(short, long, help = "Number of pending migrations to apply")]
150 num: Option<u32>,
151 },
152 #[command(about = "Rollback applied migrations", display_order = 80)]
153 Down {
154 #[arg(
155 short,
156 long,
157 default_value = "1",
158 help = "Number of applied migrations to be rolled back",
159 display_order = 90
160 )]
161 num: u32,
162 },
163}
164
165#[derive(Subcommand, PartialEq, Eq, Debug)]
166pub enum GenerateSubcommands {
167 #[command(about = "Generate entity")]
168 #[command(group(ArgGroup::new("formats").args(&["compact_format", "expanded_format"])))]
169 #[command(group(ArgGroup::new("group-tables").args(&["tables", "include_hidden_tables"])))]
170 Entity {
171 #[arg(long, help = "Generate entity file of compact format")]
172 compact_format: bool,
173
174 #[arg(long, help = "Generate entity file of expanded format")]
175 expanded_format: bool,
176
177 #[arg(
178 long,
179 help = "Generate entity file for hidden tables (i.e. table name starts with an underscore)"
180 )]
181 include_hidden_tables: bool,
182
183 #[arg(
184 short = 't',
185 long,
186 value_delimiter = ',',
187 help = "Generate entity file for specified tables only (comma separated)"
188 )]
189 tables: Vec<String>,
190
191 #[arg(
192 long,
193 value_delimiter = ',',
194 default_value = "seaql_migrations",
195 help = "Skip generating entity file for specified tables (comma separated)"
196 )]
197 ignore_tables: Vec<String>,
198
199 #[arg(
200 long,
201 default_value = "1",
202 help = "The maximum amount of connections to use when connecting to the database."
203 )]
204 max_connections: u32,
205
206 #[arg(
207 short = 'o',
208 long,
209 default_value = "./",
210 help = "Entity file output directory"
211 )]
212 output_dir: String,
213
214 #[arg(
215 short = 's',
216 long,
217 env = "DATABASE_SCHEMA",
218 long_help = "Database schema\n \
219 - For MySQL, this argument is ignored.\n \
220 - For PostgreSQL, this argument is optional with default value 'public'."
221 )]
222 database_schema: Option<String>,
223
224 #[arg(short = 'u', long, env = "DATABASE_URL", help = "Database URL")]
225 database_url: String,
226
227 #[arg(
228 long,
229 default_value = "none",
230 help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, \
231 serialize, deserialize, both)"
232 )]
233 with_serde: String,
234
235 #[arg(
236 long,
237 help = "Generate a serde field attribute, '#[serde(skip_deserializing)]', for the primary key fields to skip them during deserialization, this flag will be affective only when '--with-serde' is 'both' or 'deserialize'"
238 )]
239 serde_skip_deserializing_primary_key: bool,
240
241 #[arg(
242 long,
243 default_value = "false",
244 help = "Opt-in to add skip attributes to hidden columns (i.e. when 'with-serde' enabled and column name starts with an underscore)"
245 )]
246 serde_skip_hidden_column: bool,
247
248 #[arg(
249 long,
250 default_value = "false",
251 long_help = "Automatically derive the Copy trait on generated enums.\n\
252 Enums generated from a database don't have associated data by default, and as such can \
253 derive Copy.
254 "
255 )]
256 with_copy_enums: bool,
257
258 #[arg(
259 long,
260 default_value_t,
261 value_enum,
262 help = "The datetime crate to use for generating entities."
263 )]
264 date_time_crate: DateTimeCrate,
265
266 #[arg(
267 long,
268 short = 'l',
269 default_value = "false",
270 help = "Generate index file as `lib.rs` instead of `mod.rs`."
271 )]
272 lib: bool,
273
274 #[arg(
275 long,
276 value_delimiter = ',',
277 help = "Add extra derive macros to generated model struct (comma separated), e.g. `--model-extra-derives 'ts_rs::Ts','CustomDerive'`"
278 )]
279 model_extra_derives: Vec<String>,
280
281 #[arg(
282 long,
283 value_delimiter = ',',
284 help = r#"Add extra attributes to generated model struct, no need for `#[]` (comma separated), e.g. `--model-extra-attributes 'serde(rename_all = "camelCase")','ts(export)'`"#
285 )]
286 model_extra_attributes: Vec<String>,
287
288 #[arg(
289 long,
290 value_delimiter = ',',
291 help = "Add extra derive macros to generated enums (comma separated), e.g. `--enum-extra-derives 'ts_rs::Ts','CustomDerive'`"
292 )]
293 enum_extra_derives: Vec<String>,
294
295 #[arg(
296 long,
297 value_delimiter = ',',
298 help = r#"Add extra attributes to generated enums, no need for `#[]` (comma separated), e.g. `--enum-extra-attributes 'serde(rename_all = "camelCase")','ts(export)'`"#
299 )]
300 enum_extra_attributes: Vec<String>,
301
302 #[arg(
303 long,
304 default_value = "false",
305 long_help = "Generate helper Enumerations that are used by Seaography."
306 )]
307 seaography: bool,
308 },
309}
310
311#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Default)]
312pub enum DateTimeCrate {
313 #[default]
314 Chrono,
315 Time,
316}
317
318#[cfg(feature = "codegen")]
321pub async fn main() {
322 dotenv().ok();
323
324 let cli = Cli::parse();
325 let verbose = cli.verbose;
326
327 match cli.command {
328 Commands::Generate { command } => {
329 run_generate_command(command, verbose)
330 .await
331 .unwrap_or_else(handle_error);
332 }
333 Commands::Migrate {
334 migration_dir,
335 database_schema,
336 database_url,
337 command,
338 } => run_migrate_command(
339 command,
340 &migration_dir,
341 database_schema,
342 database_url,
343 verbose,
344 )
345 .unwrap_or_else(handle_error),
346 }
347}