no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
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
use super::*;
use std::path::PathBuf;
#[path = "tests/append_aliases.rs"]
mod append_aliases;
#[path = "tests/append_breadth.rs"]
mod append_breadth;
#[path = "tests/append_decisions.rs"]
mod append_decisions;
#[path = "tests/append_sites.rs"]
mod append_sites;
#[path = "tests/branch_correlation.rs"]
mod branch_correlation;
#[path = "tests/branch_throw.rs"]
mod branch_throw;
#[path = "tests/captured_bindings.rs"]
mod captured_bindings;
#[path = "tests/captured_builder_identities.rs"]
mod captured_builder_identities;
#[path = "tests/correlated_finite_guards.rs"]
mod correlated_finite_guards;
#[path = "tests/defensive.rs"]
mod defensive;
#[path = "tests/destructuring_assignments.rs"]
mod destructuring_assignments;
#[path = "tests/equivalent.rs"]
mod equivalent;
#[path = "tests/escape_effects.rs"]
mod escape_effects;
#[path = "tests/exception_flow.rs"]
mod exception_flow;
#[path = "tests/executor_argument_effects.rs"]
mod executor_argument_effects;
#[path = "tests/final_assignment_consumers.rs"]
mod final_assignment_consumers;
#[path = "tests/final_interpolation.rs"]
mod final_interpolation;
#[path = "tests/final_rebinding.rs"]
mod final_rebinding;
#[path = "tests/finite_identifiers.rs"]
mod finite_identifiers;
#[path = "tests/folded_append_origins.rs"]
mod folded_append_origins;
#[path = "tests/guard_alias.rs"]
mod guard_alias;
#[path = "tests/guarded_sql.rs"]
mod guarded_sql;
#[path = "tests/helper_effects.rs"]
mod helper_effects;
#[path = "tests/helper_provenance.rs"]
mod helper_provenance;
#[path = "tests/helpers.rs"]
mod helpers;
#[path = "tests/metadata.rs"]
mod metadata;
#[path = "tests/opaque_logical.rs"]
mod opaque_logical;
#[path = "tests/positions.rs"]
mod positions;
#[path = "tests/singleton_mirror.rs"]
mod singleton_mirror;
#[path = "tests/static_append.rs"]
mod static_append;
#[path = "tests/switch_generators.rs"]
mod switch_generators;
#[path = "tests/switch_member_invocations.rs"]
mod switch_member_invocations;
#[path = "tests/switch_scalar_labels.rs"]
mod switch_scalar_labels;
#[path = "tests/unaffected_switch.rs"]
mod unaffected_switch;

fn file_facts(name: &str) -> EmbeddedSqlFileFacts {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../fixtures/postgres-facts/embedded")
        .join(name);
    let source = std::fs::read_to_string(&path).unwrap();
    let options = EmbeddedSqlOptions::configured("@example/db", &[]).with_trusted_sql_tags(&[
        TrustedSqlTag {
            module: "@example/db".into(),
            name: "sql".into(),
        },
    ]);
    extract_embedded_sql_from_source(&path, &source, &options)
}
fn calls(name: &str) -> Vec<EmbeddedSqlCall> {
    file_facts(name).calls
}
fn texts(call: &EmbeddedSqlCall) -> Vec<&str> {
    call.variants
        .iter()
        .map(|version| version.sql_text.as_str())
        .collect()
}

