midas-migrate-cli 0.8.2

Do painless migration 🦀
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Migration and project commands.

use std::fs;
use std::path::Path;

use anyhow::{
  Context as _,
  Result,
};
use clap::{
  value_parser,
  Arg,
  ArgMatches,
  Command,
};
use console::style;
use midas::commander::{
  MigrationInfo,
  Migrator,
  MigratorOptions,
};
use midas::config::Config;
use midas::sqlx::{
  qualified_table_name,
  SqlxMigrationType,
  SqlxVersioning,
};
use midas::{
  migration,
  sequel,
};
use prettytable::format::consts;
use prettytable::{
  row,
  Table,
};

use crate::commands::Context;

const PROJECT_FILE: &str = "midas.toml";
const SCHEMA_FILE: &str = "schema.toml";

/// Register migration commands on the root command.
pub fn register(app: Command) -> Command {
  let mut app = app
    .subcommand(
      Command::new("init")
        .about("Set up a midas project (midas.toml, schema.toml, migrations/)")
        .arg(
          Arg::new("force")
            .long("force")
            .help("Overwrite existing configuration files")
            .action(clap::ArgAction::SetTrue),
        ),
    )
    .subcommand(
      Command::new("add")
        .visible_alias("create")
        .visible_alias("c")
        .about("Create a timestamped migration file")
        .arg(Arg::new("name").help("The migration action name").required(true))
        .arg(
          Arg::new("reversible")
            .long("reversible")
            .help("Create a .up.sql/.down.sql pair")
            .action(clap::ArgAction::SetTrue),
        ),
    );

  for command in migration_commands() {
    app = app.subcommand(command.clone());
  }

  app.subcommand(
    Command::new("migrate")
      .about("Migration commands")
      .arg_required_else_help(true)
      .subcommands(migration_commands()),
  )
}

fn migration_commands() -> Vec<Command> {
  vec![
    Command::new("run")
      .visible_alias("up")
      .visible_alias("u")
      .about("Apply pending migrations")
      .arg(
        Arg::new("target")
          .long("target")
          .value_name("version")
          .help("Apply only migrations up to this version")
          .num_args(1)
          .value_parser(value_parser!(i64)),
      )
      .arg(json_flag()),
    Command::new("info")
      .visible_alias("status")
      .visible_alias("s")
      .about("Show applied, pending, and dirty migrations")
      .arg(json_flag()),
    Command::new("list")
      .visible_alias("ls")
      .about("List migration files without a database"),
    Command::new("revert")
      .visible_alias("r")
      .about("Revert applied migrations")
      .arg(
        Arg::new("to")
          .long("to")
          .value_name("version")
          .help("Revert every migration above this version")
          .num_args(1)
          .value_parser(value_parser!(i64)),
      ),
    Command::new("redo").about("Revert and re-apply the last migration"),
    Command::new("baseline")
      .about("Mark migrations as applied without running SQL")
      .arg(
        Arg::new("to")
          .long("to")
          .value_name("version")
          .help("Highest version to mark as applied")
          .num_args(1)
          .value_parser(value_parser!(i64))
          .required_unless_present("from"),
      )
      .arg(
        Arg::new("from")
          .long("from")
          .value_name("format")
          .help("Read applied versions from another migration engine")
          .value_parser(["sqlx"])
          .num_args(1),
      ),
    Command::new("repair").about("Recompute checksums for applied migrations"),
    Command::new("resolve")
      .about("Resolve a dirty migration")
      .disable_version_flag(true)
      .arg(
        Arg::new("applied")
          .long("applied")
          .help("Mark the dirty migration as applied")
          .action(clap::ArgAction::SetTrue),
      )
      .arg(
        Arg::new("rolled-back")
          .long("rolled-back")
          .help("Remove the dirty migration record")
          .action(clap::ArgAction::SetTrue),
      )
      .arg(
        Arg::new("migration_version")
          .long("version")
          .value_name("version")
          .help("Version to resolve (defaults to the lowest dirty version)")
          .num_args(1)
          .value_parser(value_parser!(i64)),
      ),
    Command::new("reset").about("Drop metadata and replay every migration"),
    Command::new("down")
      .visible_alias("d")
      .about("Revert every applied migration")
      .arg(
        Arg::new("force")
          .long("force")
          .help("Required confirmation for reverting everything")
          .action(clap::ArgAction::SetTrue),
      ),
    crate::commands::migrate_dev::dev_command(),
    crate::commands::migrate_dev::diff_command(),
    export_command(),
    import_command(),
    Command::new("adopt")
      .about("Verify that an existing SQLx metadata table can be adopted")
      .arg(
        Arg::new("table")
          .long("table")
          .value_name("name")
          .help("SQLx metadata table, optionally schema-qualified")
          .default_value("_sqlx_migrations")
          .num_args(1),
      ),
  ]
}

