midas-migrate-cli 0.8.1

Do painless migration 🦀
Documentation
//! Phase 10 CLI contract tests for `faker` and `setup`.

mod common;

use std::path::Path;

use assert_cmd::assert::Assert;
use assert_cmd::{
  cargo,
  Command,
};
use assert_fs::TempDir;
use predicates::prelude::*;

fn write_project(directory: &Path, seed: &str) {
  std::fs::write(
    directory.join("midas.toml"),
    r#"
[project]
dialect = "sqlite"
default_schema = "main"

[database]
url_env = "DATABASE_URL"

[migrations]
dir = "migrations"

[managed]
schemas = ["main"]
ignore_objects = []

[seed]
file = "seed.toml"
"#,
  )
  .expect("write midas.toml");
  common::write_migration(
    &directory.join("migrations"),
    "create_words",
    "create table words (id integer primary key, word text not null unique);",
    "drop table words;",
  );
  std::fs::write(directory.join("seed.toml"), seed).expect("write seed.toml");
}

fn create_database(directory: &Path, url: &str) {
  common::run_midas(
    &[
      "--database",
      url,
      "db",
      "create",
      "--from-migrations",
      "migrations",
    ],
    directory,
  )
  .success();
}

fn run_with_env(args: &[&str], cwd: &Path, envs: &[(&str, &str)]) -> Assert {
  let mut command = Command::new(cargo::cargo_bin!("midas"));
  command.args(args).current_dir(cwd);
  for key in common::ENV_KEYS {
    command.env_remove(key);
  }
  for (key, value) in envs {
    command.env(key, value);
  }
  command.assert()
}

fn seed_file() -> &'static str {
  r#"
version = 1
[[seed]]
table = "words"
count = 5
[seed.faker]
word = "word"
"#
}

#[test]
fn faker_help_lists_contract_flags() {
  let temp = TempDir::new().expect("temp");
  common::run_midas(&["faker", "--help"], temp.path())
    .success()
    .stdout(predicate::str::contains("--seed-file"))
    .stdout(predicate::str::contains("--table"))
    .stdout(predicate::str::contains("--rows"))
    .stdout(predicate::str::contains("--reset"))
    .stdout(predicate::str::contains("--json"))
    .stdout(predicate::str::contains("--allow-production"));
}

#[test]
fn setup_help_lists_contract_flags() {
  let temp = TempDir::new().expect("temp");
  common::run_midas(&["setup", "--help"], temp.path())
    .success()
    .stdout(predicate::str::contains("--from"))
    .stdout(predicate::str::contains("--no-seed"))
    .stdout(predicate::str::contains("--state"))
    .stdout(predicate::str::contains("--no-state"))
    .stdout(predicate::str::contains("--accept-data-loss"));
}

#[test]
fn faker_requires_a_database_url() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  common::run_midas(&["faker"], temp.path())
    .failure()
    .stderr(predicate::str::contains("no database URL"));
}

#[test]
fn faker_requires_a_seed_file() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  std::fs::remove_file(temp.path().join("seed.toml")).expect("remove seed");
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);
  common::run_midas(&["--database", &url, "faker"], temp.path())
    .failure()
    .stderr(predicate::str::contains("does not exist"));
}

#[test]
fn faker_rejects_unknown_table_filters() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);
  common::run_midas(&["--database", &url, "faker", "--table", "missing"], temp.path())
    .failure()
    .stderr(predicate::str::contains("unknown table"));
}

#[test]
fn faker_rows_require_generated_entries() {
  let temp = TempDir::new().expect("temp");
  write_project(
    temp.path(),
    r#"
version = 1
[[seed]]
table = "words"
rows = [{ id = 1, word = "hello" }]
"#,
  );
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);
  common::run_midas(&["--database", &url, "faker", "--rows", "10"], temp.path())
    .failure()
    .stderr(predicate::str::contains("--rows requires"));
}

