vespertide-query 0.2.1

Converts migration actions into SQL statements with bind parameters
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use sea_query::{Alias, ForeignKey};
use vespertide_core::{ForeignKeyOrphanStrategy, ReferenceAction, TableConstraint};

use super::super::helpers::{quote_ident, to_sea_fk_action};
use super::super::types::{BuiltQuery, DatabaseBackend, RawSql};
use super::{QueryError, TableDef, rebuild_sqlite_table_with_added_constraint};

/// Render `ON DELETE <kw>` / `ON UPDATE <kw>` clause text, or empty
/// string when the action is `None`. Used by the F11 PG raw-SQL path.
fn render_fk_action_clause(prefix: &str, action: Option<&ReferenceAction>) -> String {
    action.map_or_else(String::new, |a| format!(" {prefix} {}", a.to_sql_keyword()))
}

/// Build the SQL for `AddConstraint(ForeignKey)` plus the F3 pre-cleanup
/// statement dictated by `orphan_strategy`.
///
/// The cleanup is always emitted; on a table with no orphan rows it is a
/// no-op (zero rows updated/deleted). Skipping it entirely would require
/// proving statically that orphans cannot exist - vespertide treats the
/// planner-side check (`find_fk_orphan_additions`) as advisory, not as a
/// proof of absence.
///
/// SQL pattern (PG/MySQL/SQLite uniform):
///
/// - **`NullifyOrphans`**: `UPDATE child SET <fk_cols> = NULL WHERE (<fk_col_i> IS NOT NULL OR ...) AND NOT EXISTS (SELECT 1 FROM parent WHERE <join>);`
/// - **`DeleteOrphans`**:  `DELETE FROM child WHERE NOT EXISTS (SELECT 1 FROM parent WHERE <join>);`
///
/// The `IS NOT NULL` guard on `NullifyOrphans` preserves rows whose
/// composite FK is wholly NULL (SQL standard treats such rows as
/// FK-exempt). Single-column FK collapses to a single `IS NOT NULL`.
#[expect(
    clippy::too_many_arguments,
    reason = "composite foreign-key builder mirrors FK action fields plus SQLite schema context plus the F3 orphan strategy; ForeignKeyContext is a deferred refactor"
)]
pub(super) fn build_foreign_key<T: AsRef<str>, U: AsRef<str>>(
    backend: DatabaseBackend,
    table: &str,
    name: Option<&str>,
    columns: &[T],
    ref_table: &str,
    ref_columns: &[U],
    on_delete: Option<&ReferenceAction>,
    on_update: Option<&ReferenceAction>,
    orphan_strategy: ForeignKeyOrphanStrategy,
    constraint: &TableConstraint,
    current_schema: &[TableDef],
    pending_constraints: &[TableConstraint],
) -> Result<Vec<BuiltQuery>, QueryError> {
    let cleanup = build_fk_orphan_cleanup(
        backend,
        table,
        columns,
        ref_table,
        ref_columns,
        orphan_strategy,
    )?;

    if backend == DatabaseBackend::Sqlite {
        let mut queries = cleanup;
        queries.extend(rebuild_sqlite_table_with_added_constraint(
            backend,
            table,
            constraint,
            current_schema,
            pending_constraints,
        )?);
        return Ok(queries);
    }

    let fk_name = vespertide_naming::build_foreign_key_name(table, columns, name);
    let mut queries = cleanup;

    if backend == DatabaseBackend::Postgres {
        // F11: PG NOT VALID + VALIDATE 2-step. Both statements run
        // inside the migration transaction so PG rollback reverts the
        // pair on failure - no partial-apply zombie. The sea-query
        // `ForeignKey` builder has no `NOT VALID` switch, so we emit
        // raw SQL here. MySQL still uses the builder below.
        let quoted_table = quote_ident(table, backend);
        let quoted_name = quote_ident(&fk_name, backend);
        let quoted_ref_table = quote_ident(ref_table, backend);
        let cols = columns
            .iter()
            .map(|c| quote_ident(c.as_ref(), backend))
            .collect::<Vec<_>>()
            .join(", ");
        let ref_cols = ref_columns
            .iter()
            .map(|c| quote_ident(c.as_ref(), backend))
            .collect::<Vec<_>>()
            .join(", ");
        let on_delete_clause = render_fk_action_clause("ON DELETE", on_delete);
        let on_update_clause = render_fk_action_clause("ON UPDATE", on_update);

        let add_not_valid = format!(
            "ALTER TABLE {quoted_table} ADD CONSTRAINT {quoted_name} \
             FOREIGN KEY ({cols}) REFERENCES {quoted_ref_table} ({ref_cols})\
             {on_delete_clause}{on_update_clause} NOT VALID"
        );
        let validate = format!("ALTER TABLE {quoted_table} VALIDATE CONSTRAINT {quoted_name}");
        queries.push(BuiltQuery::Raw(RawSql::uniform(add_not_valid)));
        queries.push(BuiltQuery::Raw(RawSql::uniform(validate)));
        return Ok(queries);
    }

    // MySQL: single statement via sea-query builder (no NOT VALID).
    let mut fk = ForeignKey::create();
    fk.name(&fk_name);
    fk.from_tbl(Alias::new(table));
    for col in columns {
        fk.from_col(Alias::new(col.as_ref()));
    }
    fk.to_tbl(Alias::new(ref_table));
    for col in ref_columns {
        fk.to_col(Alias::new(col.as_ref()));
    }
    if let Some(action) = on_delete {
        fk.on_delete(to_sea_fk_action(action));
    }
    if let Some(action) = on_update {
        fk.on_update(to_sea_fk_action(action));
    }

    queries.push(BuiltQuery::CreateForeignKey(Box::new(fk)));
    Ok(queries)
}

