no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use crate::embedded_dml::embedded_call_facts_with_demand;
use crate::statements::{SqlFactSite, SqlLocationDemand, SqlStatementFileFacts};
use crate::{extract_embedded_sql_from_source, EmbeddedSqlKind, EmbeddedSqlOptions};
use no_mistakes_postgres_sql::statements::work;

#[test]
fn mapped_non_insert_conflict_requests_omit_work_and_preserve_requested_facts() {
    let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../fixtures/postgres-facts/embedded/conflict-only-mapped-demand-gate.ts");
    let source = std::fs::read_to_string(&path).unwrap();
    let embedded = extract_embedded_sql_from_source(
        &path,
        &source,
        &EmbeddedSqlOptions::configured("@example/db", &[]),
    );
    assert_eq!(embedded.calls.len(), 11);
    for (index, call) in embedded.calls.iter().enumerate() {
        assert!(
            call.variants.is_empty() && !call.sql_source_positions.is_empty(),
            "call {index}: {call:?}"
        );
    }
    assert_eq!(embedded.calls[7].kind, EmbeddedSqlKind::Dynamic);
    assert_eq!(embedded.calls[10].kind, EmbeddedSqlKind::Dynamic);
    work::begin();
    let narrow =
        embedded_call_facts_with_demand(&embedded, false, false, SqlLocationDemand::Conflicts);
    let narrow_work = work::finish();
    assert_eq!(narrow_work.statement_extractions, 3);
    assert_eq!(narrow_work.general_fact_collections, 0);
    assert_eq!(narrow_work.full_walks, 0);
    assert_eq!(narrow.len(), 3);
    assert_eq!(
        narrow
            .iter()
            .map(|facts| facts.variant_locations.as_ref().unwrap().call_index)
            .collect::<Vec<_>>(),
        [4, 5, 6]
    );
    assert_eq!(
        narrow[0]
            .variant_locations
            .as_ref()
            .unwrap()
            .conflicts
            .len(),
        1
    );
    // Keep actual conflict sites and quoted/comment false positives unchanged.
    let complete =
        embedded_call_facts_with_demand(&embedded, false, false, SqlLocationDemand::Full);
    let expected: Vec<_> = complete[4..7].iter().cloned().map(policy_only).collect();
    assert_eq!(narrow, expected);
    for (demand, bounds) in [
        (SqlLocationDemand::None, false),
        (SqlLocationDemand::Full, false),
        (SqlLocationDemand::Conflicts, true),
    ] {
        work::begin();
        let facts = embedded_call_facts_with_demand(&embedded, bounds, false, demand);
        let complete_work = work::finish();
        assert_eq!(complete_work.statement_extractions, 11);
        assert_eq!(complete_work.general_fact_collections, 11);
        assert_eq!(facts.len(), 11);
        assert!(facts[1].parse_failed && facts[3].parse_failed);
    }
}

fn policy_only(full: SqlStatementFileFacts) -> SqlStatementFileFacts {
    SqlStatementFileFacts {
        path: full.path,
        origin_line: full.origin_line,
        parse_failed: full.parse_failed,
        parse_failures: full.parse_failures,
        variant_locations: full.variant_locations.map(|mut locations| {
            locations.locking.clear();
            locations.bound_tables.clear();
            locations.positions.retain(|site, _| {
                matches!(
                    site,
                    SqlFactSite::Origin | SqlFactSite::Conflict(_) | SqlFactSite::ParseFailure(_)
                )
            });
            locations
        }),
        ..Default::default()
    }
}