|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#24835] `update()` leaves the caller's `data` argument exactly as it was |
| 5 | + * handed in — on every path that stamps `updated_at`. |
| 6 | + * |
| 7 | + * ## What was wrong |
| 8 | + * |
| 9 | + * `formatInput` and `applyWriteColumnMap` are copy-on-write: a patch that needs |
| 10 | + * no coercion comes back as the CALLER'S OWN OBJECT. `update()` and |
| 11 | + * `rotatedUpdateById()` then assigned the `updated_at` stamp onto it, so the |
| 12 | + * stamp landed in the caller's patch. ObjectQL hands `driver.update` the same |
| 13 | + * object it later publishes as `DataEvent.changes`, so every reader of the |
| 14 | + * update event saw a value the caller never wrote: |
| 15 | + * |
| 16 | + * - on PostgreSQL / MySQL the stamp is `knex.fn.now()`, a knex `Raw` that |
| 17 | + * holds the client, its pool and a Node `Timeout`. `JSON.stringify` of the |
| 18 | + * event payload throws ("Converting circular structure to JSON"), so the |
| 19 | + * webhook outbox could not store the delivery and EVERY webhook delivery for |
| 20 | + * a record update was lost there; |
| 21 | + * - on SQLite it is an ISO-8601 string — serialisable, which is why the loss |
| 22 | + * never showed there, and just as foreign to the caller's patch. |
| 23 | + * |
| 24 | + * `updateMany()` already stamped into a fresh object; these two doors now do |
| 25 | + * the same. |
| 26 | + * |
| 27 | + * ## What is pinned |
| 28 | + * |
| 29 | + * The caller's patch is deep-equal to a clone taken before the call, on each |
| 30 | + * path that stamps: the plain path (a table this driver's DDL created), the |
| 31 | + * presumed-timestamp path (`skipSchemaSync`-style registration, both when the |
| 32 | + * column exists and when the fallback has to drop the stamp), and the rotated |
| 33 | + * path. Each case also proves the stamp really ran on that path, so a pin |
| 34 | + * cannot pass because nothing was stamped at all. SQLite only: the mutation is |
| 35 | + * dialect-independent (only the stamped VALUE differs), and the live-PG/MySQL |
| 36 | + * driver lane does not run on pull requests. |
| 37 | + */ |
| 38 | + |
| 39 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 40 | +import { SqlDriver } from './index.js'; |
| 41 | + |
| 42 | +/** These fixtures are not tenant-scoped. */ |
| 43 | +const OPTS = { bypassTenantAudit: true } as const; |
| 44 | +const BACKDATED = '2020-01-01T00:00:00.000Z'; |
| 45 | + |
| 46 | +function sqlite(): SqlDriver { |
| 47 | + return new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 48 | +} |
| 49 | + |
| 50 | +/** Read `updated_at` straight out of storage, past every read-side coercion. */ |
| 51 | +async function storedUpdatedAt(driver: SqlDriver, table: string, id: string): Promise<unknown> { |
| 52 | + const row: any = await (driver as any).knex(table).where('id', id).first(); |
| 53 | + return row?.updated_at; |
| 54 | +} |
| 55 | + |
| 56 | +/** Update with `patch`, and return what the caller's object looked like before and after. */ |
| 57 | +async function updateAndCompare(driver: SqlDriver, object: string, id: string, patch: Record<string, unknown>) { |
| 58 | + const before = structuredClone(patch); |
| 59 | + await driver.update(object, id, patch, OPTS); |
| 60 | + return { before, after: patch }; |
| 61 | +} |
| 62 | + |
| 63 | +describe('#24835 — SqlDriver.update() never writes into the caller\'s data', () => { |
| 64 | + let driver: SqlDriver | undefined; |
| 65 | + afterEach(async () => { |
| 66 | + await driver?.disconnect(); |
| 67 | + driver = undefined; |
| 68 | + }); |
| 69 | + |
| 70 | + it('plain path: a table the driver created stamps updated_at, and the patch is untouched', async () => { |
| 71 | + driver = sqlite(); |
| 72 | + await driver.initObjects([{ name: 'n24835_plain', fields: { title: { type: 'text' } } } as any]); |
| 73 | + await driver.create('n24835_plain', { id: 'p1', title: 'seed' }, OPTS); |
| 74 | + await (driver as any).knex('n24835_plain').where('id', 'p1').update({ updated_at: BACKDATED }); |
| 75 | + |
| 76 | + const { before, after } = await updateAndCompare(driver, 'n24835_plain', 'p1', { title: 'renamed' }); |
| 77 | + |
| 78 | + expect(after).toStrictEqual(before); |
| 79 | + expect(Object.keys(after)).toEqual(['title']); |
| 80 | + // The stamp ran on this path — it landed in storage, not in the patch. |
| 81 | + expect(await storedUpdatedAt(driver, 'n24835_plain', 'p1')).not.toBe(BACKDATED); |
| 82 | + }); |
| 83 | + |
| 84 | + it('presumed path, column present: the speculative stamped attempt leaves the patch untouched', async () => { |
| 85 | + driver = sqlite(); |
| 86 | + const knex = (driver as any).knex; |
| 87 | + await knex.raw( |
| 88 | + `create table "n24835_presumed" (id text primary key, title text, ` |
| 89 | + + `created_at text default (strftime('%Y-%m-%dT%H:%M:%fZ','now')), ` |
| 90 | + + `updated_at text default (strftime('%Y-%m-%dT%H:%M:%fZ','now')))`, |
| 91 | + ); |
| 92 | + driver.registerObjectMetadata([{ name: 'n24835_presumed', fields: { title: { type: 'text' } } } as any]); |
| 93 | + await driver.create('n24835_presumed', { id: 'q1', title: 'seed' }, OPTS); |
| 94 | + await knex('n24835_presumed').where('id', 'q1').update({ updated_at: BACKDATED }); |
| 95 | + expect((driver as any).updatedAtStampIsPresumed('n24835_presumed')).toBe(true); |
| 96 | + |
| 97 | + const { before, after } = await updateAndCompare(driver, 'n24835_presumed', 'q1', { title: 'renamed' }); |
| 98 | + |
| 99 | + expect(after).toStrictEqual(before); |
| 100 | + expect(await storedUpdatedAt(driver, 'n24835_presumed', 'q1')).not.toBe(BACKDATED); |
| 101 | + // The presumption was settled by this very call. |
| 102 | + expect((driver as any).updatedAtStampIsPresumed('n24835_presumed')).toBe(false); |
| 103 | + }); |
| 104 | + |
| 105 | + it('presumed path, column absent: the fallback that drops the stamp leaves the patch untouched', async () => { |
| 106 | + driver = sqlite(); |
| 107 | + const knex = (driver as any).knex; |
| 108 | + await knex.raw(`create table "n24835_nocol" (id text primary key, title text)`); |
| 109 | + driver.registerObjectMetadata([{ name: 'n24835_nocol', fields: { title: { type: 'text' } } } as any]); |
| 110 | + await knex('n24835_nocol').insert({ id: 'r1', title: 'seed' }); |
| 111 | + expect((driver as any).updatedAtStampIsPresumed('n24835_nocol')).toBe(true); |
| 112 | + |
| 113 | + const { before, after } = await updateAndCompare(driver, 'n24835_nocol', 'r1', { title: 'renamed' }); |
| 114 | + |
| 115 | + expect(after).toStrictEqual(before); |
| 116 | + expect((await knex('n24835_nocol').where('id', 'r1').first()).title).toBe('renamed'); |
| 117 | + // The fallback path really ran: the driver now knows the column is absent. |
| 118 | + expect((driver as any).updatedAtColumnState.get('n24835_nocol')).toBe('absent'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('rotated path: a rotation-managed object stamps updated_at in its shard, and the patch is untouched', async () => { |
| 122 | + driver = sqlite(); |
| 123 | + const rotated = { |
| 124 | + name: 'n24835_rot', |
| 125 | + fields: { payload: { type: 'text' }, created_at: { type: 'datetime' } }, |
| 126 | + lifecycle: { class: 'telemetry', storage: { strategy: 'rotation', shards: 3, unit: 'day' } }, |
| 127 | + }; |
| 128 | + await driver.initObjects([rotated as any]); |
| 129 | + const shards: string[] | undefined = (driver as any).rotationShardsOf('n24835_rot'); |
| 130 | + expect(shards?.length).toBeGreaterThan(0); |
| 131 | + await driver.create('n24835_rot', { id: 's1', payload: 'seed' }, OPTS); |
| 132 | + await (driver as any).knex(shards![0]).where('id', 's1').update({ updated_at: BACKDATED }); |
| 133 | + |
| 134 | + const { before, after } = await updateAndCompare(driver, 'n24835_rot', 's1', { payload: 'patched' }); |
| 135 | + |
| 136 | + expect(after).toStrictEqual(before); |
| 137 | + expect(await storedUpdatedAt(driver, shards![0], 's1')).not.toBe(BACKDATED); |
| 138 | + }); |
| 139 | +}); |
0 commit comments