/// Emit the F3 pre-cleanup statement (UPDATE / DELETE) ahead of the
/// `ADD CONSTRAINT FOREIGN KEY`. Returns an empty `Vec` only when the
/// strategy variant is unrecognised (future-proofing for `non_exhaustive`).
fn build_fk_orphan_cleanup<T: AsRef<str>, U: AsRef<str>>(
    backend: DatabaseBackend,
    child_table: &str,
    child_columns: &[T],
    ref_table: &str,
    ref_columns: &[U],
    strategy: ForeignKeyOrphanStrategy,
) -> Result<Vec<BuiltQuery>, QueryError> {
    if child_columns.len() != ref_columns.len() {
        return Err(QueryError::SchemaError(format!(
            "FK on '{child_table}': child columns ({}) and ref columns ({}) length mismatch",
            child_columns.len(),
            ref_columns.len()
        )));
    }
    if child_columns.is_empty() {
        return Ok(vec![]);
    }

    let quoted_child = quote_ident(child_table, backend);
    let quoted_ref = quote_ident(ref_table, backend);

    // Correlated `NOT EXISTS` join condition: `parent.pk_i = child.fk_i AND ...`
    // Built with an explicit loop (not `.iter().map().collect()`) so each
    // statement maps to its own coverage region — LLVM attributes a method
    // chain's head line inconsistently.
    let mut join_terms: Vec<String> = Vec::with_capacity(child_columns.len());
    for (c, r) in child_columns.iter().zip(ref_columns.iter()) {
        let qc = quote_ident(c.as_ref(), backend);
        let qr = quote_ident(r.as_ref(), backend);
        join_terms.push(format!("{quoted_ref}.{qr} = {quoted_child}.{qc}"));
    }
    let join_cond = join_terms.join(" AND ");

    let not_exists = format!("NOT EXISTS (SELECT 1 FROM {quoted_ref} WHERE {join_cond})");

    let sql = if strategy == ForeignKeyOrphanStrategy::NullifyOrphans {
        // SET <col_i> = NULL, ... (explicit loop for deterministic coverage
        // attribution; see `join_terms` above).
        let mut set_terms: Vec<String> = Vec::with_capacity(child_columns.len());
        for c in child_columns {
            let qc = quote_ident(c.as_ref(), backend);
            set_terms.push(format!("{qc} = NULL"));
        }
        let set_clause = set_terms.join(", ");

        // NULL row guard: skip rows whose FK columns are all NULL.
        // Composite FK uses `<col_i> IS NOT NULL OR ...`; single-column FK collapses to one term.
        let null_guard: Vec<String> = child_columns
            .iter()
            .map(|c| format!("{} IS NOT NULL", quote_ident(c.as_ref(), backend)))
            .collect();
        let null_guard = null_guard.join(" OR ");

        format!("UPDATE {quoted_child} SET {set_clause} WHERE ({null_guard}) AND {not_exists}")
    } else {
        format!("DELETE FROM {quoted_child} WHERE {not_exists}")
    };

    Ok(vec![BuiltQuery::Raw(RawSql::uniform(sql))])
}

#[cfg(test)]
mod tests {
    use super::*;

    use rstest::rstest;
    use vespertide_core::{ColumnDef, ColumnType, MigrationAction, SimpleColumnType};