#[test]
fn reset_requires_confirmation() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);
  common::run_midas(&["--database", &url, "faker", "--reset"], temp.path())
    .failure()
    .stderr(predicate::str::contains("--reset requires --yes"));
}

#[test]
fn production_guard_blocks_unless_allowed_or_dry() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);

  run_with_env(
    &["--database", &url, "faker", "--json"],
    temp.path(),
    &[("APP_ENV", "production")],
  )
  .failure()
  .stdout(predicate::str::contains("\"error\""))
  .stderr(predicate::str::contains("refusing to seed"));

  run_with_env(
    &["--database", &url, "faker", "--allow-production", "--json"],
    temp.path(),
    &[("APP_ENV", "production")],
  )
  .success();

  run_with_env(
    &["--database", &url, "faker", "--dry-run", "--json"],
    temp.path(),
    &[("APP_ENV", "production")],
  )
  .success()
  .stdout(predicate::str::contains("\"dry_run\":true"));
}

#[test]
fn dry_run_json_shape_is_stable_and_writes_nothing() {
  let temp = TempDir::new().expect("temp");
  write_project(
    temp.path(),
    r#"
version = 1
[[seed]]
table = "words"
rows = [{ id = 1, word = "hello" }, { id = 2, word = "world" }]
"#,
  );
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);

  // A dry run must not insert; a later real run would fail on duplicate keys
  // if it had written rows.
  common::run_midas(&["--database", &url, "faker", "--dry-run", "--json"], temp.path())
    .success()
    .stdout(predicate::str::contains("\"command\":\"faker\""))
    .stdout(predicate::str::contains("\"seed\":1"))
    .stdout(predicate::str::contains("\"dry_run\":true"))
    .stdout(predicate::str::contains("\"tables\""))
    .stdout(predicate::str::contains("\"reset\""))
    .stdout(predicate::str::contains("\"skipped\""));

  common::run_midas(&["--database", &url, "faker", "--json"], temp.path())
    .success()
    .stdout(predicate::str::contains("\"rows\":2"));
}

#[test]
fn setup_is_idempotent_and_resets_on_request() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();

  common::run_midas(
    &["--database", &url, "setup", "--from", "migrations", "--json"],
    temp.path(),
  )
  .success()
  .stdout(predicate::str::contains("\"rows\":5"));

  common::run_midas(
    &["--database", &url, "setup", "--from", "migrations", "--json"],
    temp.path(),
  )
  .success()
  .stdout(predicate::str::contains("\"skipped\""));

  common::run_midas(
    &[
      "--database",
      &url,
      "setup",
      "--from",
      "migrations",
      "--reset",
      "--yes",
      "--json",
    ],
    temp.path(),
  )
  .success()
  .stdout(predicate::str::contains("\"rows\":5"));
}

#[test]
fn setup_rejects_state_flag_conflicts() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);

  common::run_midas(
    &[
      "--database",
      &url,
      "setup",
      "--from",
      "migrations",
      "--state",
      "state.json",
    ],
    temp.path(),
  )
  .failure()
  .stderr(predicate::str::contains("--state cannot be combined"));

  common::run_midas(
    &[
      "--database",
      &url,
      "setup",
      "--from",
      "migrations",
      "--state",
      "state.json",
      "--no-state",
    ],
    temp.path(),
  )
  .failure()
  .stderr(predicate::str::contains("mutually exclusive"));
}

#[test]
fn setup_schema_source_is_postgres_only() {
  let temp = TempDir::new().expect("temp");
  write_project(temp.path(), seed_file());
  std::fs::write(temp.path().join("schema.toml"), "version = 1\n").expect("schema");
  let url = temp.path().join("words.db3");
  let url = url.to_string_lossy().into_owned();
  create_database(temp.path(), &url);
  common::run_midas(
    &[
      "--database",
      &url,
      "setup",
      "--from",
      "schema",
      "--schema",
      "schema.toml",
    ],
    temp.path(),
  )
  .failure()
  .stderr(predicate::str::contains("unsupported"));
}