#[test]
fn executor_conditionals_keep_one_dynamic_call_with_complete_alternatives() {
    let calls = calls("variants-conditional.ts");
    assert_eq!(calls.len(), 2);
    assert_eq!(texts(&calls[0]), ["SELECT 1", "SELECT 2"]);
    assert_eq!(texts(&calls[1]), ["SELECT sql_placeholder_1", "SELECT 3"]);
    assert!(calls
        .iter()
        .all(|call| call.kind == EmbeddedSqlKind::Dynamic));
}
#[test]
fn logical_operators_respect_string_truthiness_nullishness_and_absent_fragments() {
    let calls = calls("variants-logical.ts");
    assert_eq!(texts(&calls[0]), ["SELECT 1", "SELECT 2"]);
    assert_eq!(texts(&calls[1]), ["SELECT 3", "SELECT 4"]);
    assert_eq!(texts(&calls[2]), ["SELECT 6"]);
    assert_eq!(texts(&calls[3]), ["SELECT 1 ", "SELECT 1 LIMIT 1"]);
}
#[test]
fn mutations_keep_taken_and_not_taken_paths() {
    let calls = calls("variants-if.ts");
    assert_eq!(
        texts(&calls[0]),
        ["SELECT id FROM users", "SELECT id FROM users LIMIT 1"]
    );
    assert_eq!(
        texts(&calls[1]),
        [
            "SELECT id FROM users LIMIT 2",
            "SELECT id FROM users LIMIT 3"
        ]
    );
    assert_eq!(
        texts(&calls[2]),
        ["SELECT id FROM users", "SELECT id FROM users LIMIT 4"]
    );
}
#[test]
fn switch_keeps_default_no_match_and_fallthrough_paths() {
    let calls = calls("variants-switch.ts");
    assert_eq!(
        texts(&calls[0]),
        ["SELECT 1 /* a */", "SELECT 1 /* b */", "SELECT 1 /* c */"]
    );
    let mut fall = texts(&calls[1]);
    fall.sort();
    assert_eq!(
        fall,
        ["SELECT 2", "SELECT 2 /* a */ /* b */", "SELECT 2 /* b */"]
    );
}
#[test]
fn branch_assignments_do_not_invent_cross_path_combinations() {
    let calls = calls("variants-bindings.ts");
    assert_eq!(texts(&calls[0]), ["SELECT 1", "SELECT 2"]);
    assert_eq!(texts(&calls[1]), ["SELECT 1", "SELECT 20"]);
}
#[test]
fn nested_fragments_raw_literals_and_literal_joins_are_spliced() {
    let calls = calls("variants-nested.ts");
    assert_eq!(
        texts(&calls[0]),
        [
            "SELECT id FROM users WHERE active LIMIT 1",
            "SELECT id FROM users WHERE deleted LIMIT 1"
        ]
    );
    assert_eq!(
        texts(&calls[1]),
        ["SELECT id FROM users WHERE active LIMIT 1"]
    );
    assert_eq!(
        texts(&calls[2]),
        ["SELECT id FROM users WHERE active AND enabled LIMIT 1"]
    );
    assert_eq!(
        texts(&calls[3]),
        [
            "SELECT id FROM users WHERE active LIMIT 1",
            "SELECT id FROM users sql_placeholder_1 LIMIT 1"
        ]
    );
}
#[test]
fn opaque_alternatives_loops_cross_file_helpers_depth_and_cap_fail_closed() {
    for name in ["variants-opaque.ts", "variants-cap.ts"] {
        let calls = calls(name);
        assert!(!calls.is_empty());
        for call in &calls {
            assert_eq!(call.kind, EmbeddedSqlKind::Dynamic, "{name}: {call:#?}");
            if name == "variants-opaque.ts" && call.line == 9 {
                // Loop appends retain zero/one samples plus an unknown tail.
                assert_eq!(texts(call), ["SELECT 1", "SELECT 1 LIMIT 1"]);
                assert!(call.variants_truncated());
                assert!(call.is_unanalyzable());
            } else {
                assert!(call.variants.is_empty(), "{name}: {call:#?}");
            }
        }
    }
    assert_eq!(MAX_EMBEDDED_SQL_VARIANTS, 16);
}
#[test]
fn nonbranching_calls_retain_their_legacy_fields_and_statement_projection_bytes() {
    let calls = calls("variants-single.ts");
    assert!(calls.iter().all(|call| call.variants.is_empty()));
    assert_eq!(calls[0].kind, EmbeddedSqlKind::Inline);
    assert_eq!(calls[1].kind, EmbeddedSqlKind::ImmutableLocal);
    assert_eq!(calls[2].kind, EmbeddedSqlKind::Dynamic);
    let mut snapshot = String::new();
    for call in &calls {
        let original = crate::extract_sql_statement_facts_for_embedded_call(call);
        let projected = crate::extract_sql_statement_facts_for_embedded_call(
            &call.statement_calls().next().unwrap(),
        );
        assert_eq!(
            format!("{original:?}").into_bytes(),
            format!("{projected:?}").into_bytes()
        );
        // Lock the legacy call projection with additive bounded-proof metadata included.
        snapshot.push_str(&format!(
            "{:?}\n{original:?}\n",
            (
                call.line,
                &call.callee,
                &call.sql_text,
                call.kind,
                call.declaration_line,
                &call.sql_source_positions,
                &call.recovered_placeholder_positions
            )
        ));
    }
    let baseline = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../fixtures/postgres-facts/embedded/variants-single-legacy.txt");
    assert_eq!(snapshot.as_bytes(), std::fs::read(baseline).unwrap());
}

#[test]
fn statically_skipped_arms_do_not_poison_recovery_or_invent_combinations() {
    let calls = calls("variants-truthiness.ts");
    for (call, expected) in calls.iter().take(8).zip([
        "SELECT 1",
        "SELECT 2",
        "SELECT 3",
        "SELECT 4",
        "SELECT 5 LIMIT 1",
        "SELECT 6 ",
        "SELECT 7",
        "SELECT 8",
    ]) {
        assert_eq!(texts(call), [expected]);
    }
    assert_eq!(texts(&calls[8]), ["SELECT 10;", "SELECT 9"]);
    assert_eq!(texts(&calls[9]), ["SELECT 11"]);
}