    fn nn_col(name: &str, ty: SimpleColumnType) -> ColumnDef {
        ColumnDef::new(name, ColumnType::Simple(ty), false)
    }

    fn fk_constraint(strategy: ForeignKeyOrphanStrategy) -> TableConstraint {
        TableConstraint::ForeignKey {
            name: Some("fk_user".into()),
            columns: vec!["user_id".into()],
            ref_table: "users".into(),
            ref_columns: vec!["id".into()],
            on_delete: None,
            on_update: None,
            orphan_strategy: strategy,
        }
    }

    fn parent_child_schema() -> Vec<TableDef> {
        vec![
            TableDef {
                name: "users".into(),
                description: None,
                columns: vec![nn_col("id", SimpleColumnType::Integer)],
                constraints: vec![],
            },
            TableDef {
                name: "posts".into(),
                description: None,
                columns: vec![
                    nn_col("id", SimpleColumnType::Integer),
                    ColumnDef::new(
                        "user_id",
                        ColumnType::Simple(SimpleColumnType::Integer),
                        true,
                    ),
                ],
                constraints: vec![],
            },
        ]
    }

    fn sqlite_schema_with(constraints: Vec<TableConstraint>) -> Vec<TableDef> {
        vec![TableDef {
            name: "posts".into(),
            description: None,
            columns: vec![ColumnDef::new(
                "user_id",
                ColumnType::Simple(SimpleColumnType::Integer),
                true,
            )],
            constraints,
        }]
    }

