capy-cli 0.10.0

The command-line interface for the Capy web framework.
Documentation
//! Additive resource evolution never rewrites customized application feature code.
use capy_cli::{
    make_auth, make_resource, new_project_with_frontend, resource_add_field, resource_add_index,
    FrontendDriver,
};
use std::{
    collections::BTreeMap,
    fs,
    path::{Path, PathBuf},
};

fn snapshot(root: &Path) -> BTreeMap<PathBuf, Vec<u8>> {
    fn visit(root: &Path, path: &Path, files: &mut BTreeMap<PathBuf, Vec<u8>>) {
        for entry in fs::read_dir(path).unwrap() {
            let path = entry.unwrap().path();
            if path.is_dir() {
                visit(root, &path, files);
            } else {
                files.insert(
                    path.strip_prefix(root).unwrap().into(),
                    fs::read(&path).unwrap(),
                );
            }
        }
    }
    let mut files = BTreeMap::new();
    visit(root, root, &mut files);
    files
}

#[test]
fn additive_evolution_preserves_custom_code_and_registers_reviewable_migrations() {
    let temp = tempfile::tempdir().unwrap();
    let root = temp.path().join("blog");
    new_project_with_frontend(&root, "blog", FrontendDriver::None).unwrap();
    make_auth(&root).unwrap();
    make_resource(&root, "Post", &["title:string".into()], false).unwrap();
    make_resource(&root, "Tag", &["name:string".into()], false).unwrap();
    let controller = root.join("app/controllers/post_controller.rs");
    let customized = format!(
        "{}\n// keep this application code\n",
        fs::read_to_string(&controller).unwrap()
    );
    fs::write(&controller, &customized).unwrap();

    for (field, expected) in [
        ("excerpt:text?", "m_add_excerpt_to_posts"),
        ("cover_key:string?", "m_add_cover_key_to_posts"),
    ] {
        resource_add_field(&root, "Post", field, false).unwrap();
        assert!(root
            .join(format!("database/migrations/{expected}.rs"))
            .exists());
        assert!(root
            .join(format!("database/evolution/{expected}.md"))
            .exists());
    }
    resource_add_field(
        &root,
        "Tag",
        "post_id:references?:Post:on_delete_set_null",
        false,
    )
    .unwrap();
    let migration =
        fs::read_to_string(root.join("database/migrations/m_add_post_id_to_tags.rs")).unwrap();
    assert!(migration.contains("REFERENCES"));
    assert!(migration.contains("SET NULL"));
    assert!(migration.contains("idx_tags_post_id"));
    let note =
        fs::read_to_string(root.join("database/evolution/m_add_post_id_to_tags.md")).unwrap();
    assert!(note.contains("app/models/tag.rs"));
    assert!(note.contains("belongs_to"));
    assert_eq!(fs::read_to_string(&controller).unwrap(), customized);
    assert!(!fs::read_to_string(root.join("app/models/post.rs"))
        .unwrap()
        .contains("excerpt"));
    let registry = fs::read_to_string(root.join("database/migrations/mod.rs")).unwrap();
    assert!(registry.contains("m_add_excerpt_to_posts"));
    assert!(registry.contains("m_add_post_id_to_tags"));

    let before = snapshot(&root);
    assert!(resource_add_field(&root, "Post", "excerpt:text?", false).is_err());
    assert_eq!(snapshot(&root), before);
    assert!(resource_add_field(&root, "Post", "required:text", false)
        .unwrap_err()
        .to_string()
        .contains("backfill"));
    assert_eq!(snapshot(&root), before);
    assert!(
        resource_add_field(&root, "Post", "code:string?:unique", false)
            .unwrap_err()
            .to_string()
            .contains("--acknowledge-unique")
    );
    assert_eq!(snapshot(&root), before);
    resource_add_field(&root, "Post", "code:string?:unique", true).unwrap();
    let unique =
        fs::read_to_string(root.join("database/migrations/m_add_code_to_posts.rs")).unwrap();
    assert!(unique.contains(".unique()"));

    let before_index = snapshot(&root);
    assert!(
        resource_add_index(&root, "Post", "owner_id,title", true, false)
            .unwrap_err()
            .to_string()
            .contains("--acknowledge-unique")
    );
    assert_eq!(snapshot(&root), before_index);
    assert!(resource_add_index(&root, "Post", "missing,title", false, false).is_err());
    assert_eq!(snapshot(&root), before_index);
    resource_add_index(&root, "Post", "owner_id,title", true, true).unwrap();
    let scoped =
        fs::read_to_string(root.join("database/migrations/m_unique_posts_owner_id_title.rs"))
            .unwrap();
    assert!(scoped.contains(".col(Alias::new(\"owner_id\"))"));
    assert!(scoped.contains(".col(Alias::new(\"title\"))"));
    assert!(scoped.contains(".unique()"));
    let before_repeat = snapshot(&root);
    assert!(resource_add_index(&root, "Post", "owner_id,title", true, true).is_err());
    assert_eq!(snapshot(&root), before_repeat);
}