@@ -104,16 +104,37 @@ describe('write path', () => {
104104 expect ( ( await readUser ( ) ) ! [ 'email' ] ) . toBe ( 'second@example.com' )
105105 } )
106106
107- it ( 'skips a write that would change nothing ' , async ( ) => {
107+ it ( 'skips a write with the same payload and timestamp ' , async ( ) => {
108108 await upsertRows ( db , opts , users , 'prod' , [ { data : userPayload ( ) , updatedAt : '2026-01-01T00:00:00.000Z' } ] , 'replace' )
109109 const again = await upsertRows ( db , opts , users , 'prod' , [
110- { data : userPayload ( ) , updatedAt : '2026-01-02T00 :00:00.000Z' } ,
110+ { data : userPayload ( ) , updatedAt : '2026-01-01T00 :00:00.000Z' } ,
111111 ] , 'replace' )
112- // Backfill and reconcile re-fetch mostly-unchanged rows; rewriting them all
113- // would bloat WAL and churn autovacuum for nothing.
112+ // Rewriting an identical observation would bloat WAL for nothing.
114113 expect ( again ) . toMatchObject ( { created : 0 , updated : 0 , skipped : 1 } )
115114 } )
116115
116+ it . each ( [ 'merge' , 'replace' ] as const ) ( 'advances the timestamp for unchanged payloads in %s mode' , async ( mode ) => {
117+ const roles = streamByName ( 'roles' ) !
118+ const data = { slug : 'admin' , permissions : [ 'read' ] }
119+ await upsertRows ( db , opts , roles , 'prod' , [
120+ { data, updatedAt : '2026-01-01T00:00:00.000Z' } ,
121+ ] , mode )
122+ const newer = await upsertRows ( db , opts , roles , 'prod' , [
123+ { data, updatedAt : '2026-01-03T00:00:00.000Z' } ,
124+ ] , mode )
125+ const stale = await upsertRows ( db , opts , roles , 'prod' , [
126+ { data : { ...data , permissions : [ 'write' ] } , updatedAt : '2026-01-02T00:00:00.000Z' } ,
127+ ] , mode )
128+
129+ expect ( newer ) . toMatchObject ( { created : 0 , updated : 1 , skipped : 0 } )
130+ expect ( stale ) . toMatchObject ( { created : 0 , updated : 0 , skipped : 1 } )
131+ const result = await db . query ( `select permissions, "_updated_at" from workos.roles` )
132+ expect ( result . rows ) . toEqual ( [ {
133+ permissions : [ 'read' ] ,
134+ _updated_at : new Date ( '2026-01-03T00:00:00.000Z' ) ,
135+ } ] )
136+ } )
137+
117138 it ( 'merges event payloads instead of nulling the fields events omit' , async ( ) => {
118139 // The whole reason `merge` exists: user.* events do not carry external_id,
119140 // metadata, last_sign_in_at or locale.
0 commit comments