fn export_command() -> Command {
  Command::new("export")
    .about("Export migrations for another migration tool")
    .arg(
      Arg::new("format")
        .long("format")
        .value_name("format")
        .help("Output format")
        .value_parser(["sqlx"])
        .default_value("sqlx")
        .num_args(1),
    )
    .arg(
      Arg::new("out")
        .long("out")
        .value_name("dir")
        .help("Directory to write exported migrations into")
        .required(true)
        .num_args(1),
    )
}

fn import_command() -> Command {
  Command::new("import")
    .about("Import migrations from another migration tool")
    .arg(
      Arg::new("format")
        .long("format")
        .value_name("format")
        .help("Input format")
        .value_parser(["sqlx"])
        .default_value("sqlx")
        .num_args(1),
    )
    .arg(
      Arg::new("from")
        .long("from")
        .value_name("dir")
        .help("Directory to read migrations from")
        .required(true)
        .num_args(1),
    )
    .arg(
      Arg::new("out")
        .long("out")
        .value_name("dir")
        .help("Directory to write Midas migrations into")
        .required(true)
        .num_args(1),
    )
}

fn json_flag() -> Arg {
  Arg::new("json")
    .long("json")
    .help("Emit machine-readable JSON")
    .action(clap::ArgAction::SetTrue)
}

/// Whether this module owns the given subcommand name.
pub fn owns(name: &str) -> bool {
  matches!(
    name,
    "init"
      | "add"
      | "create"
      | "c"
      | "run"
      | "up"
      | "u"
      | "info"
      | "status"
      | "s"
      | "list"
      | "ls"
      | "revert"
      | "r"
      | "redo"
      | "baseline"
      | "repair"
      | "resolve"
      | "reset"
      | "down"
      | "d"
      | "migrate"
      | "export"
      | "import"
      | "adopt"
  )
}

/// Dispatch a migration subcommand.
pub fn dispatch(name: &str, matches: &ArgMatches, context: &Context) -> Result<()> {
  if name == "migrate" {
    let Some((nested, sub)) = matches.subcommand() else {
      return Ok(());
    };
    return dispatch(nested, sub, context);
  }

  match name {
    "init" => init(&context.config, matches.get_flag("force")),
    "add" | "create" | "c" => add(matches, context),
    "run" | "up" | "u" => run(matches, context),
    "info" | "status" | "s" => info(matches, context),
    "list" | "ls" => list(&context.config),
    "revert" | "r" => revert(matches, context),
    "redo" => with_migrator(context, false, |migrator| migrator.redo()).map(drop),
    "baseline" => baseline(matches, context),
    "repair" => with_migrator(context, false, |migrator| migrator.repair()).map(drop),
    "resolve" => resolve(matches, context),
    "reset" => with_migrator(context, false, |migrator| migrator.reset()).map(drop),
    "down" | "d" => with_migrator(context, false, |migrator| {
      migrator.down(matches.get_flag("force"))
    })
    .map(drop),
    "dev" | "diff" => crate::commands::migrate_dev::dispatch(name, matches, context),
    "export" => export(matches, context),
    "import" => import(matches, context),
    "adopt" => adopt(matches, context),
    other => anyhow::bail!("unknown migration subcommand `{other}`"),
  }
}

