midas-migrate-cli 0.8.2

Do painless migration 🦀
Documentation
//! Phase 4 schema-driven migration CLI tests.

mod common;

use std::fs;
use std::path::{
  Path,
  PathBuf,
};

fn write_schema(dir: &Path, body: &str) -> PathBuf {
  let path = dir.join("schema.toml");
  let contents = format!("version = 1\n[project]\ndefault_schema = \"\"\n{body}");
  fs::write(&path, contents).expect("write schema");
  path
}

fn migration_files(dir: &Path) -> Vec<PathBuf> {
  let Ok(entries) = fs::read_dir(dir) else {
    return Vec::new();
  };
  let mut paths = entries
    .filter_map(|entry| entry.ok())
    .map(|entry| entry.path())
    .filter(|path| {
      path
        .file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| name.ends_with(".sql"))
    })
    .collect::<Vec<_>>();
  paths.sort();
  paths
}

fn database(temp: &assert_fs::TempDir) -> String {
  temp.path().join("app.db3").to_string_lossy().into_owned()
}

#[test]
fn first_dev_creates_a_migration_and_second_reports_no_changes() {
  let temp = assert_fs::TempDir::new().expect("temp dir");
  write_schema(
    temp.path(),
    "[[table]]\nname = \"widget\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n",
  );
  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "migrate",
      "dev",
      "--name",
      "init",
    ],
    temp.path(),
  )
  .success()
  .stdout(predicates::str::contains("Created migration"));

  let migrations = migration_files(&temp.path().join("migrations"));
  assert_eq!(migrations.len(), 1);
  let content = fs::read_to_string(&migrations[0]).expect("read migration");
  assert!(content.contains("-- !UP"), "{content}");
  assert!(content.contains("create table \"widget\""), "{content}");
  assert!(temp.path().join(".midas/state.json").is_file());

  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "migrate",
      "dev",
    ],
    temp.path(),
  )
  .success()
  .stdout(predicates::str::contains("No schema changes"));
  assert_eq!(migration_files(&temp.path().join("migrations")).len(), 1);
}

#[test]
fn destructive_changes_require_accept_data_loss() {
  let temp = assert_fs::TempDir::new().expect("temp dir");
  write_schema(
    temp.path(),
    "[[table]]\nname = \"widget\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n[[table.column]]\nname = \"name\"\ntype = \"text\"\n",
  );
  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "migrate",
      "dev",
      "--name",
      "init",
    ],
    temp.path(),
  )
  .success();

  write_schema(
    temp.path(),
    "[[table]]\nname = \"widget\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n",
  );
  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "migrate",
      "dev",
      "--name",
      "drop_name",
    ],
    temp.path(),
  )
  .failure()
  .stderr(predicates::str::contains("refusing destructive"));
  assert_eq!(migration_files(&temp.path().join("migrations")).len(), 1);

  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "migrate",
      "dev",
      "--name",
      "drop_name",
      "--accept-data-loss",
    ],
    temp.path(),
  )
  .success();
  assert_eq!(migration_files(&temp.path().join("migrations")).len(), 2);
}

#[test]
fn dry_run_writes_no_files_and_executes_no_sql() {
  let temp = assert_fs::TempDir::new().expect("temp dir");
  write_schema(
    temp.path(),
    "[[table]]\nname = \"widget\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n",
  );
  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "migrate",
      "dev",
      "--name",
      "init",
    ],
    temp.path(),
  )
  .success();

  write_schema(
    temp.path(),
    "[[table]]\nname = \"widget\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n[[table]]\nname = \"extra\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n",
  );
  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "--dry-run",
      "migrate",
      "dev",
    ],
    temp.path(),
  )
  .success()
  .stdout(predicates::str::contains("create table \"extra\""));
  assert_eq!(migration_files(&temp.path().join("migrations")).len(), 1);
}

#[test]
fn diff_script_prints_up_and_down() {
  let temp = assert_fs::TempDir::new().expect("temp dir");
  let from = temp.path().join("from.toml");
  let to = temp.path().join("to.toml");
  fs::write(&from, "version = 1\n").expect("write from");
  fs::write(
    &to,
    "version = 1\n[[table]]\nname = \"widget\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n",
  )
  .expect("write to");

  common::run_midas(
    &[
      "migrate",
      "diff",
      "--from",
      &from.to_string_lossy(),
      "--to",
      &to.to_string_lossy(),
      "--script",
    ],
    temp.path(),
  )
  .success()
  .stdout(predicates::str::contains("-- !UP"))
  .stdout(predicates::str::contains("-- !DOWN"))
  .stdout(predicates::str::contains("create table \"public\".\"widget\""));
}

#[test]
fn corrupt_state_is_rejected() {
  let temp = assert_fs::TempDir::new().expect("temp dir");
  write_schema(
    temp.path(),
    "[[table]]\nname = \"widget\"\n[[table.column]]\nname = \"id\"\ntype = \"bigint\"\n",
  );
  fs::create_dir_all(temp.path().join(".midas")).expect("state dir");
  fs::write(temp.path().join(".midas/state.json"), "{ nope").expect("write corrupt state");
  common::run_midas(
    &[
      "--database",
      &database(&temp),
      "--source",
      "migrations",
      "migrate",
      "dev",
    ],
    temp.path(),
  )
  .failure()
  .stderr(predicates::str::contains("corrupt schema state"));
}