@@ -404,16 +404,145 @@ describe('a plain non-cascade delete is unchanged (#7413 control)', () => {
404404 expect ( rec . at ( 'warn' ) ) . toHaveLength ( 0 ) ;
405405 } ) ;
406406
407- it ( 'opens no transaction when the object HAS dependents declared but the delete is by predicate' , async ( ) => {
408- // The predicate branch never called `cascadeDeleteRelations` and still does
409- // not — the wrap is the by-id branch's alone.
410- const { engine, driver } = await makeEngine ( [ parent , kid ] ) ;
411- await engine . insert ( 'parent' , { name : 'P' } ) ;
407+ it ( 'opens no transaction for a predicate delete when no relation points at the object' , async ( ) => {
408+ // [#26871] The predicate twin of the control above: nothing to walk, so the
409+ // predicate arm keeps its single `deleteMany` — no id resolution, no wrap.
410+ const { engine, driver, rec } = await makeEngine ( [ loner ] ) ;
411+ await engine . insert ( 'loner' , { name : 'solo' } ) ;
412+ await engine . insert ( 'loner' , { name : 'solo' } ) ;
412413 driver . writes . length = 0 ;
413414
414- await engine . delete ( 'parent ' , { where : { name : 'P ' } , multi : true } as any ) ;
415+ const removed = await engine . delete ( 'loner ' , { where : { name : 'solo ' } , multi : true } as any ) ;
415416
417+ expect ( removed ) . toBe ( 2 ) ;
416418 expect ( driver . begun ) . toHaveLength ( 0 ) ;
419+ expect ( driver . rowsOf ( 'loner' ) ) . toHaveLength ( 0 ) ;
420+ for ( const w of driver . writes ) expect ( w . transaction ) . toBeUndefined ( ) ;
421+ expect ( rec . at ( 'warn' ) ) . toHaveLength ( 0 ) ;
422+ } ) ;
423+ } ) ;
424+
425+ // ---------------------------------------------------------------------------
426+ // 3b. [#26871] A predicate delete walks every matched row, as one unit of work.
427+ // ---------------------------------------------------------------------------
428+
429+ /** A `master_detail` detail of `parent`: the default behaviour is `cascade`. */
430+ const detail = {
431+ name : 'detail' ,
432+ label : 'Detail' ,
433+ fields : {
434+ id : { name : 'id' , type : 'text' as const , primaryKey : true } ,
435+ parent : { name : 'parent' , type : 'master_detail' as const , reference : 'parent' } ,
436+ } ,
437+ } ;
438+ /** An authored `restrict` lookup to `parent`. */
439+ const guard = {
440+ name : 'guard' ,
441+ label : 'Guard' ,
442+ fields : {
443+ id : { name : 'id' , type : 'text' as const , primaryKey : true } ,
444+ parent : { name : 'parent' , type : 'lookup' as const , reference : 'parent' , deleteBehavior : 'restrict' } ,
445+ } ,
446+ } ;
447+
448+ describe ( '[#26871] a predicate delete runs the referential walk for every matched row' , ( ) => {
449+ it ( 'a restrict on one matched row refuses the whole delete, and nothing is deleted' , async ( ) => {
450+ const { engine, driver } = await makeEngine ( [ parent , detail , guard ] ) ;
451+ const p1 = await engine . insert ( 'parent' , { name : 'doomed' } ) ;
452+ const p2 = await engine . insert ( 'parent' , { name : 'doomed' } ) ;
453+ await engine . insert ( 'parent' , { name : 'kept' } ) ;
454+ await engine . insert ( 'detail' , { parent : p1 . id } ) ;
455+ await engine . insert ( 'guard' , { parent : p2 . id } ) ;
456+
457+ await expect ( engine . delete ( 'parent' , { where : { name : 'doomed' } , multi : true } as any ) )
458+ . rejects . toMatchObject ( { code : 'DELETE_RESTRICTED' , status : 409 , object : 'parent' , dependentObject : 'guard' } ) ;
459+
460+ // p1's detail was cascaded before p2's restrict refused; the one unit of
461+ // work puts it back, so the 409 means nothing changed.
462+ expect ( driver . rowsOf ( 'parent' ) ) . toHaveLength ( 3 ) ;
463+ expect ( driver . rowsOf ( 'detail' ) ) . toHaveLength ( 1 ) ;
464+ expect ( driver . rowsOf ( 'guard' ) ) . toHaveLength ( 1 ) ;
465+ expect ( driver . begun ) . toHaveLength ( 1 ) ;
466+ expect ( driver . rolledBack ) . toHaveLength ( 1 ) ;
467+ expect ( driver . committed ) . toHaveLength ( 0 ) ;
468+ } ) ;
469+
470+ it ( 'cascades master_detail children and clears set_null references of every matched row' , async ( ) => {
471+ const { engine, driver } = await makeEngine ( [ parent , detail , link ] ) ;
472+ const p1 = await engine . insert ( 'parent' , { name : 'doomed' } ) ;
473+ const p2 = await engine . insert ( 'parent' , { name : 'doomed' } ) ;
474+ const kept = await engine . insert ( 'parent' , { name : 'kept' } ) ;
475+ await engine . insert ( 'detail' , { parent : p1 . id } ) ;
476+ await engine . insert ( 'detail' , { parent : p2 . id } ) ;
477+ const keptDetail = await engine . insert ( 'detail' , { parent : kept . id } ) ;
478+ const l1 = await engine . insert ( 'link' , { parent : p1 . id } ) ;
479+ const l2 = await engine . insert ( 'link' , { parent : p2 . id } ) ;
480+ const keptLink = await engine . insert ( 'link' , { parent : kept . id } ) ;
481+
482+ const removed = await engine . delete ( 'parent' , { where : { name : 'doomed' } , multi : true } as any ) ;
483+
484+ expect ( removed ) . toBe ( 2 ) ;
485+ expect ( driver . rowsOf ( 'parent' ) . map ( ( r : any ) => r . id ) ) . toEqual ( [ kept . id ] ) ;
486+ expect ( driver . rowsOf ( 'detail' ) . map ( ( r : any ) => r . id ) ) . toEqual ( [ keptDetail . id ] ) ;
487+ const links = new Map ( driver . rowsOf ( 'link' ) . map ( ( r : any ) => [ r . id , r . parent ] ) ) ;
488+ expect ( links . get ( l1 . id ) ) . toBeNull ( ) ;
489+ expect ( links . get ( l2 . id ) ) . toBeNull ( ) ;
490+ expect ( links . get ( keptLink . id ) ) . toBe ( kept . id ) ;
491+ expect ( driver . begun ) . toHaveLength ( 1 ) ;
492+ expect ( driver . committed ) . toHaveLength ( 1 ) ;
493+ } ) ;
494+
495+ it ( 'routes the walk and the predicate delete onto ONE transaction handle' , async ( ) => {
496+ const { engine, driver } = await makeEngine ( [ parent , detail , link ] ) ;
497+ const p = await engine . insert ( 'parent' , { name : 'doomed' } ) ;
498+ await engine . insert ( 'detail' , { parent : p . id } ) ;
499+ await engine . insert ( 'link' , { parent : p . id } ) ;
500+ driver . writes . length = 0 ;
501+
502+ await engine . delete ( 'parent' , { where : { name : 'doomed' } , multi : true } as any ) ;
503+
504+ expect ( driver . begun ) . toHaveLength ( 1 ) ;
505+ const handle = driver . begun [ 0 ] ;
506+ expect ( driver . writes . some ( ( w : any ) => w . object === 'parent' && w . op === 'delete' ) ) . toBe ( true ) ;
507+ expect ( driver . writes . some ( ( w : any ) => w . object === 'detail' && w . op === 'delete' ) ) . toBe ( true ) ;
508+ expect ( driver . writes . some ( ( w : any ) => w . object === 'link' && w . op === 'update' ) ) . toBe ( true ) ;
509+ for ( const w of driver . writes ) expect ( w . transaction ) . toBe ( handle ) ;
510+ } ) ;
511+
512+ it ( 'deletes only the rows it walked, even when the walk makes another row match the predicate' , async ( ) => {
513+ // A self-referencing set_null lookup: clearing `boss` on the report makes
514+ // it match `{ boss: null }`. The delete removes the rows it resolved and
515+ // walked, never a row the walk itself brought into the predicate.
516+ const node = {
517+ name : 'node' ,
518+ label : 'Node' ,
519+ fields : {
520+ id : { name : 'id' , type : 'text' as const , primaryKey : true } ,
521+ name : { name : 'name' , type : 'text' as const } ,
522+ boss : { name : 'boss' , type : 'lookup' as const , reference : 'node' } ,
523+ } ,
524+ } ;
525+ const { engine, driver } = await makeEngine ( [ node ] ) ;
526+ const root = await engine . insert ( 'node' , { name : 'root' , boss : null } ) ;
527+ const report = await engine . insert ( 'node' , { name : 'report' , boss : root . id } ) ;
528+
529+ const removed = await engine . delete ( 'node' , { where : { boss : null } , multi : true } as any ) ;
530+
531+ expect ( removed ) . toBe ( 1 ) ;
532+ expect ( driver . rowsOf ( 'node' ) . map ( ( r : any ) => [ r . id , r . boss ?? null ] ) ) . toEqual ( [ [ report . id , null ] ] ) ;
533+ } ) ;
534+
535+ it ( 'a matched set of zero rows walks nothing and deletes nothing' , async ( ) => {
536+ const { engine, driver } = await makeEngine ( [ parent , detail ] ) ;
537+ const p = await engine . insert ( 'parent' , { name : 'kept' } ) ;
538+ await engine . insert ( 'detail' , { parent : p . id } ) ;
539+ driver . writes . length = 0 ;
540+
541+ const removed = await engine . delete ( 'parent' , { where : { name : 'nobody' } , multi : true } as any ) ;
542+
543+ expect ( removed ) . toBe ( 0 ) ;
544+ expect ( driver . writes ) . toEqual ( [ ] ) ;
545+ expect ( driver . rowsOf ( 'detail' ) ) . toHaveLength ( 1 ) ;
417546 } ) ;
418547} ) ;
419548
0 commit comments