#[test]
fn exactly_sixteen_versions_are_retained_in_deterministic_order() {
    let calls = calls("variants-cap-boundary.ts");
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].variants.len(), MAX_EMBEDDED_SQL_VARIANTS);
    assert_eq!(
        texts(&calls[0]).first().copied(),
        Some("SELECT 1 /* a1 */ /* b1 */ /* c1 */ /* d1 */")
    );
    assert_eq!(
        texts(&calls[0]).last().copied(),
        Some("SELECT 1 /* a0 */ /* b0 */ /* c0 */ /* d0 */")
    );
    assert_eq!(calls, self::calls("variants-cap-boundary.ts"));
}

#[test]
fn switch_fallthrough_retains_correlation_between_bindings() {
    let calls = calls("variants-switch-correlated.ts");
    let mut actual = texts(&calls[0]);
    actual.sort();
    assert_eq!(actual, ["SELECT 2", "SELECT a2", "SELECT z9"]);
}

#[test]
fn aliases_use_the_binding_and_tag_visible_in_their_lexical_scope() {
    let calls = calls("variants-shadowing.ts");
    assert_eq!(calls.len(), 4);
    assert_eq!(
        texts(&calls[0]),
        ["SELECT id FROM users WHERE id = sql_placeholder_1"]
    );
    assert_eq!(calls[1].kind, EmbeddedSqlKind::Inline);
    assert_eq!(
        calls[1].sql_text.as_deref(),
        Some("SELECT id FROM users WHERE name = sql_placeholder_1")
    );
    assert!(calls[2].is_unanalyzable());
    assert_eq!(texts(&calls[3]), ["SELECT id FROM users WHERE active"]);
}

#[test]
fn unknown_paths_and_cross_function_or_repeated_mutations_remain_opaque() {
    let calls = calls("variants-opaque-scopes.ts");
    assert_eq!(calls.len(), 5);
    assert!(
        calls.iter().all(EmbeddedSqlCall::is_unanalyzable),
        "{calls:#?}"
    );
}

#[test]
fn assignment_rhs_call_observes_the_previous_binding_before_it_is_invalidated() {
    let calls = calls("variants-assignment-order.ts");
    assert_eq!(calls.len(), 2);
    assert_eq!(texts(&calls[0]), ["SELECT 1", "SELECT 2"]);
    assert!(calls[1].is_unanalyzable());
}

#[test]
fn literal_switches_select_only_the_reachable_case_and_its_fallthrough() {
    let calls = calls("variants-literal-switch.ts");
    assert_eq!(calls.len(), 5);
    for (call, expected) in calls.iter().zip([
        "SELECT 1",
        "SELECT defaultb",
        "SELECT number",
        "SELECT true",
        "SELECT 4",
    ]) {
        assert_eq!(texts(call), [expected]);
    }
}

#[test]
fn unsupported_abrupt_control_does_not_publish_unreachable_statement_versions() {
    let calls = calls("variants-abrupt.ts");
    assert_eq!(calls.len(), 3);
    assert!(
        calls.iter().all(EmbeddedSqlCall::is_unanalyzable),
        "{calls:#?}"
    );
}

#[test]
fn calls_inside_branches_see_only_the_paths_that_reach_their_physical_call_site() {
    let calls = calls("variants-branch-calls.ts");
    assert_eq!(calls.len(), 5);
    let mut before_append = texts(&calls[0]);
    before_append.sort();
    assert_eq!(before_append, ["SELECT 1", "SELECT 1 /* a */"]);
    let mut after_append = texts(&calls[1]);
    after_append.sort();
    assert_eq!(
        after_append,
        ["SELECT 1 /* a */ /* b */", "SELECT 1 /* b */"]
    );
    assert_eq!(texts(&calls[2]), ["SELECT 1"]);
    assert_eq!(texts(&calls[3]), ["SELECT 2"]);
    assert_eq!(texts(&calls[4]), ["SELECT 3"]);
}

