masterrecord 1.5.10 → 1.5.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.md +14 -0
- package/context.js +65 -75
- package/package.json +1 -1
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# MasterRecord Changelog
|
|
2
2
|
|
|
3
|
+
## v1.5.11 — deletes are released after commit (fix 1.5.10 "zombie delete" latch); lifecycle reconciliation centralized
|
|
4
|
+
|
|
5
|
+
**Regression fixed (introduced in 1.5.10):** 1.5.10 released a flushed entity only if its state was back to `'track'`. Inserts and updates are reset to `'track'` by the batch processors, but a **delete stays in state `'delete'`** after `_processBatchDeletes`, so it failed the release check and was **never untracked**. Each deleted entity then became a permanent "zombie" in the change set: every later `saveChanges()` re-included it and **re-issued its DELETE** (hundreds of redundant deletes), the cross-context "unsaved changes" warning fired forever and grew, and — because the unit of work was permanently non-empty and stuck — **real writes stopped persisting after a round or two** (an admin block/unblock UPDATE and session revocation both silently failed). No exception, no rollback.
|
|
6
|
+
|
|
7
|
+
**Root cause (design):** entity-lifecycle transitions were **scattered** across three batch processors plus a release filter, and the delete path had no reset — an asymmetry that made the gap inevitable.
|
|
8
|
+
|
|
9
|
+
**Fix (Unit-of-Work reconciliation, one place, one rule):** `saveChanges()` now captures each committed entity's mutation **generation** at flush time and, after the transaction commits, runs a single `_reconcileFlushed()` step with one uniform rule for insert/update/delete:
|
|
10
|
+
- **re-mutated in flight** (generation changed) → leave it in its new pending state so the next save persists it;
|
|
11
|
+
- **otherwise committed cleanly** → insert/modified reset to `'track'` then **detached**; delete **detached** (the row is gone).
|
|
12
|
+
|
|
13
|
+
The per-processor state resets were removed, so there is no longer any state-specific branch that can strand an entity. This also fixes the falsy-`__state` edge and corrects the stale `__untrack` doc comment.
|
|
14
|
+
|
|
15
|
+
New tests: `test/delete-release-no-zombie.test.js` — a committed delete is detached (no replay); repeated update-then-delete does not latch (UPDATEs keep persisting); a mixed insert+update+delete change set reconciles all three. Full suite green (0 fail, 302 pass, 20 gated skipped); 0 lint errors.
|
|
16
|
+
|
|
3
17
|
## v1.5.10 — saveChanges() is a proper unit of work: commits only its change set (real shared-context lost-write fix)
|
|
4
18
|
|
|
5
19
|
**Bug (silent lost write, 60–75% under load):** `saveChanges()` snapshotted the **entire tracked-entity list**, and after writing untracked **all of it**. On a shared/singleton context (one instance serving many requests) this is corrupting, because queried rows are auto-tracked and read-only `.toList()` handlers leave thousands of clean entities tracked forever. So one caller's `saveChanges()` — **even an empty one with nothing dirty** — snapshotted and untracked another in-flight caller's freshly loaded/mutated row, and that caller's own `saveChanges()` then found nothing tracked and **issued no UPDATE while resolving `true`**. Frequency scaled with the size of the shared list (~43k tracked entities → 60–75% loss; ~4 entities → never). The 1.5.9 `__untrack` guard didn't catch it: the interfering save evaluated the victim's state before the pending mutation, and an empty save still snapshotted/untracked everything. Emitted no SQL of its own, so nothing appeared in the log between the SELECT and the missing UPDATE.
|
package/context.js
CHANGED
|
@@ -1868,11 +1868,6 @@ class context {
|
|
|
1868
1868
|
}
|
|
1869
1869
|
}
|
|
1870
1870
|
|
|
1871
|
-
// Capture mutation versions before the async write so the state reset
|
|
1872
|
-
// below skips any entity a concurrent request re-mutated in flight.
|
|
1873
|
-
const __preWriteVersion = new Map();
|
|
1874
|
-
for (const entity of entities) { __preWriteVersion.set(entity, entity.__version || 0); }
|
|
1875
|
-
|
|
1876
1871
|
if (entities.length === 1) {
|
|
1877
1872
|
// Single insert - use existing insertManager (already sets ID)
|
|
1878
1873
|
const insert = new insertManager(this._SQLEngine, this._isModelValid, this.__entities);
|
|
@@ -1952,26 +1947,14 @@ class context {
|
|
|
1952
1947
|
}
|
|
1953
1948
|
}
|
|
1954
1949
|
|
|
1955
|
-
//
|
|
1956
|
-
//
|
|
1957
|
-
//
|
|
1958
|
-
//
|
|
1959
|
-
//
|
|
1960
|
-
//
|
|
1961
|
-
// never marks the entity modified, and the change is dropped on the next
|
|
1962
|
-
// saveChanges(). attachTrackingTo() installs those accessors in place so
|
|
1963
|
-
// the just-inserted entity is update-trackable, exactly like one loaded
|
|
1964
|
-
// from the database.
|
|
1950
|
+
// Install change-tracking accessors on each inserted entity so later
|
|
1951
|
+
// edits are tracked: a plain `new Model()` passed to add() has ordinary
|
|
1952
|
+
// own-property fields, NOT the accessors a queried entity has, so without
|
|
1953
|
+
// this a later `row.name = 'x'` would be a silent write. Lifecycle state
|
|
1954
|
+
// (insert -> clean, then detached) is transitioned centrally in
|
|
1955
|
+
// _reconcileFlushed after the transaction commits — not here.
|
|
1965
1956
|
const tracker = new EntityTrackerModel();
|
|
1966
1957
|
for (const entity of entities) {
|
|
1967
|
-
// Only mark the just-inserted entity clean if it was not re-mutated
|
|
1968
|
-
// during the async insert. If a concurrent request changed a field in
|
|
1969
|
-
// flight, leave it dirty so that change is persisted as an UPDATE on
|
|
1970
|
-
// the next save instead of being silently reset away.
|
|
1971
|
-
if ((entity.__version || 0) === __preWriteVersion.get(entity)) {
|
|
1972
|
-
entity.__state = 'track';
|
|
1973
|
-
entity.__dirtyFields = [];
|
|
1974
|
-
}
|
|
1975
1958
|
try {
|
|
1976
1959
|
tracker.attachTrackingTo(entity);
|
|
1977
1960
|
} catch (attachErr) {
|
|
@@ -1994,13 +1977,6 @@ class context {
|
|
|
1994
1977
|
}
|
|
1995
1978
|
}
|
|
1996
1979
|
|
|
1997
|
-
// Capture each entity's mutation version AFTER beforeSave and BEFORE the
|
|
1998
|
-
// async write. The reset at the end uses it to skip any entity a
|
|
1999
|
-
// concurrent request re-mutated during the write — resetting such an
|
|
2000
|
-
// entity would clear the new dirty state and silently drop that update.
|
|
2001
|
-
const __preWriteVersion = new Map();
|
|
2002
|
-
for (const entity of entities) { __preWriteVersion.set(entity, entity.__version || 0); }
|
|
2003
|
-
|
|
2004
1980
|
if (entities.length === 1) {
|
|
2005
1981
|
// Single update - use existing logic.
|
|
2006
1982
|
// NOTE: the second argument must be `__entity` (double underscore).
|
|
@@ -2063,18 +2039,11 @@ class context {
|
|
|
2063
2039
|
}
|
|
2064
2040
|
}
|
|
2065
2041
|
|
|
2066
|
-
//
|
|
2067
|
-
//
|
|
2068
|
-
//
|
|
2069
|
-
//
|
|
2070
|
-
//
|
|
2071
|
-
// UPDATE would never be issued (a silent lost write on a shared context).
|
|
2072
|
-
// Leaving it dirty lets its own saveChanges() persist the newer value.
|
|
2073
|
-
for (const entity of entities) {
|
|
2074
|
-
if ((entity.__version || 0) !== __preWriteVersion.get(entity)) continue;
|
|
2075
|
-
entity.__state = 'track';
|
|
2076
|
-
entity.__dirtyFields = [];
|
|
2077
|
-
}
|
|
2042
|
+
// Lifecycle state (modified -> clean, then detached) is transitioned
|
|
2043
|
+
// centrally in _reconcileFlushed after the transaction commits — not
|
|
2044
|
+
// here. Keeping it in one place, applied uniformly to inserts, updates
|
|
2045
|
+
// and deletes, is what prevents state-specific gaps (a delete never
|
|
2046
|
+
// becomes 'track', so a per-processor reset here would strand it).
|
|
2078
2047
|
}
|
|
2079
2048
|
|
|
2080
2049
|
/**
|
|
@@ -2225,10 +2194,18 @@ class context {
|
|
|
2225
2194
|
return true;
|
|
2226
2195
|
}
|
|
2227
2196
|
|
|
2228
|
-
//
|
|
2229
|
-
//
|
|
2230
|
-
|
|
2231
|
-
|
|
2197
|
+
// Snapshot the committed unit of work: for each entity in the change set,
|
|
2198
|
+
// capture the exact pending change we are about to flush — its mutation
|
|
2199
|
+
// GENERATION (version) and whether it was a delete. All post-commit
|
|
2200
|
+
// lifecycle transitions are driven from this snapshot in ONE place
|
|
2201
|
+
// (_reconcileFlushed), by ONE uniform rule, rather than being scattered
|
|
2202
|
+
// across the per-state batch processors — which is how a delete (never
|
|
2203
|
+
// reset to 'track') used to be stranded forever.
|
|
2204
|
+
const captured = changeSet.map(e => ({
|
|
2205
|
+
entity: e,
|
|
2206
|
+
generation: e.__version || 0,
|
|
2207
|
+
wasDelete: e.__state === 'delete',
|
|
2208
|
+
}));
|
|
2232
2209
|
|
|
2233
2210
|
const affectedTables = new Set();
|
|
2234
2211
|
for (const e of changeSet) { if (e.__name) affectedTables.add(e.__name); }
|
|
@@ -2252,15 +2229,7 @@ class context {
|
|
|
2252
2229
|
this._queryCache.invalidateTable(tableName);
|
|
2253
2230
|
}
|
|
2254
2231
|
|
|
2255
|
-
|
|
2256
|
-
// re-mutated during the write (state back to 'track' AND version
|
|
2257
|
-
// unchanged). An entity re-mutated in flight has a bumped __version
|
|
2258
|
-
// (and the batch processors leave it dirty) — keep it tracked so its
|
|
2259
|
-
// own saveChanges() still persists the newer change.
|
|
2260
|
-
const done = changeSet.filter(
|
|
2261
|
-
e => e.__state === 'track' && (e.__version || 0) === versions.get(e)
|
|
2262
|
-
);
|
|
2263
|
-
this.__untrack(done);
|
|
2232
|
+
this._reconcileFlushed(captured);
|
|
2264
2233
|
return true;
|
|
2265
2234
|
} catch (error) {
|
|
2266
2235
|
this.__clearErrorHandler();
|
|
@@ -2273,26 +2242,47 @@ class context {
|
|
|
2273
2242
|
}
|
|
2274
2243
|
|
|
2275
2244
|
/**
|
|
2276
|
-
*
|
|
2277
|
-
*
|
|
2278
|
-
*
|
|
2279
|
-
*
|
|
2280
|
-
*
|
|
2281
|
-
*
|
|
2282
|
-
*
|
|
2283
|
-
*
|
|
2284
|
-
*
|
|
2285
|
-
*
|
|
2286
|
-
*
|
|
2287
|
-
*
|
|
2288
|
-
*
|
|
2289
|
-
*
|
|
2290
|
-
*
|
|
2291
|
-
*
|
|
2292
|
-
*
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2245
|
+
* Post-commit reconciliation for a flushed unit of work — the SINGLE place
|
|
2246
|
+
* that transitions entity lifecycle state after a successful save. Driven by
|
|
2247
|
+
* the generation snapshot captured at flush time, with one uniform rule for
|
|
2248
|
+
* inserts, updates and deletes (no per-state special cases — that asymmetry
|
|
2249
|
+
* is what stranded deletes as replaying "zombies").
|
|
2250
|
+
*
|
|
2251
|
+
* For each committed entity:
|
|
2252
|
+
* - Re-mutated in flight (generation changed): a NEW change arrived during
|
|
2253
|
+
* the async write. Leave the entity in its current pending state so the
|
|
2254
|
+
* next saveChanges() persists it — never reset it, or that change is lost.
|
|
2255
|
+
* - Otherwise it was fully persisted with no newer change:
|
|
2256
|
+
* * insert / modified -> reset to the clean 'track' state, then detach
|
|
2257
|
+
* from the unit of work (remove from tracking).
|
|
2258
|
+
* * delete -> detach from the unit of work (the row is gone;
|
|
2259
|
+
* it never becomes 'track', so it is released on the delete branch).
|
|
2260
|
+
*
|
|
2261
|
+
* @param {Array<{entity:object, generation:number, wasDelete:boolean}>} captured
|
|
2262
|
+
*/
|
|
2263
|
+
_reconcileFlushed(captured) {
|
|
2264
|
+
const detach = [];
|
|
2265
|
+
for (const { entity, generation, wasDelete } of captured) {
|
|
2266
|
+
if ((entity.__version || 0) !== generation) {
|
|
2267
|
+
// A mutation landed during the flush; keep the new pending change.
|
|
2268
|
+
continue;
|
|
2269
|
+
}
|
|
2270
|
+
if (!wasDelete) {
|
|
2271
|
+
entity.__state = 'track';
|
|
2272
|
+
entity.__dirtyFields = [];
|
|
2273
|
+
}
|
|
2274
|
+
detach.push(entity);
|
|
2275
|
+
}
|
|
2276
|
+
this.__untrack(detach);
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
/**
|
|
2280
|
+
* Remove an exact set of entities from change tracking (identity map +
|
|
2281
|
+
* tracked list). The caller curates the set — saveChanges passes only the
|
|
2282
|
+
* entities it fully flushed and that were not re-mutated in flight — so this
|
|
2283
|
+
* removes precisely what it is given and applies NO state test of its own
|
|
2284
|
+
* (a state test here is what stranded deletes, whose state stays 'delete').
|
|
2285
|
+
* Also used by detach()/clearChangeTracker() and attach()'s re-home path.
|
|
2296
2286
|
*/
|
|
2297
2287
|
__untrack(batch) {
|
|
2298
2288
|
if (!batch || !batch.length) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.11",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./MasterRecord.js",
|