fn init(config: &Config, force: bool) -> Result<()> {
  let project_file = Path::new(PROJECT_FILE);
  let schema_file = Path::new(SCHEMA_FILE);

  if !force {
    for path in [project_file, schema_file] {
      if path.exists() {
        return Err(
          midas::Error::config(format!(
            "{} already exists; rerun with --force to overwrite",
            path.to_string_lossy()
          ))
          .into(),
        );
      }
    }
  }

  fs::create_dir_all(&config.migrations_dir).with_context(|| {
    format!(
      "failed to create migrations directory `{}`",
      config.migrations_dir.to_string_lossy()
    )
  })?;

  if !project_file.exists() || force {
    fs::write(project_file, project_template(config)).context("failed to write midas.toml")?;
  }
  if !schema_file.exists() || force {
    fs::write(schema_file, schema_template()).context("failed to write schema.toml")?;
  }

  println!("Initialized midas project.");
  Ok(())
}

fn add(matches: &ArgMatches, context: &Context) -> Result<()> {
  let slug = matches
    .get_one::<String>("name")
    .context("migration name is required")?;
  let existing = migration::discover(&context.config.migrations_dir)?;
  let reversible = matches.get_flag("reversible")
    || match context.config.sqlx.default_migration_type {
      SqlxMigrationType::Reversible => true,
      SqlxMigrationType::Simple => false,
      SqlxMigrationType::Inferred => existing
        .values()
        .next_back()
        .is_some_and(|migration| migration.down.is_some()),
    };
  let versioning = match context.config.sqlx.default_versioning {
    SqlxVersioning::Sequential => migration::Versioning::Sequential,
    SqlxVersioning::Timestamp => migration::Versioning::Timestamp,
    SqlxVersioning::Inferred => infer_versioning(&existing),
  };
  let version =
    migration::create_migration_file_with(&context.config.migrations_dir, slug, reversible, versioning)?;
  println!("Created migration {version:013}.");
  Ok(())
}

fn infer_versioning(existing: &migration::MigrationList) -> migration::Versioning {
  let versions = existing.keys().copied().collect::<Vec<_>>();
  let sequential = match versions.as_slice() {
    [only] => *only == 1,
    [.., previous, latest] => latest - previous == 1,
    [] => false,
  };
  if sequential {
    migration::Versioning::Sequential
  } else {
    migration::Versioning::Timestamp
  }
}

fn run(matches: &ArgMatches, context: &Context) -> Result<()> {
  let target = matches.get_one::<i64>("target").copied();
  if target.is_some_and(|target| target < 0) {
    anyhow::bail!("target version must be non-negative");
  }
  let info = with_migrator(context, matches.get_flag("json"), |migrator| migrator.up(target))?;
  output(matches, &info)
}

fn info(matches: &ArgMatches, context: &Context) -> Result<()> {
  let info = if matches.get_flag("json") {
    with_migrator(context, true, |migrator| migrator.info())?
  } else {
    with_migrator(context, false, |migrator| migrator.status())?
  };
  output(matches, &info)
}

fn list(config: &Config) -> Result<()> {
  let migrations = migration::discover(&config.migrations_dir)?;
  if migrations.is_empty() {
    println!("There are no available migration files.");
    return Ok(());
  }

  let mut table = Table::new();
  table.set_titles(row![Fbb->"Migration No.", Fbb->"Filename", Fbb->"Description"]);
  table.set_format(*consts::FORMAT_CLEAN);

  for migration in migrations.values() {
    let filename = migration
      .source_path
      .file_name()
      .map(|name| name.to_string_lossy().into_owned())
      .unwrap_or_default();
    table.add_row(row![
      b->&format!("{:013}", migration.version),
      Fgb->&filename,
      &migration.description
    ]);
  }

  let msg = style("Available migrations:").bold().cyan();
  println!();
  println!("{msg}");
  println!();
  table.printstd();
  println!();

  Ok(())
}