#[test]
fn static_builder_calls_joins_and_fluent_branch_fragments_are_complete_versions() {
    let calls = calls("variants-builders.ts");
    assert_eq!(calls.len(), 9);
    assert_eq!(texts(&calls[0]), ["SELECT id FROM users "]);
    assert_eq!(
        texts(&calls[1]),
        ["SELECT id FROM users WHERE active AND enabled"]
    );
    assert_eq!(texts(&calls[2]), ["SELECT id FROM users WHERE active"]);
    assert_eq!(
        texts(&calls[3]),
        [
            // The off (else) arm precedes the enabled singleton.
            "SELECT id FROM users WHERE active AND verified",
            "SELECT id FROM users WHERE active AND enabled"
        ]
    );
    assert_eq!(
        texts(&calls[4]),
        [
            "SELECT id FROM users WHERE active AND enabled",
            "SELECT id FROM users WHERE active OR enabled"
        ]
    );
    assert_eq!(texts(&calls[5]), ["SELECT id FROM users WHERE active"]);
    assert_eq!(texts(&calls[6]), ["SELECT id FROM users WHERE active"]);
    assert_eq!(
        texts(&calls[7]),
        ["SELECT id FROM users WHERE active AND enabled"]
    );
    assert_eq!(
        texts(&calls[8]),
        ["SELECT id FROM users WHERE active AND enabled"]
    );
}

#[test]
fn nullable_alternatives_use_the_fallback_but_null_executor_arguments_stay_opaque() {
    let calls = calls("variants-nullish.ts");
    assert_eq!(calls.len(), 6);
    for (call, mut expected) in calls.iter().take(3).zip([
        vec!["SELECT 1", "SELECT 2"],
        vec!["SELECT sql_placeholder_1", "SELECT 3"],
        vec![
            "SELECT id FROM users WHERE active",
            "SELECT id FROM users WHERE deleted",
        ],
    ]) {
        let mut actual = texts(call);
        actual.sort();
        expected.sort();
        assert_eq!(actual, expected);
    }
    assert!(calls[3..].iter().all(EmbeddedSqlCall::is_unanalyzable));
}

#[test]
fn trusted_builder_conditions_are_truthy_even_when_their_sql_is_empty() {
    let calls = calls("variants-builder-truthiness.ts");
    assert_eq!(calls.len(), 4);
    for (call, expected) in calls.iter().zip([
        "SELECT 1 LIMIT 1",
        "SELECT 2 LIMIT 2",
        "SELECT 3 LIMIT 3",
        "SELECT 4 LIMIT 4",
    ]) {
        assert_eq!(texts(call), [expected]);
    }
}

#[test]
fn repeated_immutable_conditions_keep_expression_and_mutation_choices_correlated() {
    let calls = calls("variants-correlated-conditions.ts");
    assert_eq!(calls.len(), 5);
    for call in &calls[..2] {
        let mut actual = texts(call);
        actual.sort();
        assert_eq!(actual, ["SELECT 10", "SELECT 29"]);
    }
    let mut actual = texts(&calls[2]);
    actual.sort();
    assert_eq!(actual, ["SELECT 10", "SELECT 2"]);
    for call in &calls[3..] {
        let mut actual = texts(call);
        actual.sort();
        assert_eq!(actual, ["SELECT 19", "SELECT 20"]);
    }
}

#[test]
fn scalar_falsy_arms_are_binds_while_untaken_fragment_guards_are_absent() {
    let calls = calls("variants-falsy-bindings.ts");
    assert_eq!(calls.len(), 11);
    for (call, expected) in calls
        .iter()
        .take(3)
        .zip(["SELECT 1", "SELECT 2", "SELECT 3"])
    {
        assert_eq!(texts(call), ["SELECT sql_placeholder_1", expected]);
        assert_eq!(call.variants[0].recovered_placeholder_positions.len(), 1);
        assert!(call.variants[1].recovered_placeholder_positions.is_empty());
    }
    for (call, expected) in calls[3..6]
        .iter()
        .zip(["SELECT 4 ", "SELECT 5 ", "SELECT 6 "])
    {
        assert_eq!(texts(call), [expected]);
        assert!(call.variants[0].recovered_placeholder_positions.is_empty());
    }
    let mut optional = texts(&calls[6]);
    optional.sort();
    assert_eq!(optional, ["SELECT 7 ", "SELECT 7 LIMIT 1"]);
    assert!(calls[7..].iter().all(EmbeddedSqlCall::is_unanalyzable));
}

#[path = "tests/logical.rs"]
mod logical;

#[path = "tests/mutable_append_loops.rs"]
mod mutable_append_loops;

#[path = "tests/correlated_member_guards.rs"]
mod correlated_member_guards;

#[path = "tests/named_member_guards.rs"]
mod named_member_guards;

#[path = "tests/member_guard_edges.rs"]
mod member_guard_edges;

#[path = "tests/suspension_member_guards.rs"]
mod suspension_member_guards;

#[path = "tests/namespace_member_guards.rs"]
mod namespace_member_guards;

#[path = "tests/logical_member_controls.rs"]
mod logical_member_controls;