    #[test]
    fn sqlite_table_not_found_returns_error() {
        let constraint = fk_constraint(ForeignKeyOrphanStrategy::default());
        let result = build_foreign_key(
            DatabaseBackend::Sqlite,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            None,
            None,
            ForeignKeyOrphanStrategy::default(),
            &constraint,
            &[],
            &[],
        );
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Table 'posts' not found in current schema")
        );
    }

    #[test]
    fn sqlite_rebuild_preserves_check_constraints() {
        let constraint = fk_constraint(ForeignKeyOrphanStrategy::default());
        let schema = sqlite_schema_with(vec![TableConstraint::Check {
            name: "chk_user_id".into(),
            expr: "user_id > 0".into(),
            strategy: vespertide_core::CheckViolationStrategy::default(),
        }]);
        let queries = build_foreign_key(
            DatabaseBackend::Sqlite,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            None,
            None,
            ForeignKeyOrphanStrategy::default(),
            &constraint,
            &schema,
            &[],
        )
        .unwrap();
        let sql = queries
            .iter()
            .map(|q| q.build(DatabaseBackend::Sqlite))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(sql.contains("CONSTRAINT \"chk_user_id\" CHECK"));
    }

    #[test]
    fn sqlite_rebuild_recreates_indexes() {
        let constraint = fk_constraint(ForeignKeyOrphanStrategy::default());
        let schema = sqlite_schema_with(vec![TableConstraint::Index {
            name: Some("idx_user_id".into()),
            columns: vec!["user_id".into()],
        }]);
        let queries = build_foreign_key(
            DatabaseBackend::Sqlite,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            None,
            None,
            ForeignKeyOrphanStrategy::default(),
            &constraint,
            &schema,
            &[],
        )
        .unwrap();
        let sql = queries
            .iter()
            .map(|q| q.build(DatabaseBackend::Sqlite))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(sql.contains("CREATE INDEX"));
        assert!(sql.contains("idx_user_id"));
    }

    #[test]
    fn sqlite_rebuild_handles_unique_constraint() {
        let constraint = fk_constraint(ForeignKeyOrphanStrategy::default());
        let schema = sqlite_schema_with(vec![TableConstraint::Unique {
            name: Some("uq_user_id".into()),
            columns: vec!["user_id".into()],
            strategy: vespertide_core::UniqueConstraintStrategy::default(),
        }]);
        let queries = build_foreign_key(
            DatabaseBackend::Sqlite,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            None,
            None,
            ForeignKeyOrphanStrategy::default(),
            &constraint,
            &schema,
            &[],
        )
        .unwrap();
        assert!(
            queries
                .iter()
                .map(|q| q.build(DatabaseBackend::Sqlite))
                .collect::<Vec<_>>()
                .join("\n")
                .contains("CREATE TABLE")
        );
    }

    #[test]
    fn sqlite_rebuild_without_existing_check_adds_foreign_key() {
        let constraint = fk_constraint(ForeignKeyOrphanStrategy::default());
        let queries = build_foreign_key(
            DatabaseBackend::Sqlite,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            None,
            None,
            ForeignKeyOrphanStrategy::default(),
            &constraint,
            &sqlite_schema_with(vec![]),
            &[],
        )
        .unwrap();
        let sql = queries
            .iter()
            .map(|q| q.build(DatabaseBackend::Sqlite))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(sql.contains("CREATE TABLE"));
        assert!(sql.contains("FOREIGN KEY"));
    }

    #[rstest]
    #[case::postgres(DatabaseBackend::Postgres)]
    #[case::mysql(DatabaseBackend::MySql)]
    #[case::sqlite(DatabaseBackend::Sqlite)]
    fn mismatched_column_counts_return_schema_error(#[case] backend: DatabaseBackend) {
        let result = build_fk_orphan_cleanup(
            backend,
            "posts",
            &["user_id", "tenant_id"],
            "users",
            &["id"],
            ForeignKeyOrphanStrategy::DeleteOrphans,
        );
        assert!(matches!(result, Err(QueryError::SchemaError(_))));
    }

    #[test]
    fn postgres_emits_not_valid_validate_pair() {
        let constraint = TableConstraint::ForeignKey {
            name: Some("fk_user".into()),
            columns: vec!["user_id".into()],
            ref_table: "users".into(),
            ref_columns: vec!["id".into()],
            on_delete: Some(ReferenceAction::Cascade),
            on_update: Some(ReferenceAction::Restrict),
            orphan_strategy: ForeignKeyOrphanStrategy::NullifyOrphans,
        };
        let queries = build_foreign_key(
            DatabaseBackend::Postgres,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            Some(&ReferenceAction::Cascade),
            Some(&ReferenceAction::Restrict),
            ForeignKeyOrphanStrategy::NullifyOrphans,
            &constraint,
            &parent_child_schema(),
            &[],
        )
        .unwrap();
        let sql = queries
            .iter()
            .map(|q| q.build(DatabaseBackend::Postgres))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(sql.contains("NOT VALID"));
        assert!(sql.contains("VALIDATE CONSTRAINT"));
        assert!(sql.contains("ON DELETE CASCADE") && sql.contains("ON UPDATE RESTRICT"));
    }

    #[rstest]
    #[case::postgres(DatabaseBackend::Postgres)]
    #[case::mysql(DatabaseBackend::MySql)]
    #[case::sqlite(DatabaseBackend::Sqlite)]
    fn nullify_orphans_emits_update_set_null(#[case] backend: DatabaseBackend) {
        let queries = build_fk_orphan_cleanup(
            backend,
            "posts",
            &["user_id"],
            "users",
            &["id"],
            ForeignKeyOrphanStrategy::NullifyOrphans,
        )
        .unwrap();
        let sql = queries[0].build(backend);
        assert!(sql.contains("UPDATE"));
        assert!(sql.contains("= NULL"));
        assert!(sql.contains("IS NOT NULL"));
        assert!(sql.contains("NOT EXISTS"));
    }

    #[rstest]
    #[case::postgres(DatabaseBackend::Postgres)]
    #[case::mysql(DatabaseBackend::MySql)]
    #[case::sqlite(DatabaseBackend::Sqlite)]
    fn delete_orphans_emits_delete_from(#[case] backend: DatabaseBackend) {
        let queries = build_fk_orphan_cleanup(
            backend,
            "posts",
            &["user_id"],
            "users",
            &["id"],
            ForeignKeyOrphanStrategy::DeleteOrphans,
        )
        .unwrap();
        let sql = queries[0].build(backend);
        assert!(sql.contains("DELETE FROM"));
        assert!(sql.contains("NOT EXISTS"));
    }

    #[rstest]
    #[case::postgres(DatabaseBackend::Postgres)]
    #[case::mysql(DatabaseBackend::MySql)]
    #[case::sqlite(DatabaseBackend::Sqlite)]
    fn empty_columns_return_empty_cleanup(#[case] backend: DatabaseBackend) {
        assert!(
            build_fk_orphan_cleanup::<&str, &str>(
                backend,
                "posts",
                &[],
                "users",
                &[],
                ForeignKeyOrphanStrategy::DeleteOrphans
            )
            .unwrap()
            .is_empty()
        );
    }

    #[test]
    fn composite_nullify_orphans_pg_emits_composite_null_guard() {
        let queries = build_fk_orphan_cleanup(
            DatabaseBackend::Postgres,
            "posts",
            &["user_id", "tenant_id"],
            "users",
            &["id", "tenant_id"],
            ForeignKeyOrphanStrategy::NullifyOrphans,
        )
        .unwrap();
        let sql = queries[0].build(DatabaseBackend::Postgres);
        assert!(sql.contains("UPDATE \"posts\" SET"));
        assert!(sql.contains("\"user_id\" = NULL"));
        assert!(sql.contains("\"tenant_id\" = NULL"));
        assert!(sql.contains("\"user_id\" IS NOT NULL OR \"tenant_id\" IS NOT NULL"));
    }

    #[rstest]
    #[case::postgres(DatabaseBackend::Postgres)]
    #[case::mysql(DatabaseBackend::MySql)]
    #[case::sqlite(DatabaseBackend::Sqlite)]
    fn build_foreign_key_nullify_path_runs_cleanup_then_constraint(
        #[case] backend: DatabaseBackend,
    ) {
        let constraint = TableConstraint::ForeignKey {
            name: Some("fk_user".into()),
            columns: vec!["user_id".into()],
            ref_table: "users".into(),
            ref_columns: vec!["id".into()],
            on_delete: Some(ReferenceAction::Cascade),
            on_update: Some(ReferenceAction::Restrict),
            orphan_strategy: ForeignKeyOrphanStrategy::NullifyOrphans,
        };
        let queries = build_foreign_key(
            backend,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            Some(&ReferenceAction::Cascade),
            Some(&ReferenceAction::Restrict),
            ForeignKeyOrphanStrategy::NullifyOrphans,
            &constraint,
            &parent_child_schema(),
            &[],
        )
        .unwrap();
        let sql = queries
            .iter()
            .map(|q| q.build(backend))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(sql.contains("UPDATE"));
        assert!(sql.contains("= NULL"));
        assert!(sql.contains("NOT EXISTS"));
        assert!(
            sql.contains("FOREIGN KEY")
                || sql.contains("VALIDATE CONSTRAINT")
                || sql.contains("CREATE TABLE")
        );
    }

    #[rstest]
    #[case::postgres(DatabaseBackend::Postgres)]
    #[case::mysql(DatabaseBackend::MySql)]
    #[case::sqlite(DatabaseBackend::Sqlite)]
    fn add_constraint_foreign_key_emits_delete_and_update_actions(
        #[case] backend: DatabaseBackend,
    ) {
        let constraint = TableConstraint::ForeignKey {
            name: Some("fk_posts__user_id".into()),
            columns: vec!["user_id".into()],
            ref_table: "users".into(),
            ref_columns: vec!["id".into()],
            on_delete: Some(ReferenceAction::Cascade),
            on_update: Some(ReferenceAction::Cascade),
            orphan_strategy: ForeignKeyOrphanStrategy::NullifyOrphans,
        };
        let action = MigrationAction::AddConstraint {
            table: "posts".into(),
            constraint,
        };

        let sql = crate::sql::build_action_queries(backend, &action, &parent_child_schema())
            .unwrap()
            .iter()
            .map(|query| query.build(backend))
            .collect::<Vec<_>>()
            .join("\n");

        assert!(sql.contains("ON DELETE CASCADE"), "{sql}");
        assert!(sql.contains("ON UPDATE CASCADE"), "{sql}");
    }

    #[rstest]
    #[case::postgres(DatabaseBackend::Postgres)]
    #[case::mysql(DatabaseBackend::MySql)]
    #[case::sqlite(DatabaseBackend::Sqlite)]
    fn build_foreign_key_delete_path_runs_cleanup_then_constraint(
        #[case] backend: DatabaseBackend,
    ) {
        let constraint = TableConstraint::ForeignKey {
            name: Some("fk_user".into()),
            columns: vec!["user_id".into()],
            ref_table: "users".into(),
            ref_columns: vec!["id".into()],
            on_delete: None,
            on_update: None,
            orphan_strategy: ForeignKeyOrphanStrategy::DeleteOrphans,
        };
        let queries = build_foreign_key(
            backend,
            "posts",
            Some("fk_user"),
            &["user_id"],
            "users",
            &["id"],
            None,
            None,
            ForeignKeyOrphanStrategy::DeleteOrphans,
            &constraint,
            &parent_child_schema(),
            &[],
        )
        .unwrap();
        let sql = queries
            .iter()
            .map(|q| q.build(backend))
            .collect::<Vec<_>>()
            .join("\n");
        assert!(sql.contains("DELETE FROM"));
        assert!(sql.contains("NOT EXISTS"));
        assert!(
            sql.contains("FOREIGN KEY")
                || sql.contains("VALIDATE CONSTRAINT")
                || sql.contains("CREATE TABLE")
        );
    }
}