fn revert(matches: &ArgMatches, context: &Context) -> Result<()> {
  let target = matches.get_one::<i64>("to").copied();
  if target.is_some_and(|target| target < 0) {
    anyhow::bail!("revert target must be non-negative");
  }
  with_migrator(context, false, |migrator| migrator.revert_to(target)).map(drop)
}

fn baseline(matches: &ArgMatches, context: &Context) -> Result<()> {
  if matches.get_one::<String>("from").is_some() {
    let options = migrator_options(context, false);
    let report = midas::sqlx::baseline_from_sqlx(&context.config, &options)?;
    if report.inserted.is_empty() {
      println!("No SQLx metadata needed to be baselined.");
    } else {
      println!(
        "Baselined {} migration(s) from SQLx metadata.",
        report.inserted.len()
      );
    }
    if !report.skipped.is_empty() {
      println!("Skipped {} already-applied migration(s).", report.skipped.len());
    }
    return Ok(());
  }

  let target = *matches
    .get_one::<i64>("to")
    .context("baseline version is required")?;
  if target < 0 {
    anyhow::bail!("baseline version must be non-negative");
  }
  with_migrator(context, false, |migrator| migrator.baseline(target)).map(drop)
}

fn export(matches: &ArgMatches, context: &Context) -> Result<()> {
  let out = matches
    .get_one::<String>("out")
    .context("export directory is required")?;
  let migrations = migration::discover(&context.config.migrations_dir)?;
  let written = midas::sqlx::export_sqlx(&migrations, Path::new(out))?;
  println!("Exported {} migration file(s) to `{out}`.", written.len());
  Ok(())
}

fn import(matches: &ArgMatches, context: &Context) -> Result<()> {
  let from = matches
    .get_one::<String>("from")
    .context("import directory is required")?;
  let out = matches
    .get_one::<String>("out")
    .context("output directory is required")?;
  let migrations = midas::sqlx::import_sqlx_with(Path::new(from), &context.config.sqlx)?;
  let written = midas::sqlx::write_midas(&migrations, Path::new(out))?;
  println!(
    "Imported {} migration(s) into `{out}` ({} file(s)).",
    migrations.len(),
    written.len()
  );
  Ok(())
}

fn adopt(matches: &ArgMatches, context: &Context) -> Result<()> {
  let table = matches
    .get_one::<String>("table")
    .map(String::as_str)
    .unwrap_or("_sqlx_migrations");
  let report = midas::sqlx::verify_adoption(&context.config, table)?;

  println!(
    "SQLx metadata table `{}` is adoptable ({} applied migration(s)).",
    qualified_table_name(report.schema.as_deref(), &report.table),
    report.applied.len()
  );
  if report.has_down_checksum {
    println!("The table stores DOWN checksums.");
  } else {
    println!("No `down_checksum` column; DOWN checksums are validated from migration files on revert.");
  }
  println!("To keep using it, add to `midas.toml` [database]:");
  if let Some(schema) = &report.schema {
    println!("  metadata_schema = \"{schema}\"");
  }
  println!("  metadata_table = \"{}\"", report.table);
  Ok(())
}

fn resolve(matches: &ArgMatches, context: &Context) -> Result<()> {
  let applied = matches.get_flag("applied");
  let rolled_back = matches.get_flag("rolled-back");
  if applied == rolled_back {
    anyhow::bail!("pass exactly one of --applied or --rolled-back");
  }
  let version = matches.get_one::<i64>("migration_version").copied();
  if applied {
    with_migrator(context, false, |migrator| migrator.resolve_applied(version)).map(drop)
  } else {
    with_migrator(context, false, |migrator| migrator.resolve_rolled_back(version)).map(drop)
  }
}

