@voltro/sql-mssql 0.45.0 → 0.46.0
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/CHANGELOG.md +176 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +83 -75
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -39,6 +39,182 @@ _Changes staged for the next release accumulate here (rolled up from
|
|
|
39
39
|
|
|
40
40
|
---
|
|
41
41
|
|
|
42
|
+
## [0.46.0] — 2026-08-22
|
|
43
|
+
|
|
44
|
+
### ⚠ BREAKING
|
|
45
|
+
|
|
46
|
+
- **@voltro/runtime, @voltro/cli** — An aggregate's `incremental.source` is a table name, and is now typed and audited like one.
|
|
47
|
+
|
|
48
|
+
0.45.0 narrowed a query's and a stream's `source:` for a specific failure: a name matching no table does not error, it produces a subscription that serves once and goes quiet. An aggregate's CDC source misses the same way and is quieter still — the runner subscribes to a table nothing writes, no delta arrives, and the aggregate stops tracking its input while every read of it succeeds and returns a number.
|
|
49
|
+
|
|
50
|
+
It stayed `string` in that change, and not because anyone weighed it. It reads differently — `incremental.source`, a single name on a definition, rather than a list on a descriptor — so it did not fit the loop, and a shape that does not fit reads as a type mismatch instead of a gap.
|
|
51
|
+
|
|
52
|
+
Both halves close now. The field is `TableName`, so a name no table carries is a compile error from the next `voltro dev`. And the boot audit takes aggregates alongside queries and streams, so a stale one is named in the same warning — the runtime half matters because a source can be correct at the type level and still be a table the deployment does not have.
|
|
53
|
+
|
|
54
|
+
The audit's aggregate half runs on both boot paths and needed its OWN call on each, since aggregates are discovered several hundred lines after the existing one — which is precisely the shape that ends up wired on one path only, so it is asserted by name over both files.
|
|
55
|
+
|
|
56
|
+
A recompute-only aggregate declares no `incremental` at all and is not audited: reporting an absence as an unresolved source would report a choice as a defect.
|
|
57
|
+
|
|
58
|
+
**`voltro update` carries you across this** — codemod `0.46.0/01_typed-aggregate-source`.
|
|
59
|
+
|
|
60
|
+
### Added
|
|
61
|
+
|
|
62
|
+
- **@voltro/data-transfer, @voltro/cli** — A foreign-key cycle is caught BEFORE a staged `replace` loads, and leftover staging tables have a command.
|
|
63
|
+
|
|
64
|
+
Two things the staged swap left open, closed.
|
|
65
|
+
|
|
66
|
+
**The cycle.** The swap inserts parents first, so two tables referencing each other cannot both be satisfied by a bulk copy on postgres, sqlite or SQL Server — and `SET CONSTRAINTS ALL DEFERRED` does not rescue it, because postgres only defers a constraint declared `DEFERRABLE` and the framework declares none. Until now that surfaced as a FAILED SWAP after the whole bundle had loaded: minutes of work, then a refusal. `topo.ts` already orders parents-first and breaks a cycle at its closing edge, so a cycle is exactly a reference pointing FORWARD in that order — cheap to see before anything is loaded. Such a run says so and takes the row-by-row path, whose deferred-FK pass exists for that shape. A table referencing ITSELF is deliberately not a cycle: one statement carries the whole table, measured on all five engines, and treating it as one would cost every app with an `audit()` mixin the staged path.
|
|
67
|
+
|
|
68
|
+
**The leftovers.** A staged run drops-then-creates, so it collects its own; what survives is staging for a table set a later run does not touch. `voltro data clear-staging --yes` lists them and drops them.
|
|
69
|
+
|
|
70
|
+
It is a COMMAND and not a boot sweep, which is the decision worth recording: a booting process cannot tell a leftover from a staging table another replica is loading into right now, and with several replicas that is not a rare race — one booting pod would delete an import in flight. The refusal without `--yes` says so, and names the tables so the operator can check before answering.
|
|
71
|
+
|
|
72
|
+
**And `--no-atomic` stages too now, which is where the change is largest.** The flag exists for resumability on a large bundle, and it used to be the mode with the WORST failure: the target emptied and partially refilled, in neither state — the kill test measured 889 rows of 8 000. Staged, the ledger keeps its exact meaning (a recorded table is one fully loaded; it just lands in staging) while the target stays untouched until the swap. Resumable AND all-or-nothing, which the two flags could not be at once before.
|
|
73
|
+
|
|
74
|
+
Three things came out of wiring it, and none was visible from the design. Dropping staging in the `finally` destroyed exactly what a resume needs, so a second run read from tables that no longer existed — found by the resume test, not by reasoning; staging is dropped only after a successful swap now, and a run that kept its rows says so. The SUCCESS case needed its own guard: after a completed run the ledger still says every table is done while staging is gone, so a re-run would have copied nothing over the target — `ledger.truncated` has always meant "the destructive step already happened" and the old path is guarded by exactly that flag, so it guards this one too. And the test lever was wrong at first: withholding the snapshot disables staging but also fail-closes the rollback capture, so the run refuses before loading — the unstaged case is now driven by the real reason, a registered write recorder.
|
|
75
|
+
|
|
76
|
+
Two existing assertions inverted, and BOTH truths are kept rather than one replaced: `--no-atomic` now keeps the target when it can stage, and still costs exactly what it always did when it cannot. A killed staged run leaves the target's rows intact and writes no interrupted-replace marker — that state cannot arise on the staged path, so there is nothing for a boot to refuse over.
|
|
77
|
+
- **@voltro/runtime, @voltro/cli** — `voltro dev` says when a query reads a table it did not declare in `source:`.
|
|
78
|
+
|
|
79
|
+
The other half of the `source:` problem, and the one no static tool can see. A stale name is a compile error now, and the boot warns about one that resolves to nothing. A name that is simply ABSENT has never had an observer: the write lands, the row is in the database, a reload shows it, and the open panel does not move. The type is satisfied, the audit is satisfied, the write path is correct and its tests are green.
|
|
80
|
+
|
|
81
|
+
So while `voltro dev` runs, every read is attributed to the query that made it and compared against that query's own `source:`. The finding names the table and what will not happen, once per query per boot.
|
|
82
|
+
|
|
83
|
+
**The design question was never the recording, it was compose-vs-restrict.** Only a read that CONTRIBUTES rows belongs in `source:` — a restricting read re-running on every unrelated write puts every list back on the wire. An app that scanned its own source for this needed two hand-written exceptions to get from a thousand findings to thirty, and a rule needing an exception list on a correct codebase has already spent its attention.
|
|
84
|
+
|
|
85
|
+
Neither exception is a list here. A table reached only through a predicate subquery is narrowing BY CONSTRUCTION — it returns no column to anybody — so it classifies itself off the descriptor, on every app, with nothing to maintain. And the framework's own restricting reads are ours: they are issued below the wrapper, or, where the framework runs APP code to decide access (a row filter's loader), marked at the call site we control instead of at the ones we do not.
|
|
86
|
+
|
|
87
|
+
Deliberately narrow, and each edge is a decision rather than a limitation: it reports what it SAW and never claims a declaration is otherwise complete; a query with no `source:` at all is left alone, because the finding is about an incomplete list and not a missing one; and it says which tables it did not count, so the classification can be checked rather than trusted.
|
|
88
|
+
|
|
89
|
+
Dev only. `voltro serve` installs no sink, which makes every part of it inert — no wrapper, no async-local write, no comparison. `VOLTRO_SOURCE_RECORDER=off` turns it off in dev.
|
|
90
|
+
|
|
91
|
+
`restrictingReads` (`@voltro/runtime`) is the escape for an app helper that resolves access somewhere the framework does not call it. It is a no-op outside a recording session, so it can be left in place.
|
|
92
|
+
|
|
93
|
+
Measured against a running `voltro dev`, not only in tests: a query declaring one table while reading two is reported once across eleven requests, and the correct queries beside it produce nothing.
|
|
94
|
+
|
|
95
|
+
**Eager-loaded relations count, and they were the hole.** `.with({ subTasks: true })` issues no second read — `compileEagerJson` folds the whole spec into ONE round trip — so the loaded table is never a read's own table and never a join. It is a relation NAME on the descriptor, and the recorder resolves it through the relation registry: the target, and for a many-to-many the JUNCTION as well, since a write there changes membership, which is precisely the change a user makes. Nested `with:` recurses against the target's relations, the same walk the compiler does; an unresolvable name yields nothing rather than an invented table, and a throwing target thunk cannot take the request down with it.
|
|
96
|
+
|
|
97
|
+
This was the reported failure's own shape, so the first version of the recorder could not see the case it was built for.
|
|
98
|
+
|
|
99
|
+
Measured against a running `voltro dev` and real `POST /rpc` calls: twelve requests across four queries produced exactly two findings — the two deliberately-incomplete ones. The SAME eager load declared correctly beside them is silent, and so is a query with no `source:` at all. A check that only ever fires is not evidence that it fires for a reason.
|
|
100
|
+
- **@voltro/cli** — `--rollback-key <key>` — a `replace` over `--target api` has the INSTANCE store the target's current rows in its own object storage before deleting them.
|
|
101
|
+
|
|
102
|
+
The direct path already captured beside the bundle. That is useless on this transport, and the reason is the whole point: the process that would roll a transaction back IS the instance, so a capture in the pod's filesystem goes away with exactly the failure it exists for — a deployment lost 240 172 rows to a run whose api pod disappeared nine minutes in. Object storage is durable, is already configured wherever the storage-push export works, and is reachable afterwards from anywhere.
|
|
103
|
+
|
|
104
|
+
Pushed BEFORE the first delete, through the same archive sink the export uses — one sink, not two, because two is how an instance comes to push a bundle and fail to keep one. A failure to store it stops the import with the target untouched.
|
|
105
|
+
|
|
106
|
+
Fail-closed in both directions, deliberately:
|
|
107
|
+
|
|
108
|
+
- asked for and impossible (no storage configured) → **409**, naming the fix. The request is the operator saying "I cannot afford to lose this", and serving them anyway is the one answer that removes their precaution while looking like agreement. - not asked for → the run proceeds and says what it did not keep. A `replace` into a scratch environment is legitimate, and refusing it would push people to the flag that turns the safety off everywhere.
|
|
109
|
+
|
|
110
|
+
The decision is made from the headers alone, before a byte of the bundle is read: a run that cannot take the capture it was asked for must not cost an upload first.
|
|
111
|
+
- **@voltro/database, @voltro/data-transfer, @voltro/cli** — An interrupted `replace` cannot be silent any more.
|
|
112
|
+
|
|
113
|
+
The capture only helps if somebody knows to reach for it, and a half-replaced database is indistinguishable from an empty one FROM THE INSIDE — every table exists, every constraint holds, every query returns nothing without erroring. A deployment served over one for ninety minutes and only found out through an unrelated fingerprint mismatch.
|
|
114
|
+
|
|
115
|
+
So a `replace` writes one row (`_voltro_replace_in_progress`) before the first delete and removes it after the last insert, and finding it at boot is a REFUSAL — on both boot paths, out of one function. The message names how many tables, how long ago, over which transport, and the capture to restore from, with the command spelled out.
|
|
116
|
+
|
|
117
|
+
The row lives in the SAME transaction as the emptying, so it is present exactly when the emptying is: a run that rolls back cleanly takes the marker with it, and a boot over a database nothing happened to is not refused. A completed `replace` clears its own marker and every older one, so the recovery import restores the data and silences the alarm in one command.
|
|
118
|
+
|
|
119
|
+
Nothing expires — a half-replaced database does not become whole with time, so `voltro data clear-replace-marker --yes` is a decision somebody makes.
|
|
120
|
+
|
|
121
|
+
Measured against a live mariadb by killing a run mid-load: the marker is there, the refusal names the capture, and a completing run clears it.
|
|
122
|
+
- **@voltro/data-transfer** — The staged-swap primitive for `replace` — load somewhere else, then swap the content in one short transaction.
|
|
123
|
+
|
|
124
|
+
`--mode replace` empties the target and loads into it inside ONE transaction, held open for the whole network-bound load. A deployment measured nine minutes for 242 950 rows, and the promise that the target is left as it was found rests entirely on a live process being there to roll it back. A promise that rests on the process surviving is a promise about the weather.
|
|
125
|
+
|
|
126
|
+
**The design the plan carried was wrong, and measurably so.** Shadow tables plus a final `RENAME` moves every inbound foreign key WITH the renamed table — postgres 17 by OID, MariaDB 11 and MySQL 8.4 by tracking the rename, including inside MySQL's atomic multi-pair `RENAME TABLE`. After the swap every key points at the table the design then DROPS. The atomicity of the rename, which that design reasoned about carefully, was never the hard part.
|
|
127
|
+
|
|
128
|
+
Keeping the table OBJECTS and swapping the CONTENT has none of that: every constraint stays pointed at the same object, and the long client-driven load moves OUT of the destructive transaction, which then holds only server-side bulk SQL.
|
|
129
|
+
|
|
130
|
+
**Four of five engines need no integrity switch, which inverts what the design assumed.** Three self-referencing rows — `actors.createdBy → actors`, the framework's own pattern — inserted by one `INSERT … SELECT`: postgres, SQLite and SQL Server take all three (they check at STATEMENT end); MariaDB and MySQL answer `ERROR 1452` (they check per ROW) and need the switch their own `emptyTables` already uses. Measured on each, and measured again after: a genuine violation attempted following the swap is still refused on all five, so the suspension does not leak past it.
|
|
131
|
+
|
|
132
|
+
The statement builders refuse a table name outside the framework's identifier class. This module concatenates SQL and its names arrive from a bundle MANIFEST — a file an operator can edit — so the rule applied at declaration is re-asserted where the concatenation happens rather than assumed to have survived the round trip. Staging tables are `_voltro_staging_<t>`, so the boot differ's framework-table asymmetry treats them as ours instead of planning them as user tables somebody forgot to declare.
|
|
133
|
+
|
|
134
|
+
**The importer does not use it yet**, and attempting that integration is what surfaced two blockers worth stating: a staged write would fire a change event (waking reactivity, CDC and the analytics mirror for tables nobody declared), and it would miss the target's column metadata (`encodeRowForSchema` looks the table up by NAME, so a `json()` column would be written unencoded — a wrong value, not an error). Both are tractable; neither is a line-level change. Until then the two protections already shipped — the capture written before the first delete, and the marker that refuses the next boot after an interrupted run — remain what covers the reported outcome.
|
|
135
|
+
- **@voltro/data-transfer, @voltro/cli** — `--mode replace` writes down what it is about to destroy.
|
|
136
|
+
|
|
137
|
+
Before the first delete it exports the target's CURRENT rows — exactly the tables it will empty — as an ordinary bundle beside yours, and says where:
|
|
138
|
+
|
|
139
|
+
rollback capture: 240172 row(s) across 75 table(s) → ./out.rollback-2026-… If this run does not finish, restore with: voltro data import ./out.rollback-… --mode replace
|
|
140
|
+
|
|
141
|
+
It is on disk BEFORE anything is destroyed, so it depends on no transaction and on no process being alive to roll one back. That is the whole point: a deployment lost 240 172 rows to a `replace` whose api pod disappeared nine minutes in, and recovered from an export they had taken twenty minutes earlier out of HABIT. This is that habit as behaviour. It is NOT the fix for the class — the emptying must not become visible until the load stands, which is a rebuild — it is the small half that covers the reported outcome today.
|
|
142
|
+
|
|
143
|
+
**Fail-closed.** A capture that cannot be taken stops the import before it starts, target untouched. A net you believe in and do not have is worse than none: the belief is what stops you taking your own export.
|
|
144
|
+
|
|
145
|
+
The capture is a COMPLETE bundle over a snapshot narrowed to the emptied tables, not a `tables`-scoped one over the whole schema. Same files, different manifest — and the manifest decides whether it can be restored at all, since `replace` refuses a partial bundle for a reason that is exactly false here.
|
|
146
|
+
|
|
147
|
+
`--no-rollback` opts out, `--rollback-dir <path>` relocates it. Only `replace` takes one: `upsert` and `append` destroy nothing.
|
|
148
|
+
|
|
149
|
+
**Not on `--target api`, and it says so.** The capture would live inside the instance — the thing that can go away, which is the failure it exists for. A replace over that transport warns and names the export to take first.
|
|
150
|
+
- **@voltro/database, @voltro/data-transfer** — Staging clones — the load-side half of the staged swap for `replace`.
|
|
151
|
+
|
|
152
|
+
The swap primitive shipped without the importer using it, and two things stood in the way. Both are solved by ONE answer.
|
|
153
|
+
|
|
154
|
+
A typed write resolves its table by NAME: `encodeRowForSchema` and `stampGeneratedId` both look it up in the registry. Writing to `_voltro_staging_notes` therefore found nothing — and the failure mode is not an error, it is a `json()` column written UNENCODED. Separately, every dialect store's insert ends in `routeEvent`, whose reactive guard reads `isTableReactive`, which is `isReactive !== false` — so an UNREGISTERED name counts as reactive, and loading a large bundle into staging would emit an event per row for tables nobody declared.
|
|
155
|
+
|
|
156
|
+
A staging table registered as a CLONE of its target, marked `isReactive: false`, answers both: the columns resolve, and the guard the framework already has returns before the emit. `.nonReactive()` is the documented way to say exactly that, so nothing at the store needed a special case — and it is the clone rather than the absence that makes the load quiet, which is the part worth remembering if this is ever simplified.
|
|
157
|
+
|
|
158
|
+
`registerStagingClones` returns a REQUIRED undo instead of trusting a caller to remember one. `allRegisteredTables()` feeds the declared set, the boot differ and `voltro doctor`, so a clone left in the registry reads as a table the app declares and nobody created. A failure part-way through registers nothing at all.
|
|
159
|
+
|
|
160
|
+
`unregisterTable` is new in `@voltro/database` for this: narrow on purpose. `clearTableRegistry` wipes everything and exists for tests; this removes ONE name a bounded operation owns for its duration.
|
|
161
|
+
|
|
162
|
+
**The precision is recovered, and it is strictly better than what it replaces.** Staging carries no foreign keys — required, since a staged row whose parent has not been staged yet must not be refused — so a violation moves from load time to swap time, where the database answers with one message naming a constraint. `stagedReferences` reads the edges INSIDE the replaced set off the snapshot the importer already holds (deliberately not `incomingForeignKeys`, which answers the already-answered question of keys pointing in from OUTSIDE), and `danglingProbeSql` asks STAGING the question the database was asking — against the STAGED parent set, because the swap inserts parents from staging and what matters is whether the reference resolves AFTER it.
|
|
163
|
+
|
|
164
|
+
Measured against a live postgres with four staged children — one good, one NULL, two dangling — driven through a real failing swap: the database named `ghost`; the probe named `ghost` AND `phantom`, skipping the NULL and the good row. The row-by-row path stops at the FIRST failure, so a bundle with four bad references costs four round trips; this reports all of them in one pass. The target was verified unchanged afterwards, which is the first thing the message says.
|
|
165
|
+
|
|
166
|
+
**The importer uses it now.** `--mode replace` stages when it can: create a staging table per table, load into those OUTSIDE any transaction, then swap the content across in one short transaction of server-side SQL. A process that dies during the load leaves the target exactly as it was, because nothing has been deleted yet — the destructive window shrinks from the length of the load to the length of a copy.
|
|
167
|
+
|
|
168
|
+
Wiring it surfaced two more things, and neither was visible from the design.
|
|
169
|
+
|
|
170
|
+
`INSERT … SELECT *` fails the moment the target has a STORED generated column — `CREATE TABLE … (LIKE t)` copies such a column as a PLAIN one (measured: `is_generated: NEVER`), so the select hands the target a value for a column it computes itself: `cannot insert a non-DEFAULT value into column "slug"`. The swap names its columns now, minus the generated ones, and `swapStatements` REFUSES an empty column list rather than falling back to `SELECT *`, so the trap cannot return by omission.
|
|
171
|
+
|
|
172
|
+
And a WRITE RECORDER on any table in the set rules staging out. A recorder is keyed by table NAME, so a staged write looks up `_voltro_staging_notes`, finds none, and never runs — `versioningPlugin({ timing: 'in-transaction' })` promises "if the change committed, the entry is there", and the swap's bulk SQL has no per-row hook to keep that with. Measured, not guessed: the recorder went from more than one call to zero. Such a run keeps the path that honours it and SAYS why, as does a `--no-atomic` run or a store the framework cannot send DDL to.
|
|
173
|
+
|
|
174
|
+
Every staged run says it staged. The two paths are indistinguishable from outside — both end with the target holding the bundle — and an operator deciding whether they can afford to interrupt needs to know which one is running.
|
|
175
|
+
|
|
176
|
+
Verified through the real importer, and falsified before being kept: with staging off, the assertion that the target still holds its own rows WHILE the bundle loads goes red. On real postgres the staged path runs the existing foreign-key replace suite unchanged.
|
|
177
|
+
|
|
178
|
+
### Fixed
|
|
179
|
+
|
|
180
|
+
- **@voltro/protocol, @voltro/voltro** — A `_voltro_*` name in `source:` is no longer narrowed against the generated table declaration.
|
|
181
|
+
|
|
182
|
+
Which framework tables an app declares is DEPLOYMENT-dependent. The measurement is already in the maintainer notes: at one `NODE_ENV`, on one dialect, flipping a single flag adds or removes `_voltro_traces`, `_voltro_undo_log` or `_voltro_cdc_offsets` from the declared set. The generated `voltro-tables.generated.d.ts` is written by ONE `voltro dev` run, on one machine, with one set of those inputs.
|
|
183
|
+
|
|
184
|
+
Narrowing framework names against it therefore made `source: '_voltro_traces'` compile for whoever generated the file and fail for a colleague — a type error decided by an environment variable, which is the exact class the declared-schema rule forbids one layer up. An app's own tables are unaffected: `_voltro_` is a reserved prefix, so every name a user writes for their own data narrows exactly as before.
|
|
185
|
+
|
|
186
|
+
**How it surfaced is the part worth recording.** Until now `keyof VoltroTableNames` was always `never` inside this repo, so `TableName` was always `string`, so the narrow and wide types were the same type and every rule about them held vacuously. The first time an augmentation was ever present — a fixture that boots a real server writing the declaration beside its generated rpc group — four framework source files stopped compiling. The split had shipped without once being exercised in the direction that matters.
|
|
187
|
+
|
|
188
|
+
Two things now stop that from going quiet again. A type-test program compiles the framework's own sources under an augmentation that deliberately declares NONE of its tables, and it lives in its OWN tsconfig: `declare module` merging is program-global, so a sibling type test's augmentation had silently rescued the very assertion this one exists to make. And `scripts/check-type-tests.mjs` (CI + `pnpm gate`) DISCOVERS type-test programs and runs them — because nothing did. The existing narrowing assertions had never been compiled once: excluded from their package's tsconfig for a good reason, and picked up by nothing else. It refuses a zero-program run, and a program that compiles zero `*.test-d.ts` files, for the same reason every other check here carries a floor.
|
|
189
|
+
|
|
190
|
+
**Why `apiSurface: compatible`, and how to check it rather than take it.** The gate flagged the golden line as CHANGED and asked the right question — can this turn code that compiled into code that does not? Here it cannot, because the edit WIDENS a union, and every public position the type appears in is an INPUT: `source?:` on a descriptor, and `normalizeSource`'s parameter. Nothing in the published surface RETURNS `TableName` or `ReactivitySource`, which is the only direction in which widening breaks a consumer — an assignment FROM the type into something narrower. `@voltro/voltro` is listed beside `@voltro/protocol` because it re-exports the type, so its golden moved too; the gate matches per package, and one package's classification must not vouch for another's.
|
|
191
|
+
- **@voltro/sql-mssql** — A `json()` column could not take a value on mssql. At all.
|
|
192
|
+
|
|
193
|
+
store.insert(t, { payload: { a: 1 } }) -> TypeError: Invalid string. [EPARAM] store.insert(t, { payload: null }) -> OK
|
|
194
|
+
|
|
195
|
+
So the column worked only while it held nothing. Reached first through `@voltro/plugin-versioning`, whose history row carries a full-row snapshot in exactly such a column: the versioning recorder could not write on that dialect, and therefore neither could any write to a table it covers.
|
|
196
|
+
|
|
197
|
+
The store handed the caller's row straight to `sql.insert(row)`. The mysql store runs `encodeRowForSchema` first — the schema-driven step that turns a json value into text a driver can bind — and mssql had no equivalent anywhere on its write path, so tedious received a JS object for an NVARCHAR parameter and refused it.
|
|
198
|
+
|
|
199
|
+
**Encoding alone would have been worse than the bug.** A value written as text and handed back as text means a WRITE returns a string where a READ of the same row returns an object, and nothing errors — the caller gets a different type depending on how it got there. So every `OUTPUT INSERTED.*` path decodes too (insert, insertMany, update, updateMany, patchJson, the delete old-image, and the MERGE upsert), and the test asserts the ROUND TRIP rather than the absence of an error, against a live SQL Server.
|
|
200
|
+
|
|
201
|
+
With it, the mssql case is back in the versioning key-length suite — the bound on `id()` exists on that dialect too, and it was absent for one release only because nothing could write there.
|
|
202
|
+
- **@voltro/data-transfer, @voltro/cli** — A `scope: all` bundle carried the exporting deployment's own bookkeeping, and `replace` wrote it into the target. The target's next boot refused to start:
|
|
203
|
+
|
|
204
|
+
auto-migrate: SCHEMA FINGERPRINT MISMATCH — declared=6e2c61081a9ed80c live=28af9a54414f22f1
|
|
205
|
+
|
|
206
|
+
The refusal was correct and the row was the defect. A migration-ledger row is not DATA — it states which schema THIS deployment applied — and the fingerprint is computed over the declared table set, which legitimately differs per environment (`NODE_ENV=production` declares `_voltro_traces` and `_voltro_undo_log`; a `development` run does not). So the imported row was not stale, it was FOREIGN. The environment was down for ninety minutes, and the ledger row also HID the incident it travelled with: the api would not start, and the reason looked like the failed import rather than a row in a bookkeeping table.
|
|
207
|
+
|
|
208
|
+
Ten framework tables are now classified as environment-local — the migration ledger, the file-migration and seed records, CDC offsets, schedule claims, wakeups, workflow watermarks / pending starts / admissions / pauses. They are dropped from an export's `all` scope, skipped on import, and never emptied by a `replace`, each with the reason a foreign row would be wrong stated beside it. Two of them would have made the target ACT: a pending start runs a workflow somebody queued elsewhere, a pause silently stops one here.
|
|
209
|
+
|
|
210
|
+
`all` is the only scope filtered. A caller who NAMES one of these tables gets it — an explicit name is an expectation, and this module already refuses to drop those silently.
|
|
211
|
+
|
|
212
|
+
The classification refuses to be incomplete: a guard scans every framework table and fails until a new one is decided either way. A hand-list rots by omission, and the omission cost ninety minutes.
|
|
213
|
+
|
|
214
|
+
Also fixed, found while reading that path: the admin import endpoint spread `atomic` only when truthy, so an explicit `atomic: false` was dropped and the importer applied its own default — which for `replace` is `true`. The one value a caller can only express by asking for it was the one the wire discarded.
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
42
218
|
## [0.45.0] — 2026-08-21
|
|
43
219
|
|
|
44
220
|
### ⚠ BREAKING
|
package/dist/index.d.ts
CHANGED
|
@@ -186,6 +186,20 @@ export declare class MssqlStore implements DataStore {
|
|
|
186
186
|
*/
|
|
187
187
|
readonly __mssqlReplicationFriend: MssqlAdapterFriend;
|
|
188
188
|
private executeQuery;
|
|
189
|
+
/**
|
|
190
|
+
* The row a write RETURNS, decoded like any read.
|
|
191
|
+
*
|
|
192
|
+
* The encode/decode pair has to be complete or it is worse than neither: a
|
|
193
|
+
* `json()` value written as text and handed back as text means a write returns
|
|
194
|
+
* a string where a read of the same row returns an object, and nothing errors
|
|
195
|
+
* — the caller just gets a different type depending on how it got there.
|
|
196
|
+
*
|
|
197
|
+
* This store had the decode on `query` alone and no encode at all, which is
|
|
198
|
+
* why a json column could not take a value on this dialect: the driver was
|
|
199
|
+
* handed a JS object and refused it (`TypeError: Invalid string [EPARAM]`).
|
|
200
|
+
* Every `OUTPUT INSERTED.*` path goes through here now.
|
|
201
|
+
*/
|
|
202
|
+
private decodeWritten;
|
|
189
203
|
private executeInsert;
|
|
190
204
|
private executeUpdate;
|
|
191
205
|
private executeInsertMany;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { MssqlClient as e } from "@effect/sql-mssql";
|
|
2
2
|
import { Config as t, Duration as n, Effect as r, Layer as i, ManagedRuntime as a, Option as o, Redacted as s } from "effect";
|
|
3
|
-
import { CDC_OFFSETS_TABLE as c, DEFAULT_ACQUIRE_TIMEOUT_MS as l, EagerCardinalityError as u, _voltroMssqlCdcOffsetsTable as d, attachEagerLoads as f, attributionFields as p, bulkInsertLimitsFor as m, chunkRowsForInsert as h, compileEagerJson as g, compilePredicate as _, compileRawFragment as v, compileSelect as y, decodeRowsFromSchema as b, externalChangeEvent as
|
|
4
|
-
import { EventEmitter as
|
|
5
|
-
import { createLogger as
|
|
6
|
-
import { SqlClient as
|
|
3
|
+
import { CDC_OFFSETS_TABLE as c, DEFAULT_ACQUIRE_TIMEOUT_MS as l, EagerCardinalityError as u, _voltroMssqlCdcOffsetsTable as d, attachEagerLoads as f, attributionFields as p, bulkInsertLimitsFor as m, chunkRowsForInsert as h, compileEagerJson as g, compilePredicate as _, compileRawFragment as v, compileSelect as y, decodeRowsFromSchema as b, encodeRowForSchema as x, externalChangeEvent as S, hasEagerLoads as C, isTableReactive as w, makeEagerFallbackReporter as ee, observeDbOp as T, qualifyTable as E, quoteIdent as te, raiseChangeListenerCeiling as ne, recordsTable as D, requireTable as re, runStoreTransaction as ie, runWriteRecorders as O, stampGeneratedId as k, stampGeneratedIds as A, withCapturedAttribution as j } from "@voltro/database";
|
|
4
|
+
import { EventEmitter as M } from "node:events";
|
|
5
|
+
import { createLogger as N } from "@voltro/logger";
|
|
6
|
+
import { SqlClient as P, TransactionConnection as F } from "@effect/sql/SqlClient";
|
|
7
7
|
//#region src/sqlLayer.ts
|
|
8
8
|
var ae = {
|
|
9
9
|
...e.defaultParameterTypes,
|
|
@@ -11,7 +11,7 @@ var ae = {
|
|
|
11
11
|
}, oe = (e) => {
|
|
12
12
|
let t = e.acquireTimeoutMs ?? l;
|
|
13
13
|
return t > 0 ? t : void 0;
|
|
14
|
-
},
|
|
14
|
+
}, I = (r) => {
|
|
15
15
|
let i = oe(r);
|
|
16
16
|
return e.layerConfig({
|
|
17
17
|
server: t.succeed(r.server),
|
|
@@ -25,7 +25,7 @@ var ae = {
|
|
|
25
25
|
...r.trustServer === void 0 ? {} : { trustServer: t.succeed(r.trustServer) },
|
|
26
26
|
...r.maxConnections === void 0 ? {} : { maxConnections: t.succeed(r.maxConnections) }
|
|
27
27
|
});
|
|
28
|
-
},
|
|
28
|
+
}, se = (e) => {
|
|
29
29
|
let t = e.get("sslmode");
|
|
30
30
|
if (t !== null) {
|
|
31
31
|
if (t === "require") return !0;
|
|
@@ -53,7 +53,7 @@ var ae = {
|
|
|
53
53
|
username: decodeURIComponent(r.username || "sa"),
|
|
54
54
|
password: decodeURIComponent(r.password || ""),
|
|
55
55
|
database: r.pathname.replace(/^\//, "") || "master",
|
|
56
|
-
...n(e.ssl ??
|
|
56
|
+
...n(e.ssl ?? se(r.searchParams)),
|
|
57
57
|
...e.maxConnections === void 0 ? {} : { maxConnections: e.maxConnections },
|
|
58
58
|
...t
|
|
59
59
|
};
|
|
@@ -68,7 +68,7 @@ var ae = {
|
|
|
68
68
|
...e.maxConnections === void 0 ? {} : { maxConnections: e.maxConnections },
|
|
69
69
|
...t
|
|
70
70
|
};
|
|
71
|
-
}, R = (e) =>
|
|
71
|
+
}, R = (e) => I(L(e)), z = /* @__PURE__ */ new Set(["1205"]), B = (e) => {
|
|
72
72
|
let t = e;
|
|
73
73
|
for (let e = 0; e < 5 && typeof t == "object" && t; e++) {
|
|
74
74
|
let e = t.number;
|
|
@@ -88,7 +88,7 @@ var ae = {
|
|
|
88
88
|
U: "update",
|
|
89
89
|
D: "delete"
|
|
90
90
|
}, K = (e, t) => e(r.gen(function* () {
|
|
91
|
-
let e = yield*
|
|
91
|
+
let e = yield* P;
|
|
92
92
|
if (((yield* e`
|
|
93
93
|
SELECT COUNT(*) AS ${e("on")}
|
|
94
94
|
FROM sys.change_tracking_databases
|
|
@@ -99,13 +99,13 @@ var ae = {
|
|
|
99
99
|
)
|
|
100
100
|
ALTER TABLE ${J(n)} ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = OFF)`);
|
|
101
101
|
})).then(() => void 0), q = (e) => `'${e.replace(/'/g, "''")}'`, J = (e) => `[${e.replace(/\]/g, "]]")}]`, Y = async (e) => {
|
|
102
|
-
let t =
|
|
102
|
+
let t = N({ scope: "voltro:mssql:cdc" }), n = e.pollIntervalMs ?? 500;
|
|
103
103
|
await K(e.run, e.tables);
|
|
104
104
|
let i = async () => (await e.run(r.gen(function* () {
|
|
105
|
-
return yield* (yield*
|
|
105
|
+
return yield* (yield* P)`SELECT CONVERT(VARCHAR(40), CHANGE_TRACKING_CURRENT_VERSION()) AS v`;
|
|
106
106
|
})))[0]?.v ?? "0", a = e.startVersion ?? await i(), o = !1, s = null, c = !1, l = (e, t) => BigInt(e) > BigInt(t), u = async (n, o) => {
|
|
107
107
|
let s = await e.run(r.gen(function* () {
|
|
108
|
-
let e = yield*
|
|
108
|
+
let e = yield* P, t = e.unsafe(J(n)), r = (yield* e`
|
|
109
109
|
SELECT CONVERT(VARCHAR(40), CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID(${e.unsafe(q(n))}))) AS floor`)[0]?.floor;
|
|
110
110
|
return r != null && BigInt(o) < BigInt(r) ? {
|
|
111
111
|
resync: !0,
|
|
@@ -175,26 +175,26 @@ var ae = {
|
|
|
175
175
|
},
|
|
176
176
|
currentVersion: () => a
|
|
177
177
|
};
|
|
178
|
-
}, X =
|
|
178
|
+
}, X = N({ scope: "voltro:mssql" }), Z = async (e) => {
|
|
179
179
|
let t = e.changeStrategy ?? "inline", n = t;
|
|
180
180
|
t === "cdc" && !e.cdcConfig && (X.warn("changeStrategy='cdc' requires cdcConfig (replicaId + includeTables); falling back to 'inline'."), n = "inline");
|
|
181
|
-
let r = e.tracerLayer ? i.mergeAll(e.sqlLayer, e.tracerLayer) : e.sqlLayer, o = a.make(r), s = new
|
|
181
|
+
let r = e.tracerLayer ? i.mergeAll(e.sqlLayer, e.tracerLayer) : e.sqlLayer, o = a.make(r), s = new ce(await o.runPromise(P), o, n);
|
|
182
182
|
return n === "cdc" && e.cdcConfig && await s.startCdcConsumer(e.cdcConfig), s;
|
|
183
|
-
},
|
|
183
|
+
}, ce = class e {
|
|
184
184
|
sql;
|
|
185
185
|
runtime;
|
|
186
186
|
changeStrategy;
|
|
187
187
|
namespace;
|
|
188
188
|
emitter;
|
|
189
189
|
inflightTxns = 0;
|
|
190
|
-
reportEagerFallback =
|
|
190
|
+
reportEagerFallback = ee(X);
|
|
191
191
|
cdcHandle = null;
|
|
192
192
|
cdcCheckpointTimer = null;
|
|
193
193
|
cdcPendingVersion = null;
|
|
194
194
|
cdcReplicaId = "";
|
|
195
195
|
cdcStreamName = "default";
|
|
196
196
|
constructor(e, t, n = "inline", r = null, i) {
|
|
197
|
-
this.sql = e, this.runtime = t, this.changeStrategy = n, this.namespace = r, this.emitter = i ?? new
|
|
197
|
+
this.sql = e, this.runtime = t, this.changeStrategy = n, this.namespace = r, this.emitter = i ?? new M(), ne(this.emitter);
|
|
198
198
|
}
|
|
199
199
|
get inlineEmit() {
|
|
200
200
|
return this.changeStrategy === "inline";
|
|
@@ -203,45 +203,51 @@ var ae = {
|
|
|
203
203
|
return t === this.namespace ? this : new e(this.sql, this.runtime, this.changeStrategy, t, this.emitter);
|
|
204
204
|
}
|
|
205
205
|
nsT(e) {
|
|
206
|
-
return
|
|
206
|
+
return E(this.namespace, e);
|
|
207
207
|
}
|
|
208
208
|
__mssqlReplicationFriend = { runEffect: (e) => this.runtime.runPromise(e) };
|
|
209
209
|
async executeQuery(e, t) {
|
|
210
|
-
let n = y(e, this.sql, this.namespace), i = t ? r.provideService(n,
|
|
210
|
+
let n = y(e, this.sql, this.namespace), i = t ? r.provideService(n, F, t) : n, a = await T("mssql", "select", () => this.runtime.runPromise(i));
|
|
211
211
|
return b(a, e.table, "mssql");
|
|
212
212
|
}
|
|
213
|
+
decodeWritten(e, t) {
|
|
214
|
+
return b([t], e, "mssql")[0] ?? t;
|
|
215
|
+
}
|
|
213
216
|
async executeInsert(e, t, n, i, a) {
|
|
214
|
-
t =
|
|
215
|
-
let o = this.sql, s = o`INSERT INTO ${o(this.nsT(e))} ${o.insert(t).returning("*")}`, c = n ? r.provideService(s,
|
|
217
|
+
t = k(e, t);
|
|
218
|
+
let o = this.sql, s = o`INSERT INTO ${o(this.nsT(e))} ${o.insert(x(t, e)).returning("*")}`, c = n ? r.provideService(s, F, n) : s, l = (await this.runtime.runPromise(c))[0];
|
|
216
219
|
if (!l) throw Error(`MssqlStore.insert: row not returned post-insert in '${e}'`);
|
|
220
|
+
let u = this.decodeWritten(e, l);
|
|
217
221
|
return await this.routeEvent({
|
|
218
222
|
table: e,
|
|
219
223
|
op: "insert",
|
|
220
224
|
old: null,
|
|
221
|
-
new:
|
|
222
|
-
}, i, n, a),
|
|
225
|
+
new: u
|
|
226
|
+
}, i, n, a), u;
|
|
223
227
|
}
|
|
224
228
|
async executeUpdate(e, t, n, i, a, o) {
|
|
225
|
-
let s = this.sql, c = s`UPDATE ${s(this.nsT(e))} SET ${s.update(n).returning("*")} WHERE ${s("id")} = ${t}`, l = i ? r.provideService(c,
|
|
226
|
-
|
|
229
|
+
let s = this.sql, c = s`UPDATE ${s(this.nsT(e))} SET ${s.update(x(n, e)).returning("*")} WHERE ${s("id")} = ${t}`, l = i ? r.provideService(c, F, i) : c, u = (await this.runtime.runPromise(l))[0];
|
|
230
|
+
if (!u) return null;
|
|
231
|
+
let d = this.decodeWritten(e, u);
|
|
232
|
+
return await this.routeEvent({
|
|
227
233
|
table: e,
|
|
228
234
|
op: "update",
|
|
229
235
|
old: null,
|
|
230
|
-
new:
|
|
231
|
-
}, a, i, o),
|
|
236
|
+
new: d
|
|
237
|
+
}, a, i, o), d;
|
|
232
238
|
}
|
|
233
239
|
async executeInsertMany(e, t, n, i, a) {
|
|
234
|
-
if (t =
|
|
235
|
-
let o = this.sql, s = h(t, m("mssql")), c = (t) => o`INSERT INTO ${o(this.nsT(e))} ${o.insert(t).returning("*")}`, l;
|
|
240
|
+
if (t = A(e, t), t.length === 0) return [];
|
|
241
|
+
let o = this.sql, s = h(t, m("mssql")), c = (t) => o`INSERT INTO ${o(this.nsT(e))} ${o.insert(t.map((t) => x(t, e))).returning("*")}`, l;
|
|
236
242
|
if (s.length === 1) {
|
|
237
|
-
let e = c(s[0]), t = n ? r.provideService(e,
|
|
243
|
+
let e = c(s[0]), t = n ? r.provideService(e, F, n) : e;
|
|
238
244
|
l = await this.runtime.runPromise(t);
|
|
239
245
|
} else if (n) {
|
|
240
246
|
let e = [];
|
|
241
|
-
for (let t of s) e.push(...await this.runtime.runPromise(r.provideService(c(t),
|
|
247
|
+
for (let t of s) e.push(...await this.runtime.runPromise(r.provideService(c(t), F, n)));
|
|
242
248
|
l = e;
|
|
243
249
|
} else l = await this.runtime.runPromise(o.withTransaction(r.map(r.forEach(s, c, { concurrency: 1 }), (e) => e.flat())));
|
|
244
|
-
let u = t.map((e) => e.id), d = new Map(l.map((e) => [e.id, e])), f = u.map((e) => d.get(e)).filter((e) => e !== void 0), p = f.length === l.length ? f : [...l];
|
|
250
|
+
let u = t.map((e) => e.id), d = new Map(b(l, e, "mssql").map((e) => [e.id, e])), f = u.map((e) => d.get(e)).filter((e) => e !== void 0), p = f.length === l.length ? f : [...l];
|
|
245
251
|
for (let t of p) await this.routeEvent({
|
|
246
252
|
table: e,
|
|
247
253
|
op: "insert",
|
|
@@ -251,7 +257,7 @@ var ae = {
|
|
|
251
257
|
return p;
|
|
252
258
|
}
|
|
253
259
|
async executePatchJson(e, t, n, i, a, o, s) {
|
|
254
|
-
let c = this.sql, l = n.split("."), u = l[0], d = l.slice(1), f = d.length === 0 ? "$" : `$.${d.join(".")}`, p = JSON.stringify(i ?? null), m = c`UPDATE ${c(this.nsT(e))} SET ${c(u)} = JSON_MODIFY(${c(u)}, ${f}, JSON_QUERY(${p})) OUTPUT INSERTED.* WHERE ${c("id")} = ${t}`, h = a ? r.provideService(m,
|
|
260
|
+
let c = this.sql, l = n.split("."), u = l[0], d = l.slice(1), f = d.length === 0 ? "$" : `$.${d.join(".")}`, p = JSON.stringify(i ?? null), m = c`UPDATE ${c(this.nsT(e))} SET ${c(u)} = JSON_MODIFY(${c(u)}, ${f}, JSON_QUERY(${p})) OUTPUT INSERTED.* WHERE ${c("id")} = ${t}`, h = a ? r.provideService(m, F, a) : m, g = (await this.runtime.runPromise(h))[0];
|
|
255
261
|
return g ? (await this.routeEvent({
|
|
256
262
|
table: e,
|
|
257
263
|
op: "update",
|
|
@@ -260,27 +266,29 @@ var ae = {
|
|
|
260
266
|
}, o, a, s), g) : null;
|
|
261
267
|
}
|
|
262
268
|
async executeDelete(e, t, n, i, a) {
|
|
263
|
-
let o = this.sql, s = o`DELETE FROM ${o(this.nsT(e))} OUTPUT DELETED.* WHERE ${o("id")} = ${t}`, c = n ? r.provideService(s,
|
|
264
|
-
|
|
269
|
+
let o = this.sql, s = o`DELETE FROM ${o(this.nsT(e))} OUTPUT DELETED.* WHERE ${o("id")} = ${t}`, c = n ? r.provideService(s, F, n) : s, l = (await this.runtime.runPromise(c))[0];
|
|
270
|
+
if (!l) return !1;
|
|
271
|
+
let u = this.decodeWritten(e, l);
|
|
272
|
+
return await this.routeEvent({
|
|
265
273
|
table: e,
|
|
266
274
|
op: "delete",
|
|
267
|
-
old:
|
|
275
|
+
old: u,
|
|
268
276
|
new: null
|
|
269
|
-
}, i, n, a), !0
|
|
277
|
+
}, i, n, a), !0;
|
|
270
278
|
}
|
|
271
279
|
async appendInTxn(e, t, n) {
|
|
272
|
-
let i = this.sql, a = i`INSERT INTO ${i(this.nsT(e))} ${i.insert(t)}`;
|
|
273
|
-
await this.runtime.runPromise(n ? r.provideService(a,
|
|
280
|
+
let i = this.sql, a = i`INSERT INTO ${i(this.nsT(e))} ${i.insert(x(t, e))}`;
|
|
281
|
+
await this.runtime.runPromise(n ? r.provideService(a, F, n) : a);
|
|
274
282
|
}
|
|
275
283
|
async maxInTxn(e, t, n, i) {
|
|
276
|
-
let a = this.sql, o = Object.entries(n).map(([e, t]) => a`${a(e)} = ${t}`), s = a`SELECT MAX(${a(t)}) AS ${a("m")} FROM ${a(this.nsT(e))} WHERE ${a.and(o)}`, c = (await this.runtime.runPromise(i ? r.provideService(s,
|
|
284
|
+
let a = this.sql, o = Object.entries(n).map(([e, t]) => a`${a(e)} = ${t}`), s = a`SELECT MAX(${a(t)}) AS ${a("m")} FROM ${a(this.nsT(e))} WHERE ${a.and(o)}`, c = (await this.runtime.runPromise(i ? r.provideService(s, F, i) : s))[0]?.m;
|
|
277
285
|
return c == null ? null : Number(c);
|
|
278
286
|
}
|
|
279
287
|
async routeEvent(e, t, n = null, r) {
|
|
280
288
|
e = {
|
|
281
289
|
...p(r),
|
|
282
290
|
...e
|
|
283
|
-
},
|
|
291
|
+
}, D(e.table) && await O({
|
|
284
292
|
append: (e, t) => this.appendInTxn(e, t, n),
|
|
285
293
|
maxOf: (e, t, r) => this.maxInTxn(e, t, r, n)
|
|
286
294
|
}, {
|
|
@@ -290,20 +298,20 @@ var ae = {
|
|
|
290
298
|
prev: e.old,
|
|
291
299
|
traceId: e.traceId,
|
|
292
300
|
subjectId: e.subjectId
|
|
293
|
-
}),
|
|
301
|
+
}), w(e.table) && (t === null ? this.inlineEmit && this.emitter.emit("change", e) : t.push(e));
|
|
294
302
|
}
|
|
295
303
|
query(e) {
|
|
296
304
|
return this.runWithEager(e, null);
|
|
297
305
|
}
|
|
298
306
|
raw(e, t) {
|
|
299
307
|
let n = v(e, this.sql);
|
|
300
|
-
return
|
|
308
|
+
return T("mssql", "raw", () => this.runtime.runPromise(n));
|
|
301
309
|
}
|
|
302
310
|
async runWithEager(e, t) {
|
|
303
|
-
if (!
|
|
311
|
+
if (!C(e)) return this.executeQuery(e, t);
|
|
304
312
|
let n = this.namespace === null ? g(e, this.sql, "mssql") : null;
|
|
305
313
|
if (n !== null) try {
|
|
306
|
-
let e = t ? r.provideService(n.fragment,
|
|
314
|
+
let e = t ? r.provideService(n.fragment, F, t) : n.fragment, i = await T("mssql", "select", () => this.runtime.runPromise(e));
|
|
307
315
|
return n.decode(i);
|
|
308
316
|
} catch (t) {
|
|
309
317
|
if (t instanceof u) throw t;
|
|
@@ -320,17 +328,17 @@ var ae = {
|
|
|
320
328
|
reason: "not-compilable"
|
|
321
329
|
});
|
|
322
330
|
let i = await this.executeQuery(e, t);
|
|
323
|
-
return f(i, e.eager, e.sourceTable ??
|
|
331
|
+
return f(i, e.eager, e.sourceTable ?? re(e.table), (e) => this.executeQuery(e, t));
|
|
324
332
|
}
|
|
325
333
|
getInternalRunWithEager() {
|
|
326
334
|
return this.runWithEager.bind(this);
|
|
327
335
|
}
|
|
328
336
|
localWrite(e, t, n) {
|
|
329
|
-
let r = (e) =>
|
|
330
|
-
return
|
|
337
|
+
let r = (e) => D(t) ? this.writeWithRecorders(n, e) : n(e, null, null);
|
|
338
|
+
return T("mssql", e, () => j(r));
|
|
331
339
|
}
|
|
332
340
|
async writeWithRecorders(e, t) {
|
|
333
|
-
let n = [], i = this.sql, a = await this.runtime.runPromise(i.withTransaction(r.flatMap(r.serviceOption(
|
|
341
|
+
let n = [], i = this.sql, a = await this.runtime.runPromise(i.withTransaction(r.flatMap(r.serviceOption(F), (i) => o.isNone(i) ? r.fail(/* @__PURE__ */ Error("MssqlStore.write: TransactionConnection missing.")) : r.tryPromise({
|
|
334
342
|
try: () => e(t, i.value, n),
|
|
335
343
|
catch: (e) => e
|
|
336
344
|
}))));
|
|
@@ -353,7 +361,7 @@ var ae = {
|
|
|
353
361
|
return this.localWrite("delete", e, (n, r, i) => this.executeDelete(e, t, r, i, n));
|
|
354
362
|
}
|
|
355
363
|
async updateMany(e, t, n) {
|
|
356
|
-
let r = this.sql, i = _(n.where, r, this.namespace), a = r`UPDATE ${r(this.nsT(e))} SET ${r.update(t)} OUTPUT INSERTED.* WHERE ${i}`, o = await
|
|
364
|
+
let r = this.sql, i = _(n.where, r, this.namespace), a = r`UPDATE ${r(this.nsT(e))} SET ${r.update(x(t, e))} OUTPUT INSERTED.* WHERE ${i}`, o = await T("mssql", "update", () => this.runtime.runPromise(a));
|
|
357
365
|
for (let t of o) await this.routeEvent({
|
|
358
366
|
table: e,
|
|
359
367
|
op: "update",
|
|
@@ -363,7 +371,7 @@ var ae = {
|
|
|
363
371
|
return o.length;
|
|
364
372
|
}
|
|
365
373
|
async deleteMany(e, t) {
|
|
366
|
-
let n = this.sql, r = _(t.where, n, this.namespace), i = n`DELETE FROM ${n(this.nsT(e))} OUTPUT DELETED.* WHERE ${r}`, a = await
|
|
374
|
+
let n = this.sql, r = _(t.where, n, this.namespace), i = n`DELETE FROM ${n(this.nsT(e))} OUTPUT DELETED.* WHERE ${r}`, a = await T("mssql", "delete", () => this.runtime.runPromise(i));
|
|
367
375
|
for (let t of a) await this.routeEvent({
|
|
368
376
|
table: e,
|
|
369
377
|
op: "delete",
|
|
@@ -406,24 +414,24 @@ var ae = {
|
|
|
406
414
|
}
|
|
407
415
|
}
|
|
408
416
|
async mergeUpsert(e, t, n, i, a) {
|
|
409
|
-
let o = this.sql, s = Object.keys(t), c = Array.isArray(n.update) ? n.update.filter((e) => s.includes(e)) : s.filter((e) => e !== "id" && !n.conflictColumns.includes(e)), l = s.map((e) => o`${
|
|
417
|
+
let o = this.sql, s = Object.keys(t), c = Array.isArray(n.update) ? n.update.filter((e) => s.includes(e)) : s.filter((e) => e !== "id" && !n.conflictColumns.includes(e)), l = x(t, e), u = s.map((e) => o`${l[e]}`), d = s.map((e) => o`${o(e)}`), f = n.conflictColumns.map((e) => o`tgt.${o(e)} = src.${o(e)}`), p = s.map((e) => o`${o(e)}`), m = s.map((e) => o`src.${o(e)}`), h = (c.length > 0 ? c : [n.conflictColumns[0]]).map((e) => o`tgt.${o(e)} = src.${o(e)}`), g = o`
|
|
410
418
|
MERGE ${o(this.nsT(e))} WITH (HOLDLOCK) AS tgt
|
|
411
|
-
USING (VALUES (${o.csv(
|
|
412
|
-
ON ${o.and(
|
|
413
|
-
WHEN MATCHED THEN UPDATE SET ${o.csv(
|
|
414
|
-
WHEN NOT MATCHED THEN INSERT (${o.csv(
|
|
415
|
-
OUTPUT $action AS ${o("__action")}, INSERTED.*;`,
|
|
416
|
-
if (!
|
|
417
|
-
let
|
|
419
|
+
USING (VALUES (${o.csv(u)})) AS src (${o.csv(d)})
|
|
420
|
+
ON ${o.and(f)}
|
|
421
|
+
WHEN MATCHED THEN UPDATE SET ${o.csv(h)}
|
|
422
|
+
WHEN NOT MATCHED THEN INSERT (${o.csv(p)}) VALUES (${o.csv(m)})
|
|
423
|
+
OUTPUT $action AS ${o("__action")}, INSERTED.*;`, _ = i ? r.provideService(g, F, i) : g, v = (await this.runtime.runPromise(_))[0];
|
|
424
|
+
if (!v) throw Error(`MssqlStore.upsert: MERGE returned no row for '${e}'`);
|
|
425
|
+
let y = v.__action, { __action: b, ...S } = v, C = this.decodeWritten(e, S);
|
|
418
426
|
return this.routeEvent({
|
|
419
427
|
table: e,
|
|
420
|
-
op:
|
|
428
|
+
op: y === "INSERT" ? "insert" : "update",
|
|
421
429
|
old: null,
|
|
422
|
-
new:
|
|
423
|
-
}, a),
|
|
430
|
+
new: C
|
|
431
|
+
}, a), C;
|
|
424
432
|
}
|
|
425
433
|
async executeInsertIgnore(e, t, n, r, i, a) {
|
|
426
|
-
t =
|
|
434
|
+
t = k(e, t);
|
|
427
435
|
let o = await this.findByConflict(e, t, n.conflictColumns, r);
|
|
428
436
|
if (o) return o;
|
|
429
437
|
try {
|
|
@@ -437,7 +445,7 @@ var ae = {
|
|
|
437
445
|
}
|
|
438
446
|
async findByConflict(e, t, n, i) {
|
|
439
447
|
if (n.length === 0) return;
|
|
440
|
-
let a = this.sql, o = n.map((e) => a`${a(e)} = ${t[e]}`), s = a`SELECT TOP 1 * FROM ${a(this.nsT(e))} WHERE ${a.and(o)}`, c = i ? r.provideService(s,
|
|
448
|
+
let a = this.sql, o = n.map((e) => a`${a(e)} = ${t[e]}`), s = a`SELECT TOP 1 * FROM ${a(this.nsT(e))} WHERE ${a.and(o)}`, c = i ? r.provideService(s, F, i) : s;
|
|
441
449
|
return (await this.runtime.runPromise(c))[0];
|
|
442
450
|
}
|
|
443
451
|
emitChange(e) {
|
|
@@ -544,16 +552,16 @@ var ae = {
|
|
|
544
552
|
}
|
|
545
553
|
async emptyTablesOn(e, t) {
|
|
546
554
|
if (e.length === 0) return;
|
|
547
|
-
let n = this.sql, i = (e) => this.nsT(e), a = (e, t) => n.unsafe(`ALTER TABLE ${
|
|
555
|
+
let n = this.sql, i = (e) => this.nsT(e), a = (e, t) => n.unsafe(`ALTER TABLE ${te(i(e), "mssql")} ${t ? "WITH CHECK CHECK" : "NOCHECK"} CONSTRAINT ALL`), o = r.acquireUseRelease(r.forEach(e, (e) => a(e, !1), { discard: !0 }), () => r.forEach(e, (e) => n`DELETE FROM ${n(i(e))}`, { discard: !0 }), () => r.orDie(r.ignore(r.forEach(e, (e) => a(e, !0), { discard: !0 })))), s = t === null ? n.withTransaction(o) : r.provideService(o, F, t);
|
|
548
556
|
await this.runtime.runPromise(s);
|
|
549
557
|
}
|
|
550
558
|
async transactional(e) {
|
|
551
559
|
this.inflightTxns++;
|
|
552
560
|
try {
|
|
553
|
-
return await
|
|
561
|
+
return await ie({
|
|
554
562
|
...this.txnSpec("MssqlStore.transactional"),
|
|
555
563
|
work: e,
|
|
556
|
-
makeView: (e, t) => new
|
|
564
|
+
makeView: (e, t) => new le(this, e, t)
|
|
557
565
|
});
|
|
558
566
|
} finally {
|
|
559
567
|
this.inflightTxns--;
|
|
@@ -584,7 +592,7 @@ var ae = {
|
|
|
584
592
|
return this.changeStrategy === "cdc" ? "fleet" : "local";
|
|
585
593
|
}
|
|
586
594
|
injectExternalChange(e) {
|
|
587
|
-
|
|
595
|
+
w(e.table) && this.emitter.emit("change", S(e));
|
|
588
596
|
}
|
|
589
597
|
run(e) {
|
|
590
598
|
return this.runtime.runPromise(e);
|
|
@@ -607,7 +615,7 @@ var ae = {
|
|
|
607
615
|
async ping() {
|
|
608
616
|
await this.runtime.runPromise(this.sql`SELECT 1`);
|
|
609
617
|
}
|
|
610
|
-
},
|
|
618
|
+
}, le = class {
|
|
611
619
|
parent;
|
|
612
620
|
txn;
|
|
613
621
|
attr;
|
|
@@ -683,12 +691,12 @@ var ae = {
|
|
|
683
691
|
this.events.length = 0;
|
|
684
692
|
}
|
|
685
693
|
}
|
|
686
|
-
}, Q = (e) => e.__mssqlReplicationFriend ?? null,
|
|
694
|
+
}, Q = (e) => e.__mssqlReplicationFriend ?? null, $ = (e, t) => e === t ? 0 : e < t ? -1 : 1, ue = () => ({
|
|
687
695
|
async capturePrimaryPosition(e) {
|
|
688
696
|
let t = Q(e);
|
|
689
697
|
if (t === null) throw Error("mssqlReplicationAdapter: primary is not an MssqlStore (missing __mssqlReplicationFriend).");
|
|
690
698
|
return t.runEffect(r.gen(function* () {
|
|
691
|
-
return (yield* (yield*
|
|
699
|
+
return (yield* (yield* P)`
|
|
692
700
|
SELECT CONVERT(VARCHAR(40), end_of_log_lsn) AS lsn
|
|
693
701
|
FROM sys.dm_hadr_database_replica_states
|
|
694
702
|
WHERE database_id = DB_ID()
|
|
@@ -701,7 +709,7 @@ var ae = {
|
|
|
701
709
|
let t = Q(e);
|
|
702
710
|
if (t === null) throw Error("mssqlReplicationAdapter: replica is not an MssqlStore.");
|
|
703
711
|
return t.runEffect(r.gen(function* () {
|
|
704
|
-
return (yield* (yield*
|
|
712
|
+
return (yield* (yield* P)`
|
|
705
713
|
SELECT CONVERT(VARCHAR(40), last_hardened_lsn) AS lsn
|
|
706
714
|
FROM sys.dm_hadr_database_replica_states
|
|
707
715
|
WHERE database_id = DB_ID()
|
|
@@ -710,9 +718,9 @@ var ae = {
|
|
|
710
718
|
}));
|
|
711
719
|
},
|
|
712
720
|
compare(e, t) {
|
|
713
|
-
return
|
|
721
|
+
return $(t, e) >= 0 ? "caught-up" : "behind";
|
|
714
722
|
}
|
|
715
|
-
}),
|
|
723
|
+
}), de = {
|
|
716
724
|
id: "mssql",
|
|
717
725
|
makeSqlLayer: (e) => R(e),
|
|
718
726
|
makeStore: (e) => Z(e),
|
|
@@ -720,4 +728,4 @@ var ae = {
|
|
|
720
728
|
retryFilter: W
|
|
721
729
|
};
|
|
722
730
|
//#endregion
|
|
723
|
-
export { c as CDC_OFFSETS_TABLE, e as MssqlClient, d as _voltroMssqlCdcOffsetsTable, L as connectionFromConfig, K as ensureChangeTracking,
|
|
731
|
+
export { c as CDC_OFFSETS_TABLE, e as MssqlClient, d as _voltroMssqlCdcOffsetsTable, L as connectionFromConfig, K as ensureChangeTracking, I as makeMssqlSqlLayer, R as makeMssqlSqlLayerFromConfig, Z as makeMssqlStore, de as mssqlDialect, ue as mssqlReplicationAdapter, W as mssqlRetryFilter, Y as startChangeTrackingCdc };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voltro/sql-mssql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0",
|
|
4
4
|
"description": "SQL Server (mssql) dialect adapter for Voltro's cross-dialect DataStore (OUTPUT-clause returning, OFFSET/FETCH paging, inline reactivity).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"voltro",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@effect/sql": "^0.52.0",
|
|
37
37
|
"@effect/sql-mssql": "^0.53.0",
|
|
38
|
-
"@voltro/database": "0.
|
|
39
|
-
"@voltro/logger": "0.
|
|
38
|
+
"@voltro/database": "0.46.0",
|
|
39
|
+
"@voltro/logger": "0.46.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"effect": "^3.22.0"
|