2121 * before, once storage confirms no row of that column still names it and
2222 * the handle is this column's own. A handle a predicate write shared across
2323 * rows outlives every row but the last.
24+ * - **Released when the record goes** ([#27581]). A committed delete, by id,
25+ * by predicate or by cascade, releases every handle the removed rows held,
26+ * through the same release and its same checks; a delete that is refused or
27+ * rolled back keeps the row.
2428 */
2529
2630import { describe , it , expect , beforeEach } from 'vitest' ;
@@ -32,7 +36,13 @@ import { SECRET_MASK, isSecretRef, parseSecretRef } from './secret-fields.js';
3236
3337type Row = Record < string , unknown > ;
3438
35- /** A stub driver whose stores can be counted, rolled back and told to refuse a statement. */
39+ /**
40+ * A stub driver whose stores can be counted, rolled back and told to refuse a statement.
41+ *
42+ * A rollback restores the snapshot the unit began with, unless `isolation.on`: then it undoes
43+ * only the writes made under that unit's handle, as a database does, so a write that escaped
44+ * the unit survives the rollback ([#27581]).
45+ */
3646function makeDriver ( ) {
3747 let stores = new Map < string , Map < string , Row > > ( ) ;
3848 const storeFor = ( o : string ) => stores . get ( o ) ?? ( stores . set ( o , new Map ( ) ) , stores . get ( o ) ! ) ;
@@ -48,6 +58,10 @@ function makeDriver() {
4858 /** Objects whose `count` throws, so a release cannot confirm anything. */
4959 const countFails = new Set < string > ( ) ;
5060 const snapshots = new Map < unknown , typeof stores > ( ) ;
61+ const isolation = { on : false } ;
62+ const undoLogs = new Map < unknown , Array < ( ) => void > > ( ) ;
63+ /** Record how to take back one write, when it was made under an open unit's handle. */
64+ const undoable = ( opts : any , take : ( ) => void ) => { undoLogs . get ( opts ?. transaction ) ?. push ( take ) ; } ;
5165 let n = 0 ;
5266 const refused = ( o : string ) => {
5367 if ( refuse . has ( o ) ) throw Object . assign ( new Error ( `statement refused on ${ o } ` ) , { code : 'ETIMEDOUT' } ) ;
@@ -62,29 +76,46 @@ function makeDriver() {
6276 return page . map ( copy ) ;
6377 } ,
6478 async findOne ( o : string , ast : any ) { return ( await this . find ( o , ast ) ) [ 0 ] ?? null ; } ,
65- async create ( o : string , data : Row ) {
79+ async create ( o : string , data : Row , opts ?: any ) {
6680 refused ( o ) ;
6781 const row = { ...data , id : ( data . id as string ) ?? `${ o } _${ ++ n } ` } ;
6882 storeFor ( o ) . set ( String ( row . id ) , row ) ;
83+ undoable ( opts , ( ) => storeFor ( o ) . delete ( String ( row . id ) ) ) ;
6984 return copy ( row ) ;
7085 } ,
71- async update ( o : string , id : string , data : Row ) {
86+ async update ( o : string , id : string , data : Row , opts ?: any ) {
7287 refused ( o ) ;
73- const row = { ...storeFor ( o ) . get ( String ( id ) ) , ...data , id } ;
88+ const prior = storeFor ( o ) . get ( String ( id ) ) ;
89+ const row = { ...prior , ...data , id } ;
7490 storeFor ( o ) . set ( String ( id ) , row ) ;
91+ undoable ( opts , ( ) => ( prior ? storeFor ( o ) . set ( String ( id ) , prior ) : storeFor ( o ) . delete ( String ( id ) ) ) ) ;
7592 return copy ( row ) ;
7693 } ,
77- async updateMany ( o : string , ast : any , data : Row ) {
94+ async updateMany ( o : string , ast : any , data : Row , opts ?: any ) {
7895 refused ( o ) ;
7996 let k = 0 ;
8097 for ( const [ id , row ] of storeFor ( o ) ) {
8198 if ( ! matches ( row , ast ?. where ) ) continue ;
8299 storeFor ( o ) . set ( id , { ...row , ...data , id } ) ;
100+ undoable ( opts , ( ) => storeFor ( o ) . set ( id , row ) ) ;
101+ k += 1 ;
102+ }
103+ return k ;
104+ } ,
105+ async delete ( o : string , id : string , opts ?: any ) {
106+ const prior = storeFor ( o ) . get ( String ( id ) ) ;
107+ if ( prior ) undoable ( opts , ( ) => storeFor ( o ) . set ( String ( id ) , prior ) ) ;
108+ return storeFor ( o ) . delete ( String ( id ) ) ;
109+ } ,
110+ async deleteMany ( o : string , ast : any , opts ?: any ) {
111+ let k = 0 ;
112+ for ( const [ id , row ] of Array . from ( storeFor ( o ) ) ) {
113+ if ( ! matches ( row , ast ?. where ) ) continue ;
114+ await this . delete ( o , id , opts ) ;
83115 k += 1 ;
84116 }
85117 return k ;
86118 } ,
87- async delete ( o : string , id : string ) { return storeFor ( o ) . delete ( String ( id ) ) ; } ,
88119 async count ( o : string , ast : any ) {
89120 if ( countFails . has ( o ) ) throw new Error ( `count unavailable on ${ o } ` ) ;
90121 return ( await this . find ( o , ast ) ) . length ;
@@ -94,13 +125,19 @@ function makeDriver() {
94125 async beginTransaction ( ) {
95126 const handle = { trx : snapshots . size + 1 } ;
96127 snapshots . set ( handle , new Map ( Array . from ( stores , ( [ o , s ] ) => [ o , new Map ( Array . from ( s , ( [ k , v ] ) => [ k , { ...v } ] ) ) ] ) ) ) ;
128+ undoLogs . set ( handle , [ ] ) ;
97129 return handle ;
98130 } ,
99- async commit ( h : unknown ) { snapshots . delete ( h ) ; } ,
100- async rollback ( h : unknown ) { stores = snapshots . get ( h ) ?? stores ; snapshots . delete ( h ) ; } ,
131+ async commit ( h : unknown ) { snapshots . delete ( h ) ; undoLogs . delete ( h ) ; } ,
132+ async rollback ( h : unknown ) {
133+ if ( isolation . on ) for ( const take of ( undoLogs . get ( h ) ?? [ ] ) . reverse ( ) ) take ( ) ;
134+ else stores = snapshots . get ( h ) ?? stores ;
135+ snapshots . delete ( h ) ;
136+ undoLogs . delete ( h ) ;
137+ } ,
101138 } ;
102139 return {
103- driver, refuse, countFails,
140+ driver, refuse, countFails, isolation ,
104141 rowsOf : ( o : string ) => Array . from ( storeFor ( o ) . values ( ) ) ,
105142 } ;
106143}
@@ -145,6 +182,16 @@ const other = {
145182} ;
146183
147184const SYSTEM = { context : { isSystem : true } } as any ;
185+ /** [#27581] A detail of the vault, holding its own secret: a cascade target. */
186+ const KEY = 'qa_vault_key' ;
187+ const vaultKey = {
188+ name : KEY , label : 'Vault key' ,
189+ fields : {
190+ id : { name : 'id' , type : 'text' } , name : { name : 'name' , type : 'text' } ,
191+ vault : { name : 'vault' , type : 'master_detail' , reference : VAULT } ,
192+ key_token : { name : 'key_token' , type : 'secret' } ,
193+ } ,
194+ } ;
148195
149196describe ( '[#27495] a sys_secret row exists only while a committed row names it' , ( ) => {
150197 let engine : ObjectQL ;
@@ -371,4 +418,115 @@ describe('[#27495] a sys_secret row exists only while a committed row names it',
371418 expect ( errors [ 0 ] ) . not . toContain ( before ) ;
372419 } ) ;
373420 } ) ;
421+
422+ describe ( '[#27581] a committed delete releases the handles the removed rows held' , ( ) => {
423+ it ( 'a by-id delete removes the row its record named' , async ( ) => {
424+ const gone = await seed ( 'r1' , 'gone' ) ;
425+ const kept = await seed ( 'r2' , 'kept' ) ;
426+ await expect ( engine . delete ( VAULT , { where : { id : 'r1' } } ) ) . resolves . toBe ( true ) ;
427+ expect ( secrets ( ) . map ( ( s ) => s . id ) ) . toEqual ( [ kept ] ) ;
428+ expect ( secrets ( ) . some ( ( s ) => s . id === gone ) ) . toBe ( false ) ;
429+ expect ( await engine . resolveSecretField ( VAULT , 'r2' , 'token' ) ) . toBe ( 'kept' ) ;
430+ expect ( errors ) . toEqual ( [ ] ) ;
431+ } ) ;
432+
433+ it ( 'a predicate delete removes every matched row\'s handle and no other' , async ( ) => {
434+ await seed ( 'r1' , 'one' ) ;
435+ await seed ( 'r2' , 'two' ) ;
436+ await engine . update ( VAULT , { id : 'r2' , tier : 'silver' } ) ;
437+ const silver = handleOf ( 'r2' ) ;
438+ await seed ( 'r3' , 'three' ) ;
439+ await expect ( engine . delete ( VAULT , { where : { tier : 'gold' } , multi : true } as any ) ) . resolves . toBe ( 2 ) ;
440+ expect ( secrets ( ) . map ( ( s ) => s . id ) ) . toEqual ( [ silver ] ) ;
441+ } ) ;
442+
443+ it ( 'a predicate delete with a per-row hook releases from the rows that hook read' , async ( ) => {
444+ const seen : unknown [ ] = [ ] ;
445+ engine . registerHook ( 'afterDelete' , ( ctx : any ) => { seen . push ( ctx . previous ?. id ) ; } , { object : VAULT } ) ;
446+ await seed ( 'r1' , 'one' ) ;
447+ await seed ( 'r2' , 'two' ) ;
448+ await engine . delete ( VAULT , { where : { tier : 'gold' } , multi : true } as any ) ;
449+ expect ( seen . sort ( ) ) . toEqual ( [ 'r1' , 'r2' ] ) ;
450+ expect ( secrets ( ) ) . toHaveLength ( 0 ) ;
451+ } ) ;
452+
453+ it ( 'a cascade removes the detail rows\' handles with the parent\'s, by id and by predicate' , async ( ) => {
454+ engine . registry . registerObject ( vaultKey as any ) ;
455+ await seed ( 'r1' , 'parent-1' ) ;
456+ await seed ( 'r2' , 'parent-2' ) ;
457+ await engine . insert ( KEY , { id : 'k1' , name : 'k1' , vault : 'r1' , key_token : 'child-1' } , SYSTEM ) ;
458+ await engine . insert ( KEY , { id : 'k2' , name : 'k2' , vault : 'r2' , key_token : 'child-2' } , SYSTEM ) ;
459+ expect ( secrets ( ) ) . toHaveLength ( 4 ) ;
460+ await engine . delete ( VAULT , { where : { id : 'r1' } } ) ;
461+ expect ( d . rowsOf ( KEY ) . map ( ( r ) => r . id ) ) . toEqual ( [ 'k2' ] ) ;
462+ expect ( secrets ( ) . map ( ( s ) => s . key ) . sort ( ) ) . toEqual ( [ 'key_token' , 'token' ] ) ;
463+ await engine . delete ( VAULT , { where : { tier : 'gold' } , multi : true } as any ) ;
464+ expect ( d . rowsOf ( KEY ) ) . toHaveLength ( 0 ) ;
465+ expect ( secrets ( ) ) . toHaveLength ( 0 ) ;
466+ } ) ;
467+
468+ it ( 'a handle another row still names outlives the delete of every row but the last' , async ( ) => {
469+ await seed ( 'r1' , 'one' ) ;
470+ await seed ( 'r2' , 'two' ) ;
471+ await engine . update ( VAULT , { token : 'shared' } , { where : { tier : 'gold' } , multi : true } as any ) ;
472+ const shared = handleOf ( 'r1' ) ;
473+ expect ( handleOf ( 'r2' ) ) . toBe ( shared ) ;
474+
475+ await engine . delete ( VAULT , { where : { id : 'r1' } } ) ;
476+ expect ( secrets ( ) . map ( ( s ) => s . id ) ) . toEqual ( [ shared ] ) ;
477+ expect ( await engine . resolveSecretField ( VAULT , 'r2' , 'token' ) ) . toBe ( 'shared' ) ;
478+
479+ await engine . delete ( VAULT , { where : { id : 'r2' } } ) ;
480+ expect ( secrets ( ) ) . toHaveLength ( 0 ) ;
481+ } ) ;
482+
483+ it ( 'deleting a row that holds another column\'s handle releases nothing' , async ( ) => {
484+ const foreign = await seed ( 'r1' , 'theirs' ) ;
485+ await engine . insert ( OTHER , { id : 'o1' , name : 'copy' , other_token : `secret:${ foreign } ` } , SYSTEM ) ;
486+ await engine . delete ( OTHER , { where : { id : 'o1' } } ) ;
487+ expect ( secrets ( ) . map ( ( s ) => s . id ) ) . toEqual ( [ foreign ] ) ;
488+ expect ( await engine . resolveSecretField ( VAULT , 'r1' , 'token' ) ) . toBe ( 'theirs' ) ;
489+ } ) ;
490+
491+ it ( 'a delete rolled back with its unit keeps the row, though the record write was undone alone' , async ( ) => {
492+ d . isolation . on = true ;
493+ const before = await seed ( 'r1' , 'stays' ) ;
494+ await expect ( engine . transaction ( async ( trx : any ) => {
495+ await engine . delete ( VAULT , { where : { id : 'r1' } , context : trx } ) ;
496+ throw new Error ( 'roll the unit back' ) ;
497+ } , SYSTEM . context ) ) . rejects . toThrow ( / r o l l t h e u n i t b a c k / ) ;
498+ expect ( stored ( 'r1' ) ) . toBeDefined ( ) ;
499+ expect ( secrets ( ) . map ( ( s ) => s . id ) ) . toEqual ( [ before ] ) ;
500+ expect ( await engine . resolveSecretField ( VAULT , 'r1' , 'token' ) ) . toBe ( 'stays' ) ;
501+ } ) ;
502+
503+ it ( 'a delete refused by a beforeDelete hook, or rolled back by an aborting afterDelete, keeps the row' , async ( ) => {
504+ const before = await seed ( 'r1' , 'stays' ) ;
505+ engine . registerHook ( 'beforeDelete' , ( ) => { throw Object . assign ( new Error ( 'hook says no' ) , { code : 'HOOK_REFUSED' , status : 400 } ) ; } , { object : VAULT } ) ;
506+ await expect ( engine . delete ( VAULT , { where : { id : 'r1' } } ) ) . rejects . toMatchObject ( { code : 'HOOK_REFUSED' , status : 400 } ) ;
507+ expect ( secrets ( ) . map ( ( s ) => s . id ) ) . toEqual ( [ before ] ) ;
508+
509+ const other = await seed ( 'o1' , 'also-stays' ) ;
510+ engine . registry . registerObject ( { ...vault , name : 'qa_vault_two' } as any ) ;
511+ await engine . insert ( 'qa_vault_two' , { id : 't1' , name : 't1' , token : 'two-stays' } , SYSTEM ) ;
512+ const twoHandle = parseSecretRef ( d . rowsOf ( 'qa_vault_two' ) [ 0 ] . token ) ;
513+ bindHooksToEngine ( engine , [
514+ { name : 'veto' , object : 'qa_vault_two' , events : [ 'afterDelete' ] , handler : ( ) => { throw new Error ( 'refused by the hook' ) ; } } as unknown as Hook ,
515+ ] , { packageId : 'app:t' , logger } ) ;
516+ await expect ( engine . delete ( 'qa_vault_two' , { where : { id : 't1' } } ) ) . rejects . toThrow ( / r e f u s e d b y t h e h o o k / ) ;
517+ expect ( d . rowsOf ( 'qa_vault_two' ) ) . toHaveLength ( 1 ) ;
518+ expect ( secrets ( ) . map ( ( s ) => s . id ) . sort ( ) ) . toEqual ( [ before , other , twoHandle ] . sort ( ) ) ;
519+ } ) ;
520+
521+ it ( 'a release that cannot confirm the column let go keeps the row and says so' , async ( ) => {
522+ const before = await seed ( 'r1' , 'stays-stored' ) ;
523+ d . countFails . add ( VAULT ) ;
524+ await engine . delete ( VAULT , { where : { id : 'r1' } } ) ;
525+ expect ( d . rowsOf ( VAULT ) ) . toHaveLength ( 0 ) ;
526+ expect ( secrets ( ) . map ( ( s ) => s . id ) ) . toEqual ( [ before ] ) ;
527+ expect ( errors ) . toHaveLength ( 1 ) ;
528+ expect ( errors [ 0 ] ) . toContain ( `${ VAULT } .token` ) ;
529+ expect ( errors [ 0 ] ) . not . toContain ( before ) ;
530+ } ) ;
531+ } ) ;
374532} ) ;
0 commit comments