fn output(matches: &ArgMatches, info: &MigrationInfo) -> Result<()> {
  if matches.get_flag("json") {
    println!("{}", serde_json::to_string(info)?);
  }
  Ok(())
}

pub(crate) fn with_migrator<F, T>(context: &Context, quiet: bool, action: F) -> Result<T>
where
  F: FnOnce(&mut Migrator) -> midas::Result<T>,
{
  context.config.require_database_url()?;
  let migrations = migration::discover(&context.config.migrations_dir)?;
  let backend = sequel::connect(&context.config)?;
  let options = migrator_options(context, quiet);
  let mut migrator = Migrator::new(backend, migrations).with_options(options);
  Ok(action(&mut migrator)?)
}

fn migrator_options(context: &Context, quiet: bool) -> MigratorOptions {
  MigratorOptions {
    dry_run: context.dry_run,
    no_lock: context.no_lock,
    transaction: context.config.transaction,
    quiet,
    ignore_missing: context.ignore_missing,
  }
}

fn project_template(config: &Config) -> String {
  let dir = config.migrations_dir.to_string_lossy();
  let dialect = configured_dialect(config);
  let (default_schema, metadata_schema) = match dialect {
    "sqlite" => ("default_schema = \"main\"\n".to_string(), String::new()),
    _ => (
      "default_schema = \"public\"\n".to_string(),
      "metadata_schema = \"midas\"\n".to_string(),
    ),
  };
  let managed_schema = if dialect == "sqlite" { "main" } else { "public" };

  format!(
    r#"# midas project configuration
[project]
dialect = "{dialect}"
{default_schema}
[database]
url_env = "DATABASE_URL"
{metadata_schema}metadata_table = "__schema_migrations"
shadow_url_env = "SHADOW_DATABASE_URL"

[migrations]
dir = "{dir}"
transaction = "per-migration"

[managed]
schemas = ["{managed_schema}"]
ignore_objects = []
"#
  )
}

fn configured_dialect(config: &Config) -> &'static str {
  config
    .database_url
    .as_deref()
    .and_then(|url| midas::sequel::DatabaseEngine::from_url(url).ok())
    .map(midas::sequel::DatabaseEngine::dialect_name)
    .unwrap_or("postgres")
}

fn schema_template() -> &'static str {
  "# midas schema DSL file.\n\
   # Define tables, enums, and other schema objects here.\n"
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn owners_are_explicit() {
    for name in [
      "init", "add", "create", "run", "up", "info", "status", "list", "revert", "redo", "baseline", "repair",
      "resolve", "reset", "down",
    ] {
      assert!(owns(name), "expected owner for `{name}`");
    }
    assert!(!owns("db"));
    assert!(!owns("schema"));
  }

  #[test]
  fn project_template_contains_migrations_dir() {
    let config = Config {
      migrations_dir: "custom_migrations".into(),
      ..Config::default()
    };
    let template = project_template(&config);
    assert!(template.contains("dir = \"custom_migrations\""));
    assert!(template.contains("metadata_table = \"__schema_migrations\""));
  }

  #[test]
  fn project_template_follows_the_configured_dialect() {
    let sqlite = Config {
      database_url: Some("sqlite://./app.db3".to_string()),
      ..Config::default()
    };
    let template = project_template(&sqlite);
    assert!(template.contains("dialect = \"sqlite\""));
    assert!(!template.contains("metadata_schema"));

    let cockroach = Config {
      database_url: Some("cockroachdb://root@localhost:26257/app".to_string()),
      ..Config::default()
    };
    let template = project_template(&cockroach);
    assert!(template.contains("dialect = \"postgres\""));
    assert!(template.contains("metadata_schema = \"midas\""));
  }

  #[test]
  fn schema_template_is_comment_only() {
    assert!(schema_template().lines().all(|line| line.starts_with('#')));
  }
}