Skip to content

Commit 0aad28a

Browse files
committed
dry run for migrations
1 parent 531811a commit 0aad28a

File tree

6 files changed

+166
-66
lines changed

6 files changed

+166
-66
lines changed

src/args.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,17 @@ impl ClapArgumentLoader {
149149
.about("Runs the migrations.")
150150
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
151151
.arg(clap::Arg::new("count").short('c').long("count").required(false))
152-
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying")),
152+
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying"))
153+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
153154
)
154155
.subcommand(
155156
clap::Command::new("down")
156157
.about("Rolls back the migrations.")
157158
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
158159
.arg(clap::Arg::new("remote").short('r').long("remote").required(false).num_args(0))
159160
.arg(clap::Arg::new("count").short('c').long("count").required(false))
160-
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying")),
161+
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying"))
162+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
161163
)
162164
.subcommand(
163165
clap::Command::new("list")
@@ -188,14 +190,16 @@ impl ClapArgumentLoader {
188190
clap::Command::new("up")
189191
.about("Applies a specific migration.")
190192
.arg(clap::Arg::new("id").help("Migration ID to apply").required(true))
191-
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false)),
193+
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
194+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
192195
)
193196
.subcommand(
194197
clap::Command::new("down")
195198
.about("Reverts a specific migration.")
196199
.arg(clap::Arg::new("id").help("Migration ID to revert").required(true))
197200
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
198-
.arg(clap::Arg::new("remote").short('r').long("remote").required(false).num_args(0)),
201+
.arg(clap::Arg::new("remote").short('r').long("remote").required(false).num_args(0))
202+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
199203
),
200204
),
201205
)
@@ -218,15 +222,17 @@ impl ClapArgumentLoader {
218222
.about("Runs the migrations.")
219223
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
220224
.arg(clap::Arg::new("count").short('c').long("count").required(false))
221-
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying")),
225+
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying"))
226+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
222227
)
223228
.subcommand(
224229
clap::Command::new("down")
225230
.about("Rolls back the migrations.")
226231
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
227232
.arg(clap::Arg::new("remote").short('r').long("remote").required(false).num_args(0))
228233
.arg(clap::Arg::new("count").short('c').long("count").required(false))
229-
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying")),
234+
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying"))
235+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
230236
)
231237
.subcommand(
232238
clap::Command::new("list")
@@ -257,14 +263,16 @@ impl ClapArgumentLoader {
257263
clap::Command::new("up")
258264
.about("Applies a specific migration.")
259265
.arg(clap::Arg::new("id").help("Migration ID to apply").required(true))
260-
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false)),
266+
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
267+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
261268
)
262269
.subcommand(
263270
clap::Command::new("down")
264271
.about("Reverts a specific migration.")
265272
.arg(clap::Arg::new("id").help("Migration ID to revert").required(true))
266273
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
267-
.arg(clap::Arg::new("remote").short('r').long("remote").required(false).num_args(0)),
274+
.arg(clap::Arg::new("remote").short('r').long("remote").required(false).num_args(0))
275+
.arg(clap::Arg::new("dry").long("dry").required(false).num_args(0).help("Execute migration in a transaction but rollback instead of committing")),
268276
),
269277
),
270278
)
@@ -310,13 +318,15 @@ impl ClapArgumentLoader {
310318
timeout: up_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
311319
count: up_subc.get_one::<String>("count").map(|s| s.parse::<usize>().unwrap()),
312320
diff: up_subc.get_flag("diff"),
321+
dry: up_subc.get_flag("dry"),
313322
}
314323
} else if let Some(down_subc) = postgres_subc.subcommand_matches("down") {
315324
crate::subsystem::postgres::commands::Command::Down {
316325
timeout: down_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
317326
count: down_subc.get_one::<String>("count").map(|s| s.parse::<usize>().unwrap()),
318327
remote: down_subc.get_flag("remote"),
319328
diff: down_subc.get_flag("diff"),
329+
dry: down_subc.get_flag("dry"),
320330
}
321331
} else if let Some(_) = postgres_subc.subcommand_matches("list") {
322332
crate::subsystem::postgres::commands::Command::List
@@ -336,12 +346,14 @@ impl ClapArgumentLoader {
336346
crate::subsystem::postgres::commands::Command::Apply(crate::subsystem::postgres::commands::MigrationApply::Up {
337347
id: up_subc.get_one::<String>("id").unwrap().clone(),
338348
timeout: up_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
349+
dry: up_subc.get_flag("dry"),
339350
})
340351
} else if let Some(down_subc) = apply_subc.subcommand_matches("down") {
341352
crate::subsystem::postgres::commands::Command::Apply(crate::subsystem::postgres::commands::MigrationApply::Down {
342353
id: down_subc.get_one::<String>("id").unwrap().clone(),
343354
timeout: down_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
344355
remote: down_subc.get_flag("remote"),
356+
dry: down_subc.get_flag("dry"),
345357
})
346358
} else {
347359
unreachable!();
@@ -364,13 +376,15 @@ impl ClapArgumentLoader {
364376
timeout: up_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
365377
count: up_subc.get_one::<String>("count").map(|s| s.parse::<usize>().unwrap()),
366378
diff: up_subc.get_flag("diff"),
379+
dry: up_subc.get_flag("dry"),
367380
}
368381
} else if let Some(down_subc) = sqlite_subc.subcommand_matches("down") {
369382
crate::subsystem::sqlite::commands::Command::Down {
370383
timeout: down_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
371384
count: down_subc.get_one::<String>("count").map(|s| s.parse::<usize>().unwrap()),
372385
remote: down_subc.get_flag("remote"),
373386
diff: down_subc.get_flag("diff"),
387+
dry: down_subc.get_flag("dry"),
374388
}
375389
} else if let Some(_) = sqlite_subc.subcommand_matches("list") {
376390
crate::subsystem::sqlite::commands::Command::List
@@ -390,12 +404,14 @@ impl ClapArgumentLoader {
390404
crate::subsystem::sqlite::commands::Command::Apply(crate::subsystem::sqlite::commands::MigrationApply::Up {
391405
id: up_subc.get_one::<String>("id").unwrap().clone(),
392406
timeout: up_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
407+
dry: up_subc.get_flag("dry"),
393408
})
394409
} else if let Some(down_subc) = apply_subc.subcommand_matches("down") {
395410
crate::subsystem::sqlite::commands::Command::Apply(crate::subsystem::sqlite::commands::MigrationApply::Down {
396411
id: down_subc.get_one::<String>("id").unwrap().clone(),
397412
timeout: down_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
398413
remote: down_subc.get_flag("remote"),
414+
dry: down_subc.get_flag("dry"),
399415
})
400416
} else {
401417
unreachable!();

src/main.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ async fn main() -> Result<()> {
6161
| crate::subsystem::postgres::commands::Command::Init => {
6262
crate::subsystem::postgres::migration::init(&path).await
6363
},
64-
| crate::subsystem::postgres::commands::Command::Up { timeout, count, diff } => {
65-
crate::subsystem::postgres::migration::up(&path, timeout, count, diff).await
64+
| crate::subsystem::postgres::commands::Command::Up { timeout, count, diff, dry } => {
65+
crate::subsystem::postgres::migration::up(&path, timeout, count, diff, dry).await
6666
},
67-
| crate::subsystem::postgres::commands::Command::Down { timeout, count, remote, diff } => {
68-
crate::subsystem::postgres::migration::down(&path, timeout, count, remote, diff).await
67+
| crate::subsystem::postgres::commands::Command::Down { timeout, count, remote, diff, dry } => {
68+
crate::subsystem::postgres::migration::down(&path, timeout, count, remote, diff, dry).await
6969
},
7070
| crate::subsystem::postgres::commands::Command::Apply(apply_cmd) => {
7171
match apply_cmd {
72-
| crate::subsystem::postgres::commands::MigrationApply::Up { id, timeout } => {
73-
crate::subsystem::postgres::migration::apply_up(&path, &id, timeout).await
72+
| crate::subsystem::postgres::commands::MigrationApply::Up { id, timeout, dry } => {
73+
crate::subsystem::postgres::migration::apply_up(&path, &id, timeout, dry).await
7474
},
75-
| crate::subsystem::postgres::commands::MigrationApply::Down { id, timeout, remote } => {
76-
crate::subsystem::postgres::migration::apply_down(&path, &id, timeout, remote).await
75+
| crate::subsystem::postgres::commands::MigrationApply::Down { id, timeout, remote, dry } => {
76+
crate::subsystem::postgres::migration::apply_down(&path, &id, timeout, remote, dry).await
7777
},
7878
}
7979
},
@@ -101,19 +101,19 @@ async fn main() -> Result<()> {
101101
| crate::subsystem::sqlite::commands::Command::Init => {
102102
crate::subsystem::sqlite::migration::init(&path).await
103103
},
104-
| crate::subsystem::sqlite::commands::Command::Up { timeout, count, diff } => {
105-
crate::subsystem::sqlite::migration::up(&path, timeout, count, diff).await
104+
| crate::subsystem::sqlite::commands::Command::Up { timeout, count, diff, dry } => {
105+
crate::subsystem::sqlite::migration::up(&path, timeout, count, diff, dry).await
106106
},
107-
| crate::subsystem::sqlite::commands::Command::Down { timeout, count, remote, diff } => {
108-
crate::subsystem::sqlite::migration::down(&path, timeout, count, remote, diff).await
107+
| crate::subsystem::sqlite::commands::Command::Down { timeout, count, remote, diff, dry } => {
108+
crate::subsystem::sqlite::migration::down(&path, timeout, count, remote, diff, dry).await
109109
},
110110
| crate::subsystem::sqlite::commands::Command::Apply(apply_cmd) => {
111111
match apply_cmd {
112-
| crate::subsystem::sqlite::commands::MigrationApply::Up { id, timeout } => {
113-
crate::subsystem::sqlite::migration::apply_up(&path, &id, timeout).await
112+
| crate::subsystem::sqlite::commands::MigrationApply::Up { id, timeout, dry } => {
113+
crate::subsystem::sqlite::migration::apply_up(&path, &id, timeout, dry).await
114114
},
115-
| crate::subsystem::sqlite::commands::MigrationApply::Down { id, timeout, remote } => {
116-
crate::subsystem::sqlite::migration::apply_down(&path, &id, timeout, remote).await
115+
| crate::subsystem::sqlite::commands::MigrationApply::Down { id, timeout, remote, dry } => {
116+
crate::subsystem::sqlite::migration::apply_down(&path, &id, timeout, remote, dry).await
117117
},
118118
}
119119
},

src/subsystem/postgres/commands.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ pub enum MigrationApply {
33
Up {
44
id: String,
55
timeout: Option<u64>,
6+
dry: bool,
67
},
78
Down {
89
id: String,
910
timeout: Option<u64>,
1011
remote: bool,
12+
dry: bool,
1113
},
1214
}
1315

@@ -25,12 +27,14 @@ pub enum Command {
2527
timeout: Option<u64>,
2628
count: Option<usize>,
2729
diff: bool,
30+
dry: bool,
2831
},
2932
Down {
3033
timeout: Option<u64>,
3134
count: Option<usize>,
3235
remote: bool,
3336
diff: bool,
37+
dry: bool,
3438
},
3539
Apply(MigrationApply),
3640
List,

0 commit comments

Comments
 (0)