@dudousxd/nestjs-durable-store-drizzle 0.25.0 → 0.27.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/dist/index.cjs +190 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +300 -1
- package/dist/index.d.ts +300 -1
- package/dist/index.js +189 -8
- package/dist/index.js.map +1 -1
- package/dist/pg/index.cjs +832 -122
- package/dist/pg/index.cjs.map +1 -1
- package/dist/pg/index.d.cts +295 -2
- package/dist/pg/index.d.ts +295 -2
- package/dist/pg/index.js +714 -15
- package/dist/pg/index.js.map +1 -1
- package/package.json +3 -3
package/dist/pg/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
9
|
var __export = (target, all) => {
|
|
@@ -16,6 +18,14 @@ var __copyProps = (to, from, except, desc2) => {
|
|
|
16
18
|
}
|
|
17
19
|
return to;
|
|
18
20
|
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
19
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
30
|
|
|
21
31
|
// src/pg/index.ts
|
|
@@ -32,6 +42,7 @@ __export(pg_exports, {
|
|
|
32
42
|
durablePgSchema: () => durablePgSchema,
|
|
33
43
|
durableRunAttributes: () => durableRunAttributes,
|
|
34
44
|
durableSchedules: () => durableSchedules,
|
|
45
|
+
durableSignalIds: () => durableSignalIds,
|
|
35
46
|
durableSignalWaiters: () => durableSignalWaiters,
|
|
36
47
|
durableStepCheckpoints: () => durableStepCheckpoints,
|
|
37
48
|
durableWorkflowRuns: () => durableWorkflowRuns,
|
|
@@ -96,6 +107,8 @@ var DURABLE_PG_DDL = [
|
|
|
96
107
|
"priority" integer,
|
|
97
108
|
"namespace" text DEFAULT 'default' NOT NULL,
|
|
98
109
|
"origin" text,
|
|
110
|
+
"tenant_id" text,
|
|
111
|
+
"tenant_kind" text,
|
|
99
112
|
"created_at" timestamp with time zone NOT NULL,
|
|
100
113
|
"updated_at" timestamp with time zone NOT NULL
|
|
101
114
|
)`,
|
|
@@ -142,6 +155,12 @@ var DURABLE_PG_DDL = [
|
|
|
142
155
|
"name" text NOT NULL,
|
|
143
156
|
"payload" jsonb,
|
|
144
157
|
"published_at" timestamp with time zone NOT NULL
|
|
158
|
+
)`,
|
|
159
|
+
`CREATE TABLE IF NOT EXISTS "durable_signal_ids" (
|
|
160
|
+
"token" text NOT NULL,
|
|
161
|
+
"signal_id" text NOT NULL,
|
|
162
|
+
"expires_at" timestamp with time zone NOT NULL,
|
|
163
|
+
CONSTRAINT "durable_signal_ids_token_signal_id_pk" PRIMARY KEY("token","signal_id")
|
|
145
164
|
)`,
|
|
146
165
|
`CREATE TABLE IF NOT EXISTS "durable_schedules" (
|
|
147
166
|
"id" text PRIMARY KEY NOT NULL,
|
|
@@ -152,9 +171,16 @@ var DURABLE_PG_DDL = [
|
|
|
152
171
|
"tags" jsonb,
|
|
153
172
|
"spec" jsonb NOT NULL,
|
|
154
173
|
"state" jsonb NOT NULL,
|
|
174
|
+
"tenant_id" text,
|
|
175
|
+
"tenant_kind" text,
|
|
155
176
|
"created_at" timestamp with time zone NOT NULL,
|
|
156
177
|
"updated_at" timestamp with time zone NOT NULL
|
|
157
178
|
)`,
|
|
179
|
+
// Tenancy columns (the customer a run / schedule belongs to): added in place on an existing database.
|
|
180
|
+
`ALTER TABLE "durable_workflow_runs" ADD COLUMN IF NOT EXISTS "tenant_id" text`,
|
|
181
|
+
`ALTER TABLE "durable_workflow_runs" ADD COLUMN IF NOT EXISTS "tenant_kind" text`,
|
|
182
|
+
`ALTER TABLE "durable_schedules" ADD COLUMN IF NOT EXISTS "tenant_id" text`,
|
|
183
|
+
`ALTER TABLE "durable_schedules" ADD COLUMN IF NOT EXISTS "tenant_kind" text`,
|
|
158
184
|
`CREATE INDEX IF NOT EXISTS "durable_runs_status_idx" ON "durable_workflow_runs" USING btree ("status","wake_at")`,
|
|
159
185
|
`CREATE INDEX IF NOT EXISTS "durable_runs_workflow_status_idx" ON "durable_workflow_runs" USING btree ("workflow","status")`,
|
|
160
186
|
`CREATE INDEX IF NOT EXISTS "durable_workflow_runs_namespace_status_idx" ON "durable_workflow_runs" USING btree ("namespace","status","created_at")`,
|
|
@@ -166,7 +192,10 @@ var DURABLE_PG_DDL = [
|
|
|
166
192
|
`CREATE INDEX IF NOT EXISTS "durable_signal_waiters_run_id_idx" ON "durable_signal_waiters" USING btree ("run_id")`,
|
|
167
193
|
`CREATE INDEX IF NOT EXISTS "durable_buffered_signals_token_idx" ON "durable_buffered_signals" USING btree ("token","id")`,
|
|
168
194
|
`CREATE INDEX IF NOT EXISTS "durable_buffered_events_name_published_at_idx" ON "durable_buffered_events" USING btree ("name","published_at")`,
|
|
169
|
-
`CREATE INDEX IF NOT EXISTS "durable_schedules_due_idx" ON "durable_schedules" USING btree ("paused","next_fire_at")
|
|
195
|
+
`CREATE INDEX IF NOT EXISTS "durable_schedules_due_idx" ON "durable_schedules" USING btree ("paused","next_fire_at")`,
|
|
196
|
+
`CREATE INDEX IF NOT EXISTS "durable_workflow_runs_tenant_idx" ON "durable_workflow_runs" USING btree ("tenant_id","created_at")`,
|
|
197
|
+
`CREATE INDEX IF NOT EXISTS "durable_schedules_tenant_idx" ON "durable_schedules" USING btree ("tenant_id")`,
|
|
198
|
+
`CREATE INDEX IF NOT EXISTS "durable_signal_ids_expires_at_idx" ON "durable_signal_ids" USING btree ("expires_at")`
|
|
170
199
|
];
|
|
171
200
|
var SCHEMA_LOCK_KEY = "durable_schema";
|
|
172
201
|
function drizzlePgSchemaRepairWarning(repairs) {
|
|
@@ -239,10 +268,190 @@ __name(ensureDrizzlePgDurableSchema, "ensureDrizzlePgDurableSchema");
|
|
|
239
268
|
|
|
240
269
|
// src/pg/drizzle-pg-state-store.ts
|
|
241
270
|
var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
|
|
242
|
-
var
|
|
271
|
+
var import_drizzle_orm5 = require("drizzle-orm");
|
|
243
272
|
|
|
244
|
-
// src/
|
|
273
|
+
// src/tenant-columns.ts
|
|
274
|
+
var PG_STATEMENTS = [
|
|
275
|
+
'ALTER TABLE "durable_workflow_runs" ADD COLUMN IF NOT EXISTS "tenant_id" text;',
|
|
276
|
+
'ALTER TABLE "durable_workflow_runs" ADD COLUMN IF NOT EXISTS "tenant_kind" text;',
|
|
277
|
+
'ALTER TABLE "durable_schedules" ADD COLUMN IF NOT EXISTS "tenant_id" text;',
|
|
278
|
+
'ALTER TABLE "durable_schedules" ADD COLUMN IF NOT EXISTS "tenant_kind" text;',
|
|
279
|
+
'CREATE INDEX IF NOT EXISTS "durable_workflow_runs_tenant_idx" ON "durable_workflow_runs" USING btree ("tenant_id","created_at");',
|
|
280
|
+
'CREATE INDEX IF NOT EXISTS "durable_schedules_tenant_idx" ON "durable_schedules" USING btree ("tenant_id");'
|
|
281
|
+
];
|
|
282
|
+
var SQLITE_STATEMENTS = [
|
|
283
|
+
"ALTER TABLE durable_workflow_runs ADD COLUMN tenant_id TEXT;",
|
|
284
|
+
"ALTER TABLE durable_workflow_runs ADD COLUMN tenant_kind TEXT;",
|
|
285
|
+
"ALTER TABLE durable_schedules ADD COLUMN tenant_id TEXT;",
|
|
286
|
+
"ALTER TABLE durable_schedules ADD COLUMN tenant_kind TEXT;",
|
|
287
|
+
"CREATE INDEX IF NOT EXISTS durable_workflow_runs_tenant_idx ON durable_workflow_runs (tenant_id, created_at);",
|
|
288
|
+
"CREATE INDEX IF NOT EXISTS durable_schedules_tenant_idx ON durable_schedules (tenant_id);"
|
|
289
|
+
];
|
|
290
|
+
var TENANT_COLUMN = /tenant_(id|kind)/;
|
|
291
|
+
function isMissingTenantColumn(err) {
|
|
292
|
+
let cur = err;
|
|
293
|
+
for (let depth = 0; cur && depth < 5; depth += 1) {
|
|
294
|
+
const e = cur;
|
|
295
|
+
const message = typeof e.message === "string" ? e.message : "";
|
|
296
|
+
if (TENANT_COLUMN.test(message)) {
|
|
297
|
+
if (e.code === "42703" || /does not exist/.test(message) || /no such column/i.test(message)) {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
cur = e.cause;
|
|
302
|
+
}
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
__name(isMissingTenantColumn, "isMissingTenantColumn");
|
|
306
|
+
var MissingTenantColumnsError = class extends Error {
|
|
307
|
+
static {
|
|
308
|
+
__name(this, "MissingTenantColumnsError");
|
|
309
|
+
}
|
|
310
|
+
constructor(dialect, cause) {
|
|
311
|
+
const statements = dialect === "pg" ? PG_STATEMENTS : SQLITE_STATEMENTS;
|
|
312
|
+
const fix = dialect === "pg" ? "Generate a migration with drizzle-kit (`drizzle-kit generate`) from the store schema and apply it, or turn on the module's `autoSchema`, which adds them on boot." : "Generate a migration with drizzle-kit (`drizzle-kit generate`) from the store schema and apply it.";
|
|
313
|
+
super([
|
|
314
|
+
"[nestjs-durable] The durable tables are missing the tenancy columns (tenant_id, tenant_kind).",
|
|
315
|
+
"This version of the Drizzle store reads them on every run and schedule query, even with tenancy off.",
|
|
316
|
+
fix,
|
|
317
|
+
"The migration must contain:",
|
|
318
|
+
...statements.map((s) => ` ${s}`)
|
|
319
|
+
].join("\n"), {
|
|
320
|
+
cause
|
|
321
|
+
});
|
|
322
|
+
this.name = "MissingTenantColumnsError";
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
function tenantColumnsHint(dialect) {
|
|
326
|
+
return (err) => {
|
|
327
|
+
if (isMissingTenantColumn(err)) throw new MissingTenantColumnsError(dialect, err);
|
|
328
|
+
throw err;
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
__name(tenantColumnsHint, "tenantColumnsHint");
|
|
332
|
+
async function withTenantColumnsHint(dialect, query) {
|
|
333
|
+
try {
|
|
334
|
+
return await query;
|
|
335
|
+
} catch (err) {
|
|
336
|
+
return tenantColumnsHint(dialect)(err);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
__name(withTenantColumnsHint, "withTenantColumnsHint");
|
|
340
|
+
|
|
341
|
+
// src/pg/hot-path.ts
|
|
342
|
+
var drizzleOrm = __toESM(require("drizzle-orm"), 1);
|
|
245
343
|
var import_drizzle_orm3 = require("drizzle-orm");
|
|
344
|
+
function nodePgClient(db) {
|
|
345
|
+
const client = db.$client;
|
|
346
|
+
if (!client || typeof client !== "object") return void 0;
|
|
347
|
+
if (typeof client.query !== "function") return void 0;
|
|
348
|
+
const isPool = "totalCount" in client && "idleCount" in client;
|
|
349
|
+
const isClient = "connectionParameters" in client;
|
|
350
|
+
return isPool || isClient ? client : void 0;
|
|
351
|
+
}
|
|
352
|
+
__name(nodePgClient, "nodePgClient");
|
|
353
|
+
function drizzleLogger(db) {
|
|
354
|
+
const logger = db.session?.logger;
|
|
355
|
+
if (!logger || typeof logger.logQuery !== "function") return void 0;
|
|
356
|
+
if (logger.constructor?.name === "NoopLogger") {
|
|
357
|
+
return void 0;
|
|
358
|
+
}
|
|
359
|
+
return logger;
|
|
360
|
+
}
|
|
361
|
+
__name(drizzleLogger, "drizzleLogger");
|
|
362
|
+
var QueryError = drizzleOrm.DrizzleQueryError;
|
|
363
|
+
function hash(text2) {
|
|
364
|
+
let h = 2166136261;
|
|
365
|
+
for (let i = 0; i < text2.length; i += 1) {
|
|
366
|
+
h ^= text2.charCodeAt(i);
|
|
367
|
+
h = Math.imul(h, 16777619);
|
|
368
|
+
}
|
|
369
|
+
return (h >>> 0).toString(36);
|
|
370
|
+
}
|
|
371
|
+
__name(hash, "hash");
|
|
372
|
+
var PgHotPath = class _PgHotPath {
|
|
373
|
+
static {
|
|
374
|
+
__name(this, "PgHotPath");
|
|
375
|
+
}
|
|
376
|
+
client;
|
|
377
|
+
logger;
|
|
378
|
+
prepared;
|
|
379
|
+
constructor(client, logger, prepared) {
|
|
380
|
+
this.client = client;
|
|
381
|
+
this.logger = logger;
|
|
382
|
+
this.prepared = prepared;
|
|
383
|
+
}
|
|
384
|
+
/** The hot path for `db`, or `undefined` when `db` does not run on node-postgres. */
|
|
385
|
+
static for(db, options = {}) {
|
|
386
|
+
const client = nodePgClient(db);
|
|
387
|
+
if (!client) return void 0;
|
|
388
|
+
return new _PgHotPath(client, drizzleLogger(db), options.preparedStatements !== false);
|
|
389
|
+
}
|
|
390
|
+
/** Run one statement; resolves its rows as the driver decoded them (column names as stored). */
|
|
391
|
+
async query(text2, values) {
|
|
392
|
+
this.logger?.logQuery(text2, values);
|
|
393
|
+
const config = {
|
|
394
|
+
text: text2,
|
|
395
|
+
values
|
|
396
|
+
};
|
|
397
|
+
if (this.prepared) config.name = `durable_${hash(text2)}`;
|
|
398
|
+
try {
|
|
399
|
+
return (await this.client.query(config)).rows;
|
|
400
|
+
} catch (err) {
|
|
401
|
+
throw QueryError ? new QueryError(text2, values, err) : err;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
var TableCodec = class {
|
|
406
|
+
static {
|
|
407
|
+
__name(this, "TableCodec");
|
|
408
|
+
}
|
|
409
|
+
columns;
|
|
410
|
+
/** `"a", "b", …` in column order. */
|
|
411
|
+
list;
|
|
412
|
+
constructor(table, omit = []) {
|
|
413
|
+
this.columns = Object.entries((0, import_drizzle_orm3.getTableColumns)(table)).filter(([key]) => !omit.includes(key));
|
|
414
|
+
this.list = this.columns.map(([, c]) => quote(c.name)).join(", ");
|
|
415
|
+
}
|
|
416
|
+
/** The quoted column name of `property`. */
|
|
417
|
+
name(property) {
|
|
418
|
+
const found = this.columns.find(([key]) => key === property);
|
|
419
|
+
if (!found) throw new Error(`no column for ${property}`);
|
|
420
|
+
return quote(found[1].name);
|
|
421
|
+
}
|
|
422
|
+
/** A value of `property` as drizzle's builder would bind it. */
|
|
423
|
+
encode(property, value) {
|
|
424
|
+
if (value == null) return null;
|
|
425
|
+
const found = this.columns.find(([key]) => key === property);
|
|
426
|
+
return found ? found[1].mapToDriverValue(value) : value;
|
|
427
|
+
}
|
|
428
|
+
/** A row's values, in column order, as drizzle's builder would bind them. */
|
|
429
|
+
encodeRow(row) {
|
|
430
|
+
return this.columns.map(([key, column]) => {
|
|
431
|
+
const value = row[key];
|
|
432
|
+
return value == null ? null : column.mapToDriverValue(value);
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
/** A row a statement returned, as drizzle would map it (absent / omitted columns read `null`). */
|
|
436
|
+
decode(raw) {
|
|
437
|
+
const row = {};
|
|
438
|
+
for (const [key, column] of this.columns) {
|
|
439
|
+
const value = raw[column.name];
|
|
440
|
+
row[key] = value == null ? null : column.mapFromDriverValue(value);
|
|
441
|
+
}
|
|
442
|
+
return row;
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
var quote = /* @__PURE__ */ __name((name) => `"${name.replace(/"/g, '""')}"`, "quote");
|
|
446
|
+
function placeholders(from, count) {
|
|
447
|
+
return Array.from({
|
|
448
|
+
length: count
|
|
449
|
+
}, (_, i) => `$${from + i}`).join(", ");
|
|
450
|
+
}
|
|
451
|
+
__name(placeholders, "placeholders");
|
|
452
|
+
|
|
453
|
+
// src/pg/schema.ts
|
|
454
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
246
455
|
var import_pg_core = require("drizzle-orm/pg-core");
|
|
247
456
|
var DEFAULT_NAMESPACE = "default";
|
|
248
457
|
var tstz = /* @__PURE__ */ __name((name) => (0, import_pg_core.timestamp)(name, {
|
|
@@ -272,9 +481,14 @@ var durableWorkflowRuns = (0, import_pg_core.pgTable)("durable_workflow_runs", {
|
|
|
272
481
|
namespace: (0, import_pg_core.text)("namespace").notNull().default(DEFAULT_NAMESPACE),
|
|
273
482
|
// Which package declared the workflow. Nullable, no default: NULL = unknown, never guessed.
|
|
274
483
|
origin: (0, import_pg_core.text)("origin"),
|
|
484
|
+
// The customer (tenancy) the run belongs to — not the worker-pool `namespace`. NULL = none.
|
|
485
|
+
tenantId: (0, import_pg_core.text)("tenant_id"),
|
|
486
|
+
tenantKind: (0, import_pg_core.text)("tenant_kind"),
|
|
275
487
|
createdAt: tstz("created_at").notNull(),
|
|
276
488
|
updatedAt: tstz("updated_at").notNull()
|
|
277
489
|
}, (t) => [
|
|
490
|
+
// App-facing reads with tenancy on: `tenant_id = $1 ORDER BY created_at DESC`.
|
|
491
|
+
(0, import_pg_core.index)("durable_workflow_runs_tenant_idx").on(t.tenantId, t.createdAt),
|
|
278
492
|
// Timer poller (`listDueTimers`) + recovery (`listIncompleteRuns`). Same name as the TypeORM DDL.
|
|
279
493
|
(0, import_pg_core.index)("durable_runs_status_idx").on(t.status, t.wakeAt),
|
|
280
494
|
// Dashboard `listRuns({ workflow, status })` and the singleton gate's per-workflow scans.
|
|
@@ -372,6 +586,19 @@ var durableBufferedEvents = (0, import_pg_core.pgTable)("durable_buffered_events
|
|
|
372
586
|
}, (t) => [
|
|
373
587
|
(0, import_pg_core.index)("durable_buffered_events_name_published_at_idx").on(t.name, t.publishedAt)
|
|
374
588
|
]);
|
|
589
|
+
var durableSignalIds = (0, import_pg_core.pgTable)("durable_signal_ids", {
|
|
590
|
+
token: (0, import_pg_core.text)("token").notNull(),
|
|
591
|
+
signalId: (0, import_pg_core.text)("signal_id").notNull(),
|
|
592
|
+
expiresAt: tstz("expires_at").notNull()
|
|
593
|
+
}, (t) => [
|
|
594
|
+
(0, import_pg_core.primaryKey)({
|
|
595
|
+
columns: [
|
|
596
|
+
t.token,
|
|
597
|
+
t.signalId
|
|
598
|
+
]
|
|
599
|
+
}),
|
|
600
|
+
(0, import_pg_core.index)("durable_signal_ids_expires_at_idx").on(t.expiresAt)
|
|
601
|
+
]);
|
|
375
602
|
var durableSchedules = (0, import_pg_core.pgTable)(
|
|
376
603
|
"durable_schedules",
|
|
377
604
|
{
|
|
@@ -383,12 +610,16 @@ var durableSchedules = (0, import_pg_core.pgTable)(
|
|
|
383
610
|
tags: (0, import_pg_core.jsonb)("tags").$type(),
|
|
384
611
|
spec: (0, import_pg_core.jsonb)("spec").$type().notNull(),
|
|
385
612
|
state: (0, import_pg_core.jsonb)("state").$type().notNull(),
|
|
613
|
+
// The customer the schedule (and every run it starts) belongs to. NULL = none.
|
|
614
|
+
tenantId: (0, import_pg_core.text)("tenant_id"),
|
|
615
|
+
tenantKind: (0, import_pg_core.text)("tenant_kind"),
|
|
386
616
|
createdAt: tstz("created_at").notNull(),
|
|
387
617
|
updatedAt: tstz("updated_at").notNull()
|
|
388
618
|
},
|
|
389
619
|
// The poll's "due by T" scan: `paused = false AND next_fire_at <= T ORDER BY next_fire_at`.
|
|
390
620
|
(t) => [
|
|
391
|
-
(0, import_pg_core.index)("durable_schedules_due_idx").on(t.paused, t.nextFireAt)
|
|
621
|
+
(0, import_pg_core.index)("durable_schedules_due_idx").on(t.paused, t.nextFireAt),
|
|
622
|
+
(0, import_pg_core.index)("durable_schedules_tenant_idx").on(t.tenantId)
|
|
392
623
|
]
|
|
393
624
|
);
|
|
394
625
|
var durablePgSchema = {
|
|
@@ -398,10 +629,11 @@ var durablePgSchema = {
|
|
|
398
629
|
durableSignalWaiters,
|
|
399
630
|
durableBufferedSignals,
|
|
400
631
|
durableBufferedEvents,
|
|
632
|
+
durableSignalIds,
|
|
401
633
|
durableSchedules
|
|
402
634
|
};
|
|
403
635
|
function durablePgManagedTables() {
|
|
404
|
-
return Object.values(durablePgSchema).map((table) => (0,
|
|
636
|
+
return Object.values(durablePgSchema).map((table) => (0, import_drizzle_orm4.getTableName)(table));
|
|
405
637
|
}
|
|
406
638
|
__name(durablePgManagedTables, "durablePgManagedTables");
|
|
407
639
|
|
|
@@ -411,22 +643,30 @@ var RETENTION_EXCLUDED = [
|
|
|
411
643
|
];
|
|
412
644
|
function anyOf(conditions) {
|
|
413
645
|
const present = conditions.filter((c) => c !== void 0);
|
|
414
|
-
return present.length ? (0,
|
|
646
|
+
return present.length ? (0, import_drizzle_orm5.or)(...present) : import_drizzle_orm5.sql`1 = 0`;
|
|
415
647
|
}
|
|
416
648
|
__name(anyOf, "anyOf");
|
|
649
|
+
function tenantFilters(column, query) {
|
|
650
|
+
const one = /* @__PURE__ */ __name((id) => id === null ? (0, import_drizzle_orm5.isNull)(column) : (0, import_drizzle_orm5.eq)(column, id), "one");
|
|
651
|
+
return [
|
|
652
|
+
query.tenantId !== void 0 ? one(query.tenantId) : void 0,
|
|
653
|
+
query.tenantIds ? anyOf(query.tenantIds.map(one)) : void 0
|
|
654
|
+
];
|
|
655
|
+
}
|
|
656
|
+
__name(tenantFilters, "tenantFilters");
|
|
417
657
|
function namespaceFilter(namespace) {
|
|
418
658
|
if (namespace === void 0) return void 0;
|
|
419
659
|
if (namespace === DEFAULT_NAMESPACE || namespace === "") {
|
|
420
|
-
return (0,
|
|
660
|
+
return (0, import_drizzle_orm5.inArray)(durableWorkflowRuns.namespace, [
|
|
421
661
|
DEFAULT_NAMESPACE,
|
|
422
662
|
""
|
|
423
663
|
]);
|
|
424
664
|
}
|
|
425
|
-
return (0,
|
|
665
|
+
return (0, import_drizzle_orm5.eq)(durableWorkflowRuns.namespace, namespace);
|
|
426
666
|
}
|
|
427
667
|
__name(namespaceFilter, "namespaceFilter");
|
|
428
668
|
function hasTag(tag) {
|
|
429
|
-
return
|
|
669
|
+
return import_drizzle_orm5.sql`${durableWorkflowRuns.tags} @> ${JSON.stringify([
|
|
430
670
|
tag
|
|
431
671
|
])}::jsonb`;
|
|
432
672
|
}
|
|
@@ -440,10 +680,59 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
440
680
|
db;
|
|
441
681
|
options;
|
|
442
682
|
scopeNamespace;
|
|
683
|
+
/** Constant-SQL path for the per-step statements, on node-postgres (see `hot-path.ts`). */
|
|
684
|
+
hot;
|
|
685
|
+
/** The run read-scope predicate as SQL text from `$2` on, and its values (hot path). */
|
|
686
|
+
hotScope;
|
|
687
|
+
/** Hot-path UPDATE texts by patch shape. */
|
|
688
|
+
hotUpdates = /* @__PURE__ */ new Map();
|
|
443
689
|
constructor(db, options = {}) {
|
|
444
690
|
this.db = db;
|
|
445
691
|
this.options = options;
|
|
446
692
|
this.scopeNamespace = options.scope?.namespace;
|
|
693
|
+
this.hot = PgHotPath.for(db, {
|
|
694
|
+
preparedStatements: options.preparedStatements
|
|
695
|
+
});
|
|
696
|
+
const ns = this.scopeNamespace;
|
|
697
|
+
this.hotScope = ns === void 0 ? {
|
|
698
|
+
text: "",
|
|
699
|
+
values: []
|
|
700
|
+
} : ns === DEFAULT_NAMESPACE || ns === "" ? {
|
|
701
|
+
text: ' AND "namespace" IN ($2, $3)',
|
|
702
|
+
values: [
|
|
703
|
+
DEFAULT_NAMESPACE,
|
|
704
|
+
""
|
|
705
|
+
]
|
|
706
|
+
} : {
|
|
707
|
+
text: ' AND "namespace" = $2',
|
|
708
|
+
values: [
|
|
709
|
+
ns
|
|
710
|
+
]
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* A hot-path `UPDATE` of the runs table setting `row`'s columns on run `$n`, optionally only while
|
|
715
|
+
* its status is one of `expected` (then `RETURNING "id"`). Text cached per shape.
|
|
716
|
+
*/
|
|
717
|
+
hotRunUpdate(runId, row, expected) {
|
|
718
|
+
const keys = Object.keys(row);
|
|
719
|
+
const shape = `${keys.join(",")}|${expected?.length ?? -1}`;
|
|
720
|
+
let text2 = this.hotUpdates.get(shape);
|
|
721
|
+
if (text2 === void 0) {
|
|
722
|
+
const set = keys.map((key, i) => `${RUNS.name(key)} = $${i + 1}`).join(", ");
|
|
723
|
+
const id = keys.length + 1;
|
|
724
|
+
text2 = `UPDATE ${T_RUNS} SET ${set} WHERE "id" = $${id}`;
|
|
725
|
+
if (expected) {
|
|
726
|
+
text2 += ` AND "status" IN (${placeholders(id + 1, expected.length)}) RETURNING "id"`;
|
|
727
|
+
}
|
|
728
|
+
this.hotUpdates.set(shape, text2);
|
|
729
|
+
}
|
|
730
|
+
const values = keys.map((key) => RUNS.encode(key, row[key]));
|
|
731
|
+
values.push(runId, ...expected ?? []);
|
|
732
|
+
return {
|
|
733
|
+
text: text2,
|
|
734
|
+
values
|
|
735
|
+
};
|
|
447
736
|
}
|
|
448
737
|
/** A store confined to `scope.namespace`, sharing this store's db (no new connection). Used by the
|
|
449
738
|
* NestJS module's `scopeReads`; `{ namespace: undefined }` is the unscoped operator view. */
|
|
@@ -466,28 +755,35 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
466
755
|
}
|
|
467
756
|
async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
|
|
468
757
|
return this.db.transaction(async (tx) => {
|
|
469
|
-
const where = (0,
|
|
758
|
+
const where = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.workflow, workflow), (0, import_drizzle_orm5.inArray)(durableWorkflowRuns.status, import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES), hasTag(tag), this.scope());
|
|
470
759
|
await tx.select({
|
|
471
760
|
id: durableWorkflowRuns.id
|
|
472
|
-
}).from(durableWorkflowRuns).where(where).orderBy((0,
|
|
761
|
+
}).from(durableWorkflowRuns).where(where).orderBy((0, import_drizzle_orm5.asc)(durableWorkflowRuns.id)).for("update");
|
|
473
762
|
const rows = await tx.select().from(durableWorkflowRuns).where(where);
|
|
474
763
|
const runs = rows.map(fromRunRow);
|
|
475
764
|
const tags = (0, import_nestjs_durable_core.singletonAdmissionTags)(runs, runId, tag, workflow, limit);
|
|
476
765
|
if (!tags) {
|
|
477
766
|
const patch = (0, import_nestjs_durable_core.singletonAdmissionDenialPatch)(runs, runId, tag, workflow, retryWakeAt);
|
|
478
|
-
if (patch) await tx.update(durableWorkflowRuns).set(toRunPatch(patch)).where((0,
|
|
767
|
+
if (patch) await tx.update(durableWorkflowRuns).set(toRunPatch(patch)).where((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId));
|
|
479
768
|
return false;
|
|
480
769
|
}
|
|
481
770
|
await tx.update(durableWorkflowRuns).set({
|
|
482
771
|
tags
|
|
483
|
-
}).where((0,
|
|
772
|
+
}).where((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId));
|
|
484
773
|
return true;
|
|
485
774
|
});
|
|
486
775
|
}
|
|
487
776
|
async createRun(run) {
|
|
488
777
|
if ((0, import_nestjs_durable_core.normalizeAttributeRows)(run.id, run.searchAttributes).length === 0) {
|
|
778
|
+
if (this.hot) {
|
|
779
|
+
await this.hot.query(INSERT_RUN, RUNS.encodeRow(toRunRow(run)));
|
|
780
|
+
await this.hot.query(DELETE_ATTRIBUTES, [
|
|
781
|
+
run.id
|
|
782
|
+
]);
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
489
785
|
await this.db.insert(durableWorkflowRuns).values(toRunRow(run));
|
|
490
|
-
await this.db.delete(durableRunAttributes).where((0,
|
|
786
|
+
await this.db.delete(durableRunAttributes).where((0, import_drizzle_orm5.eq)(durableRunAttributes.runId, run.id));
|
|
491
787
|
return;
|
|
492
788
|
}
|
|
493
789
|
await this.db.transaction(async (tx) => {
|
|
@@ -501,11 +797,16 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
501
797
|
const reindex = "searchAttributes" in patch;
|
|
502
798
|
if (!Object.keys(row).length && !reindex) return;
|
|
503
799
|
if (!reindex) {
|
|
504
|
-
|
|
800
|
+
if (this.hot) {
|
|
801
|
+
const { text: text2, values } = this.hotRunUpdate(runId, row);
|
|
802
|
+
await this.hot.query(text2, values);
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
await this.db.update(durableWorkflowRuns).set(row).where((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId));
|
|
505
806
|
return;
|
|
506
807
|
}
|
|
507
808
|
await this.db.transaction(async (tx) => {
|
|
508
|
-
if (Object.keys(row).length) await tx.update(durableWorkflowRuns).set(row).where((0,
|
|
809
|
+
if (Object.keys(row).length) await tx.update(durableWorkflowRuns).set(row).where((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId));
|
|
509
810
|
if (reindex) await reindexAttributes(tx, runId, patch.searchAttributes);
|
|
510
811
|
});
|
|
511
812
|
}
|
|
@@ -514,7 +815,17 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
514
815
|
const patch = inputPatch.status && import_nestjs_durable_core.TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await (0, import_nestjs_durable_core.clearTerminalSingletonAdmission)(runId, inputPatch, (id) => this.withScope({}).getRun(id)) : inputPatch;
|
|
515
816
|
const row = toRunPatch(patch);
|
|
516
817
|
const reindex = "searchAttributes" in patch;
|
|
517
|
-
const matches = (0,
|
|
818
|
+
const matches = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId), (0, import_drizzle_orm5.inArray)(durableWorkflowRuns.status, expectedStatuses));
|
|
819
|
+
if (!reindex && Object.keys(row).length) {
|
|
820
|
+
if (this.hot) {
|
|
821
|
+
const { text: text2, values } = this.hotRunUpdate(runId, row, expectedStatuses);
|
|
822
|
+
return (await this.hot.query(text2, values)).length === 1;
|
|
823
|
+
}
|
|
824
|
+
const applied = await this.db.update(durableWorkflowRuns).set(row).where(matches).returning({
|
|
825
|
+
id: durableWorkflowRuns.id
|
|
826
|
+
});
|
|
827
|
+
return applied.length === 1;
|
|
828
|
+
}
|
|
518
829
|
return this.db.transaction(async (tx) => {
|
|
519
830
|
const applied = Object.keys(row).length ? (await tx.update(durableWorkflowRuns).set(row).where(matches).returning({
|
|
520
831
|
id: durableWorkflowRuns.id
|
|
@@ -526,12 +837,19 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
526
837
|
});
|
|
527
838
|
}
|
|
528
839
|
async getRun(runId) {
|
|
529
|
-
|
|
840
|
+
if (this.hot) {
|
|
841
|
+
const rows2 = await this.hot.query(`${SELECT_RUN}${this.hotScope.text} LIMIT 1`, [
|
|
842
|
+
runId,
|
|
843
|
+
...this.hotScope.values
|
|
844
|
+
]);
|
|
845
|
+
return rows2[0] ? fromRunRow(RUNS.decode(rows2[0])) : null;
|
|
846
|
+
}
|
|
847
|
+
const rows = await this.db.select().from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId), this.scope())).limit(1);
|
|
530
848
|
return rows[0] ? fromRunRow(rows[0]) : null;
|
|
531
849
|
}
|
|
532
850
|
async getRuns(runIds) {
|
|
533
851
|
if (runIds.length === 0) return [];
|
|
534
|
-
const rows = await this.db.select().from(durableWorkflowRuns).where((0,
|
|
852
|
+
const rows = await this.db.select().from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.inArray)(durableWorkflowRuns.id, [
|
|
535
853
|
...new Set(runIds)
|
|
536
854
|
]), this.scope()));
|
|
537
855
|
return rows.map(fromRunRow);
|
|
@@ -565,7 +883,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
565
883
|
const rows = await tx.select({
|
|
566
884
|
id: durableWorkflowRuns.id,
|
|
567
885
|
tags: durableWorkflowRuns.tags
|
|
568
|
-
}).from(durableWorkflowRuns).where((0,
|
|
886
|
+
}).from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)(...scoped, (0, import_drizzle_orm5.lt)(durableWorkflowRuns.updatedAt, cutoff))).orderBy((0, import_drizzle_orm5.asc)(durableWorkflowRuns.updatedAt)).limit(limit).for("update", {
|
|
569
887
|
skipLocked: true
|
|
570
888
|
});
|
|
571
889
|
for (const r of rows) if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
|
|
@@ -574,7 +892,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
574
892
|
const rows = await tx.select({
|
|
575
893
|
id: durableWorkflowRuns.id,
|
|
576
894
|
tags: durableWorkflowRuns.tags
|
|
577
|
-
}).from(durableWorkflowRuns).where((0,
|
|
895
|
+
}).from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)(...scoped)).orderBy((0, import_drizzle_orm5.desc)(durableWorkflowRuns.updatedAt), (0, import_drizzle_orm5.desc)(durableWorkflowRuns.id)).limit(limit).offset(policy.maxCount).for("update", {
|
|
578
896
|
skipLocked: true
|
|
579
897
|
});
|
|
580
898
|
for (const r of rows) {
|
|
@@ -591,7 +909,14 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
591
909
|
});
|
|
592
910
|
}
|
|
593
911
|
async getCheckpoint(runId, seq) {
|
|
594
|
-
|
|
912
|
+
if (this.hot) {
|
|
913
|
+
const rows2 = await this.hot.query(SELECT_CHECKPOINT, [
|
|
914
|
+
runId,
|
|
915
|
+
seq
|
|
916
|
+
]);
|
|
917
|
+
return rows2[0] ? fromCheckpointRow(CPS.decode(rows2[0])) : null;
|
|
918
|
+
}
|
|
919
|
+
const rows = await this.db.select().from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.eq)(durableStepCheckpoints.seq, seq))).limit(1);
|
|
595
920
|
return rows[0] ? fromCheckpointRow(rows[0]) : null;
|
|
596
921
|
}
|
|
597
922
|
async getCheckpoints(runId, seqs) {
|
|
@@ -600,7 +925,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
600
925
|
];
|
|
601
926
|
const rows = [];
|
|
602
927
|
for (let at = 0; at < unique.length; at += 500) {
|
|
603
|
-
rows.push(...await this.db.select().from(durableStepCheckpoints).where((0,
|
|
928
|
+
rows.push(...await this.db.select().from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.inArray)(durableStepCheckpoints.seq, unique.slice(at, at + 500)))));
|
|
604
929
|
}
|
|
605
930
|
return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
|
|
606
931
|
}
|
|
@@ -608,10 +933,18 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
608
933
|
const unique = [
|
|
609
934
|
...new Set(seqs)
|
|
610
935
|
];
|
|
611
|
-
|
|
936
|
+
if (this.hot) {
|
|
937
|
+
if (unique.length === 0) return [];
|
|
938
|
+
const rows2 = await this.hot.query(SELECT_CHECKPOINTS_FOR_SETTLE, [
|
|
939
|
+
runId,
|
|
940
|
+
unique
|
|
941
|
+
]);
|
|
942
|
+
return rows2.map((raw) => fromCheckpointRow(CPS_NO_INPUT.decode(raw)));
|
|
943
|
+
}
|
|
944
|
+
const { input: _input, ...columns } = (0, import_drizzle_orm5.getTableColumns)(durableStepCheckpoints);
|
|
612
945
|
const rows = [];
|
|
613
946
|
for (let at = 0; at < unique.length; at += 500) {
|
|
614
|
-
rows.push(...await this.db.select(columns).from(durableStepCheckpoints).where((0,
|
|
947
|
+
rows.push(...await this.db.select(columns).from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.inArray)(durableStepCheckpoints.seq, unique.slice(at, at + 500)))));
|
|
615
948
|
}
|
|
616
949
|
return rows.sort((a, b) => a.seq - b.seq).map((row) => fromCheckpointRow({
|
|
617
950
|
...row,
|
|
@@ -619,6 +952,10 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
619
952
|
}));
|
|
620
953
|
}
|
|
621
954
|
async saveCheckpoint(checkpoint) {
|
|
955
|
+
if (this.hot) {
|
|
956
|
+
await this.hot.query(UPSERT_CHECKPOINT, CPS.encodeRow(toCheckpointRow(checkpoint)));
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
622
959
|
await upsertCheckpoint(this.db, checkpoint);
|
|
623
960
|
}
|
|
624
961
|
async saveCheckpoints(checkpoints) {
|
|
@@ -637,7 +974,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
637
974
|
}
|
|
638
975
|
async saveCheckpointIfPending(checkpoint) {
|
|
639
976
|
const { runId, seq, ...set } = toCheckpointRow(checkpoint);
|
|
640
|
-
const rows = await this.db.update(durableStepCheckpoints).set(set).where((0,
|
|
977
|
+
const rows = await this.db.update(durableStepCheckpoints).set(set).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.eq)(durableStepCheckpoints.seq, seq), (0, import_drizzle_orm5.eq)(durableStepCheckpoints.status, "pending"))).returning({
|
|
641
978
|
seq: durableStepCheckpoints.seq
|
|
642
979
|
});
|
|
643
980
|
return rows.length === 1;
|
|
@@ -645,7 +982,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
645
982
|
async saveCheckpointsIfPending(checkpoints) {
|
|
646
983
|
const wrote = checkpoints.map(() => false);
|
|
647
984
|
if (checkpoints.length === 0) return wrote;
|
|
648
|
-
await this.
|
|
985
|
+
await this.inTransactionIf(oneStatement(checkpoints), async (tx) => {
|
|
649
986
|
for (const round of distinctRounds(checkpoints)) {
|
|
650
987
|
for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
|
|
651
988
|
const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
|
|
@@ -669,7 +1006,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
669
1006
|
async settleCheckpointsIfPending(settlements) {
|
|
670
1007
|
const wrote = settlements.map(() => false);
|
|
671
1008
|
if (settlements.length === 0) return wrote;
|
|
672
|
-
await this.
|
|
1009
|
+
await this.inTransactionIf(oneStatement(settlements), async (tx) => {
|
|
673
1010
|
for (const round of distinctRounds(settlements)) {
|
|
674
1011
|
for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
|
|
675
1012
|
const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
|
|
@@ -690,6 +1027,84 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
690
1027
|
});
|
|
691
1028
|
return wrote;
|
|
692
1029
|
}
|
|
1030
|
+
async settleResultsIfPending(results) {
|
|
1031
|
+
const out = results.map(() => null);
|
|
1032
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1033
|
+
const sent = results.flatMap((r, i) => {
|
|
1034
|
+
const key = rowKey(r);
|
|
1035
|
+
if (seen.has(key)) return [];
|
|
1036
|
+
seen.add(key);
|
|
1037
|
+
return [
|
|
1038
|
+
i
|
|
1039
|
+
];
|
|
1040
|
+
});
|
|
1041
|
+
if (sent.length === 0) return out;
|
|
1042
|
+
if (this.hot && sent.length <= HOT_SETTLE_MAX_ROWS) {
|
|
1043
|
+
const rows = await this.hot.query(settleResultsText(sent.length), sent.flatMap((i) => settleResultValues(results[i])));
|
|
1044
|
+
const byKey = new Map(sent.map((i) => [
|
|
1045
|
+
rowKey(results[i]),
|
|
1046
|
+
i
|
|
1047
|
+
]));
|
|
1048
|
+
for (const raw of rows) {
|
|
1049
|
+
const cp = fromCheckpointRow(CPS_NO_INPUT.decode(raw));
|
|
1050
|
+
const i = byKey.get(rowKey(cp));
|
|
1051
|
+
if (i !== void 0) out[i] = cp;
|
|
1052
|
+
}
|
|
1053
|
+
return out;
|
|
1054
|
+
}
|
|
1055
|
+
await this.inTransactionIf(sent.length <= CHECKPOINT_BATCH_ROWS, async (tx) => {
|
|
1056
|
+
for (let at = 0; at < sent.length; at += CHECKPOINT_BATCH_ROWS) {
|
|
1057
|
+
const chunk = sent.slice(at, at + CHECKPOINT_BATCH_ROWS);
|
|
1058
|
+
const rows = rowsOf(await tx.execute(settleResultsFrom(chunk.map((i) => results[i]))));
|
|
1059
|
+
const byKey = new Map(chunk.map((i) => [
|
|
1060
|
+
rowKey(results[i]),
|
|
1061
|
+
i
|
|
1062
|
+
]));
|
|
1063
|
+
for (const raw of rows) {
|
|
1064
|
+
const cp = fromCheckpointRow(settledRowFromDriver(raw));
|
|
1065
|
+
const i = byKey.get(rowKey(cp));
|
|
1066
|
+
if (i !== void 0) out[i] = cp;
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
});
|
|
1070
|
+
return out;
|
|
1071
|
+
}
|
|
1072
|
+
async insertCheckpointIfAbsent(checkpoint) {
|
|
1073
|
+
if (this.hot) {
|
|
1074
|
+
const values = CPS.encodeRow(toCheckpointRow(checkpoint));
|
|
1075
|
+
return (await this.hot.query(INSERT_CHECKPOINT_IF_ABSENT, values)).length === 1;
|
|
1076
|
+
}
|
|
1077
|
+
const rows = await this.db.insert(durableStepCheckpoints).values(toCheckpointRow(checkpoint)).onConflictDoNothing({
|
|
1078
|
+
target: [
|
|
1079
|
+
durableStepCheckpoints.runId,
|
|
1080
|
+
durableStepCheckpoints.seq
|
|
1081
|
+
]
|
|
1082
|
+
}).returning({
|
|
1083
|
+
seq: durableStepCheckpoints.seq
|
|
1084
|
+
});
|
|
1085
|
+
return rows.length === 1;
|
|
1086
|
+
}
|
|
1087
|
+
async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
|
|
1088
|
+
if (this.hot) {
|
|
1089
|
+
const rows2 = await this.hot.query(`${LOCK_RUN}${this.hotScope.text.replace(/\$(\d+)/g, (_, n) => `$${Number(n) + 3}`)} RETURNING ${RUNS.list}`, [
|
|
1090
|
+
runId,
|
|
1091
|
+
owner,
|
|
1092
|
+
lockTime(leaseUntilMs),
|
|
1093
|
+
lockTime(nowMs),
|
|
1094
|
+
...this.hotScope.values
|
|
1095
|
+
]);
|
|
1096
|
+
return rows2[0] ? fromRunRow(RUNS.decode(rows2[0])) : null;
|
|
1097
|
+
}
|
|
1098
|
+
const rows = await this.db.update(durableWorkflowRuns).set({
|
|
1099
|
+
lockedBy: owner,
|
|
1100
|
+
lockedUntil: new Date(leaseUntilMs)
|
|
1101
|
+
}).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId), (0, import_drizzle_orm5.or)((0, import_drizzle_orm5.isNull)(durableWorkflowRuns.lockedUntil), (0, import_drizzle_orm5.lte)(durableWorkflowRuns.lockedUntil, new Date(nowMs))), this.scope())).returning();
|
|
1102
|
+
return rows[0] ? fromRunRow(rows[0]) : null;
|
|
1103
|
+
}
|
|
1104
|
+
/** Run `work` on the db as is when it is one statement (atomic by itself), else in a transaction. */
|
|
1105
|
+
inTransactionIf(single, work) {
|
|
1106
|
+
return single ? work(this.db) : this.db.transaction((tx) => work(tx));
|
|
1107
|
+
}
|
|
693
1108
|
async transaction(work) {
|
|
694
1109
|
return this.db.transaction(async (tx) => work({
|
|
695
1110
|
raw: tx,
|
|
@@ -698,55 +1113,106 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
698
1113
|
}
|
|
699
1114
|
/** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
|
|
700
1115
|
async listIncompleteRuns(namespace) {
|
|
701
|
-
const rows = await this.db.select().from(durableWorkflowRuns).where((0,
|
|
1116
|
+
const rows = await withTenantColumnsHint("pg", this.db.select().from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.inArray)(durableWorkflowRuns.status, [
|
|
702
1117
|
"running",
|
|
703
1118
|
"cancelling"
|
|
704
|
-
]), namespaceFilter(namespace), this.scope()));
|
|
1119
|
+
]), namespaceFilter(namespace), this.scope())));
|
|
705
1120
|
return rows.map(fromRunRow);
|
|
706
1121
|
}
|
|
707
1122
|
async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
|
|
708
1123
|
if (limit <= 0) return [];
|
|
709
|
-
const
|
|
1124
|
+
const query = this.db.select().from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.inArray)(durableWorkflowRuns.status, [
|
|
710
1125
|
"running",
|
|
711
1126
|
"cancelling"
|
|
712
|
-
]), (0,
|
|
713
|
-
(0,
|
|
714
|
-
(0,
|
|
1127
|
+
]), (0, import_drizzle_orm5.or)(
|
|
1128
|
+
(0, import_drizzle_orm5.isNull)(durableWorkflowRuns.lockedUntil),
|
|
1129
|
+
(0, import_drizzle_orm5.lte)(durableWorkflowRuns.lockedUntil, new Date(nowMs)),
|
|
715
1130
|
// A live lease left by a previous process with the caller's own instance id.
|
|
716
|
-
ownedBy ? (0,
|
|
717
|
-
), namespaceFilter(namespace), this.scope())).orderBy((0,
|
|
1131
|
+
ownedBy ? (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.lockedBy, ownedBy.owner), (0, import_drizzle_orm5.lt)(durableWorkflowRuns.lockedUntil, new Date(ownedBy.lockedBefore))) : void 0
|
|
1132
|
+
), namespaceFilter(namespace), this.scope())).orderBy((0, import_drizzle_orm5.asc)(durableWorkflowRuns.createdAt)).limit(limit);
|
|
1133
|
+
const rows = await withTenantColumnsHint("pg", query);
|
|
718
1134
|
return rows.map(fromRunRow);
|
|
719
1135
|
}
|
|
720
1136
|
/** FIFO pick-up, served by `durable_workflow_runs_namespace_status_idx`. The engine then leases
|
|
721
1137
|
* each run with {@link tryLockRun}; the listing itself takes no locks (it runs outside a tx). */
|
|
722
1138
|
async listPendingRuns(limit, namespace) {
|
|
723
|
-
const rows = await this.db.select().from(durableWorkflowRuns).where((0,
|
|
1139
|
+
const rows = await withTenantColumnsHint("pg", this.db.select().from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.status, "pending"), namespaceFilter(namespace), this.scope())).orderBy((0, import_drizzle_orm5.asc)(durableWorkflowRuns.createdAt)).limit(limit));
|
|
724
1140
|
return rows.map(fromRunRow);
|
|
725
1141
|
}
|
|
1142
|
+
async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
|
|
1143
|
+
if (limit <= 0) return [];
|
|
1144
|
+
const filters = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.status, "pending"), (0, import_drizzle_orm5.or)((0, import_drizzle_orm5.isNull)(durableWorkflowRuns.lockedUntil), (0, import_drizzle_orm5.lte)(durableWorkflowRuns.lockedUntil, new Date(nowMs))), namespaceFilter(namespace), this.scope());
|
|
1145
|
+
const returning = import_drizzle_orm5.sql.join(RUNS.columns.map(([, column]) => import_drizzle_orm5.sql`r.${import_drizzle_orm5.sql.identifier(column.name)}`), import_drizzle_orm5.sql`, `);
|
|
1146
|
+
const rows = rowsOf(
|
|
1147
|
+
// A CTE, MATERIALIZED, picks the rows once. As a subquery in UPDATE ... FROM, Postgres may
|
|
1148
|
+
// rescan it, and each rescan skips the rows this statement already updated and takes the
|
|
1149
|
+
// next ones, so a claim of `limit` rows could lease every free pending run.
|
|
1150
|
+
await withTenantColumnsHint("pg", this.db.execute(import_drizzle_orm5.sql`WITH c AS MATERIALIZED (
|
|
1151
|
+
SELECT ${durableWorkflowRuns.id} AS claimed_id FROM ${durableWorkflowRuns}
|
|
1152
|
+
WHERE ${filters}
|
|
1153
|
+
ORDER BY ${durableWorkflowRuns.createdAt}, ${durableWorkflowRuns.id}
|
|
1154
|
+
LIMIT ${limit}
|
|
1155
|
+
FOR UPDATE SKIP LOCKED
|
|
1156
|
+
)
|
|
1157
|
+
UPDATE ${durableWorkflowRuns} AS r
|
|
1158
|
+
SET ${import_drizzle_orm5.sql.identifier((0, import_drizzle_orm5.getTableColumns)(durableWorkflowRuns).lockedBy.name)} = ${owner},
|
|
1159
|
+
${import_drizzle_orm5.sql.identifier((0, import_drizzle_orm5.getTableColumns)(durableWorkflowRuns).lockedUntil.name)} = ${new Date(leaseUntilMs).toISOString()}::timestamptz
|
|
1160
|
+
FROM c
|
|
1161
|
+
WHERE r.${import_drizzle_orm5.sql.identifier((0, import_drizzle_orm5.getTableColumns)(durableWorkflowRuns).id.name)} = c.claimed_id
|
|
1162
|
+
RETURNING ${returning}`))
|
|
1163
|
+
);
|
|
1164
|
+
return rows.map((raw) => fromRunRow(RUNS.decode(raw))).sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id));
|
|
1165
|
+
}
|
|
726
1166
|
async listDueTimers(nowMs, namespace, limit) {
|
|
727
|
-
const query = this.db.select().from(durableWorkflowRuns).where((0,
|
|
728
|
-
const rows = limit !== void 0 ?
|
|
1167
|
+
const query = this.db.select().from(durableWorkflowRuns).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.status, "suspended"), (0, import_drizzle_orm5.isNotNull)(durableWorkflowRuns.wakeAt), (0, import_drizzle_orm5.lte)(durableWorkflowRuns.wakeAt, new Date(nowMs)), namespaceFilter(namespace), this.scope())).orderBy((0, import_drizzle_orm5.asc)(durableWorkflowRuns.wakeAt));
|
|
1168
|
+
const rows = await withTenantColumnsHint("pg", limit !== void 0 ? query.limit(limit) : query);
|
|
729
1169
|
return rows.map(fromRunRow);
|
|
730
1170
|
}
|
|
731
1171
|
async tryLockRun(runId, owner, leaseUntilMs, nowMs) {
|
|
1172
|
+
if (this.hot) {
|
|
1173
|
+
const rows2 = await this.hot.query(`${LOCK_RUN} RETURNING "id"`, [
|
|
1174
|
+
runId,
|
|
1175
|
+
owner,
|
|
1176
|
+
lockTime(leaseUntilMs),
|
|
1177
|
+
lockTime(nowMs)
|
|
1178
|
+
]);
|
|
1179
|
+
return rows2.length === 1;
|
|
1180
|
+
}
|
|
732
1181
|
const rows = await this.db.update(durableWorkflowRuns).set({
|
|
733
1182
|
lockedBy: owner,
|
|
734
1183
|
lockedUntil: new Date(leaseUntilMs)
|
|
735
|
-
}).where((0,
|
|
1184
|
+
}).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId), (0, import_drizzle_orm5.or)((0, import_drizzle_orm5.isNull)(durableWorkflowRuns.lockedUntil), (0, import_drizzle_orm5.lte)(durableWorkflowRuns.lockedUntil, new Date(nowMs))))).returning({
|
|
736
1185
|
id: durableWorkflowRuns.id
|
|
737
1186
|
});
|
|
738
1187
|
return rows.length === 1;
|
|
739
1188
|
}
|
|
740
1189
|
async releaseRunLock(runId, owner) {
|
|
1190
|
+
if (this.hot) {
|
|
1191
|
+
await (owner === void 0 ? this.hot.query(RELEASE_LOCK, [
|
|
1192
|
+
runId
|
|
1193
|
+
]) : this.hot.query(`${RELEASE_LOCK} AND "locked_by" = $2`, [
|
|
1194
|
+
runId,
|
|
1195
|
+
owner
|
|
1196
|
+
]));
|
|
1197
|
+
return;
|
|
1198
|
+
}
|
|
741
1199
|
await this.db.update(durableWorkflowRuns).set({
|
|
742
1200
|
lockedBy: null,
|
|
743
1201
|
lockedUntil: null
|
|
744
|
-
}).where(owner === void 0 ? (0,
|
|
1202
|
+
}).where(owner === void 0 ? (0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId) : (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId), (0, import_drizzle_orm5.eq)(durableWorkflowRuns.lockedBy, owner)));
|
|
745
1203
|
}
|
|
746
1204
|
async renewRunLock(runId, owner, leaseUntilMs) {
|
|
1205
|
+
if (this.hot) {
|
|
1206
|
+
const rows2 = await this.hot.query(RENEW_LOCK, [
|
|
1207
|
+
runId,
|
|
1208
|
+
owner,
|
|
1209
|
+
lockTime(leaseUntilMs)
|
|
1210
|
+
]);
|
|
1211
|
+
return rows2.length === 1;
|
|
1212
|
+
}
|
|
747
1213
|
const rows = await this.db.update(durableWorkflowRuns).set({
|
|
748
1214
|
lockedUntil: new Date(leaseUntilMs)
|
|
749
|
-
}).where((0,
|
|
1215
|
+
}).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableWorkflowRuns.id, runId), (0, import_drizzle_orm5.eq)(durableWorkflowRuns.lockedBy, owner))).returning({
|
|
750
1216
|
id: durableWorkflowRuns.id
|
|
751
1217
|
});
|
|
752
1218
|
return rows.length === 1;
|
|
@@ -755,19 +1221,21 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
755
1221
|
runFilters(query) {
|
|
756
1222
|
return [
|
|
757
1223
|
this.scope(),
|
|
758
|
-
query.workflow ? (0,
|
|
759
|
-
query.workflows ? query.workflows.length ? (0,
|
|
760
|
-
query.excludeWorkflows?.length ? (0,
|
|
761
|
-
query.updatedAfter !== void 0 ? (0,
|
|
1224
|
+
query.workflow ? (0, import_drizzle_orm5.eq)(durableWorkflowRuns.workflow, query.workflow) : void 0,
|
|
1225
|
+
query.workflows ? query.workflows.length ? (0, import_drizzle_orm5.inArray)(durableWorkflowRuns.workflow, query.workflows) : import_drizzle_orm5.sql`1 = 0` : void 0,
|
|
1226
|
+
query.excludeWorkflows?.length ? (0, import_drizzle_orm5.notInArray)(durableWorkflowRuns.workflow, query.excludeWorkflows) : void 0,
|
|
1227
|
+
query.updatedAfter !== void 0 ? (0, import_drizzle_orm5.gte)(durableWorkflowRuns.updatedAt, new Date(query.updatedAfter)) : void 0,
|
|
762
1228
|
// The tenant boundary. Also the engine's execution-timeout sweep path
|
|
763
1229
|
// (`listRuns({ workflow, status, namespace })`) — unscoped, it would cancel another tenant's runs.
|
|
764
1230
|
namespaceFilter(query.namespace),
|
|
765
|
-
query.namespaces ? query.namespaces.length ? (0,
|
|
1231
|
+
query.namespaces ? query.namespaces.length ? (0, import_drizzle_orm5.inArray)(durableWorkflowRuns.namespace, query.namespaces) : import_drizzle_orm5.sql`1 = 0` : void 0,
|
|
766
1232
|
// `null` selects the unattributed bucket; a value never matches a NULL origin.
|
|
767
|
-
query.origin === null ? (0,
|
|
768
|
-
query.origins ? anyOf(query.origins.map((o) => o === null ? (0,
|
|
769
|
-
|
|
770
|
-
|
|
1233
|
+
query.origin === null ? (0, import_drizzle_orm5.isNull)(durableWorkflowRuns.origin) : query.origin !== void 0 ? (0, import_drizzle_orm5.eq)(durableWorkflowRuns.origin, query.origin) : void 0,
|
|
1234
|
+
query.origins ? anyOf(query.origins.map((o) => o === null ? (0, import_drizzle_orm5.isNull)(durableWorkflowRuns.origin) : (0, import_drizzle_orm5.eq)(durableWorkflowRuns.origin, o))) : void 0,
|
|
1235
|
+
// The customer (tenancy matching rule): `null` = runs with no tenant.
|
|
1236
|
+
...tenantFilters(durableWorkflowRuns.tenantId, query),
|
|
1237
|
+
query.status ? (0, import_drizzle_orm5.eq)(durableWorkflowRuns.status, query.status) : void 0,
|
|
1238
|
+
query.statuses ? query.statuses.length ? (0, import_drizzle_orm5.inArray)(durableWorkflowRuns.status, query.statuses) : import_drizzle_orm5.sql`1 = 0` : void 0,
|
|
771
1239
|
query.tag ? hasTag(query.tag) : void 0,
|
|
772
1240
|
query.tags ? anyOf(query.tags.map(hasTag)) : void 0,
|
|
773
1241
|
// One EXISTS per attribute predicate against the normalized side table (ANDed).
|
|
@@ -776,7 +1244,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
776
1244
|
}
|
|
777
1245
|
async listRuns(query) {
|
|
778
1246
|
const filters = this.runFilters(query);
|
|
779
|
-
const base = this.db.select().from(durableWorkflowRuns).where(filters.length ? (0,
|
|
1247
|
+
const base = this.db.select().from(durableWorkflowRuns).where(filters.length ? (0, import_drizzle_orm5.and)(...filters) : void 0).orderBy((0, import_drizzle_orm5.desc)(durableWorkflowRuns.createdAt), (0, import_drizzle_orm5.desc)(durableWorkflowRuns.id)).$dynamic();
|
|
780
1248
|
if (query.limit != null) base.limit(query.limit);
|
|
781
1249
|
if (query.offset != null) base.offset(query.offset);
|
|
782
1250
|
const rows = await base;
|
|
@@ -785,8 +1253,8 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
785
1253
|
async countRuns(query) {
|
|
786
1254
|
const filters = this.runFilters(query);
|
|
787
1255
|
const rows = await this.db.select({
|
|
788
|
-
count:
|
|
789
|
-
}).from(durableWorkflowRuns).where(filters.length ? (0,
|
|
1256
|
+
count: import_drizzle_orm5.sql`count(*)`.mapWith(Number)
|
|
1257
|
+
}).from(durableWorkflowRuns).where(filters.length ? (0, import_drizzle_orm5.and)(...filters) : void 0);
|
|
790
1258
|
return rows[0]?.count ?? 0;
|
|
791
1259
|
}
|
|
792
1260
|
async runFacets(query) {
|
|
@@ -794,8 +1262,8 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
794
1262
|
const rows = await this.db.select({
|
|
795
1263
|
status: durableWorkflowRuns.status,
|
|
796
1264
|
origin: durableWorkflowRuns.origin,
|
|
797
|
-
count:
|
|
798
|
-
}).from(durableWorkflowRuns).where(filters.length ? (0,
|
|
1265
|
+
count: import_drizzle_orm5.sql`count(*)`.mapWith(Number)
|
|
1266
|
+
}).from(durableWorkflowRuns).where(filters.length ? (0, import_drizzle_orm5.and)(...filters) : void 0).groupBy(durableWorkflowRuns.status, durableWorkflowRuns.origin);
|
|
799
1267
|
return (0, import_nestjs_durable_core.mergeRunFacetRows)(rows.map((r) => ({
|
|
800
1268
|
status: r.status,
|
|
801
1269
|
origin: r.origin,
|
|
@@ -816,8 +1284,8 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
816
1284
|
const filters = this.runFilters(query);
|
|
817
1285
|
const rows = await this.db.select({
|
|
818
1286
|
value: column,
|
|
819
|
-
count:
|
|
820
|
-
}).from(durableWorkflowRuns).where(filters.length ? (0,
|
|
1287
|
+
count: import_drizzle_orm5.sql`count(*)`.mapWith(Number)
|
|
1288
|
+
}).from(durableWorkflowRuns).where(filters.length ? (0, import_drizzle_orm5.and)(...filters) : void 0).groupBy(column);
|
|
821
1289
|
return (0, import_nestjs_durable_core.mergeRunValueFacetRows)(rows.map((r) => ({
|
|
822
1290
|
value: r.value ?? null,
|
|
823
1291
|
count: r.count
|
|
@@ -827,10 +1295,10 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
827
1295
|
* excludes runs lacking the key (EXISTS requires the row). Numbers compare `num_value`, strings
|
|
828
1296
|
* and booleans `str_value`; an `in` set ORs its members inside the one EXISTS. */
|
|
829
1297
|
attributeExists(f) {
|
|
830
|
-
const comparisons = (0, import_nestjs_durable_core.attributePredicateOperands)(f).map(({ column, comparator, operand }) =>
|
|
831
|
-
return (0,
|
|
832
|
-
one:
|
|
833
|
-
}).from(durableRunAttributes).where((0,
|
|
1298
|
+
const comparisons = (0, import_nestjs_durable_core.attributePredicateOperands)(f).map(({ column, comparator, operand }) => import_drizzle_orm5.sql`${column === "numValue" ? durableRunAttributes.numValue : durableRunAttributes.strValue} ${import_drizzle_orm5.sql.raw(comparator)} ${operand}`);
|
|
1299
|
+
return (0, import_drizzle_orm5.exists)(this.db.select({
|
|
1300
|
+
one: import_drizzle_orm5.sql`1`
|
|
1301
|
+
}).from(durableRunAttributes).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableRunAttributes.runId, durableWorkflowRuns.id), (0, import_drizzle_orm5.eq)(durableRunAttributes.key, f.key), anyOf(comparisons))));
|
|
834
1302
|
}
|
|
835
1303
|
// ---- persisted schedules ----------------------------------------------------------------------
|
|
836
1304
|
async saveSchedule(record) {
|
|
@@ -841,46 +1309,65 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
841
1309
|
});
|
|
842
1310
|
}
|
|
843
1311
|
async getSchedule(id) {
|
|
844
|
-
const rows = await this.db.select().from(durableSchedules).where((0,
|
|
1312
|
+
const rows = await this.db.select().from(durableSchedules).where((0, import_drizzle_orm5.eq)(durableSchedules.id, id)).limit(1);
|
|
845
1313
|
return rows[0] ? fromScheduleRow(rows[0]) : null;
|
|
846
1314
|
}
|
|
847
1315
|
/** A single conditional `UPDATE … RETURNING`: the compare-and-set on `next_fire_at` is atomic. */
|
|
848
1316
|
async updateSchedule(id, patch, expectedNextFireAt) {
|
|
849
1317
|
const set = toSchedulePatch(patch);
|
|
850
1318
|
if (!Object.keys(set).length) return await this.getSchedule(id) !== null;
|
|
851
|
-
const rows = await this.db.update(durableSchedules).set(set).where((0,
|
|
1319
|
+
const rows = await this.db.update(durableSchedules).set(set).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableSchedules.id, id), expectedNextFireAt === void 0 ? void 0 : expectedNextFireAt === null ? (0, import_drizzle_orm5.isNull)(durableSchedules.nextFireAt) : (0, import_drizzle_orm5.eq)(durableSchedules.nextFireAt, new Date(expectedNextFireAt)))).returning({
|
|
852
1320
|
id: durableSchedules.id
|
|
853
1321
|
});
|
|
854
1322
|
return rows.length > 0;
|
|
855
1323
|
}
|
|
856
1324
|
async deleteSchedule(id) {
|
|
857
|
-
const rows = await this.db.delete(durableSchedules).where((0,
|
|
1325
|
+
const rows = await this.db.delete(durableSchedules).where((0, import_drizzle_orm5.eq)(durableSchedules.id, id)).returning({
|
|
858
1326
|
id: durableSchedules.id
|
|
859
1327
|
});
|
|
860
1328
|
return rows.length > 0;
|
|
861
1329
|
}
|
|
862
1330
|
async listSchedules(query) {
|
|
863
|
-
const base = this.db.select().from(durableSchedules).where((0,
|
|
1331
|
+
const base = this.db.select().from(durableSchedules).where((0, import_drizzle_orm5.and)(query.namespace !== void 0 ? (0, import_drizzle_orm5.eq)(durableSchedules.namespace, query.namespace) : void 0, query.workflow !== void 0 ? (0, import_drizzle_orm5.eq)(durableSchedules.workflow, query.workflow) : void 0, query.paused !== void 0 ? (0, import_drizzle_orm5.eq)(durableSchedules.paused, query.paused) : void 0, query.tag !== void 0 ? import_drizzle_orm5.sql`${durableSchedules.tags} @> ${JSON.stringify([
|
|
864
1332
|
query.tag
|
|
865
|
-
])}::jsonb` : void 0, query.dueBy !== void 0 ? (0,
|
|
1333
|
+
])}::jsonb` : void 0, ...tenantFilters(durableSchedules.tenantId, query), query.dueBy !== void 0 ? (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableSchedules.paused, false), (0, import_drizzle_orm5.lte)(durableSchedules.nextFireAt, new Date(query.dueBy))) : void 0)).orderBy(import_drizzle_orm5.sql`${durableSchedules.nextFireAt} ASC NULLS LAST`, (0, import_drizzle_orm5.asc)(durableSchedules.id)).$dynamic();
|
|
866
1334
|
if (query.limit != null) base.limit(query.limit);
|
|
867
1335
|
if (query.offset != null) base.offset(query.offset);
|
|
868
|
-
return (await base).map(fromScheduleRow);
|
|
1336
|
+
return (await withTenantColumnsHint("pg", base)).map(fromScheduleRow);
|
|
869
1337
|
}
|
|
870
1338
|
async listCheckpoints(runId) {
|
|
871
|
-
const rows = await this.db.select().from(durableStepCheckpoints).where((0,
|
|
1339
|
+
const rows = await this.db.select().from(durableStepCheckpoints).where((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId)).orderBy((0, import_drizzle_orm5.asc)(durableStepCheckpoints.seq));
|
|
872
1340
|
return rows.map(fromCheckpointRow);
|
|
873
1341
|
}
|
|
874
1342
|
async listCheckpointsForReplay(runId) {
|
|
875
|
-
|
|
876
|
-
|
|
1343
|
+
if (this.hot) {
|
|
1344
|
+
const rows2 = (await this.hot.query(SELECT_REPLAY, [
|
|
1345
|
+
runId
|
|
1346
|
+
])).map((raw) => CPS_NO_INPUT.decode(raw));
|
|
1347
|
+
const open2 = rows2.filter((row) => row.status !== "completed").map((row) => row.seq);
|
|
1348
|
+
const inputs2 = /* @__PURE__ */ new Map();
|
|
1349
|
+
if (open2.length > 0) {
|
|
1350
|
+
for (const raw of await this.hot.query(SELECT_INPUTS, [
|
|
1351
|
+
runId,
|
|
1352
|
+
open2
|
|
1353
|
+
])) {
|
|
1354
|
+
inputs2.set(Number(raw.seq), CPS.decode(raw).input);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
return rows2.map((row) => fromCheckpointRow({
|
|
1358
|
+
...row,
|
|
1359
|
+
input: inputs2.get(row.seq) ?? null
|
|
1360
|
+
}));
|
|
1361
|
+
}
|
|
1362
|
+
const { input: _input, ...columns } = (0, import_drizzle_orm5.getTableColumns)(durableStepCheckpoints);
|
|
1363
|
+
const rows = await this.db.select(columns).from(durableStepCheckpoints).where((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId)).orderBy((0, import_drizzle_orm5.asc)(durableStepCheckpoints.seq));
|
|
877
1364
|
const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
|
|
878
1365
|
const inputs = /* @__PURE__ */ new Map();
|
|
879
1366
|
for (let at = 0; at < open.length; at += 500) {
|
|
880
1367
|
const found = await this.db.select({
|
|
881
1368
|
seq: durableStepCheckpoints.seq,
|
|
882
1369
|
input: durableStepCheckpoints.input
|
|
883
|
-
}).from(durableStepCheckpoints).where((0,
|
|
1370
|
+
}).from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.inArray)(durableStepCheckpoints.seq, open.slice(at, at + 500))));
|
|
884
1371
|
for (const row of found) inputs.set(row.seq, row.input);
|
|
885
1372
|
}
|
|
886
1373
|
return rows.map((row) => fromCheckpointRow({
|
|
@@ -889,33 +1376,51 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
889
1376
|
}));
|
|
890
1377
|
}
|
|
891
1378
|
async getLatestCheckpointByName(runId, name) {
|
|
892
|
-
const rows = await this.db.select().from(durableStepCheckpoints).where((0,
|
|
1379
|
+
const rows = await this.db.select().from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.eq)(durableStepCheckpoints.name, name))).orderBy((0, import_drizzle_orm5.desc)(durableStepCheckpoints.seq)).limit(1);
|
|
893
1380
|
return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
|
|
894
1381
|
}
|
|
895
1382
|
async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
|
|
896
1383
|
if (statuses.length === 0) return false;
|
|
1384
|
+
if (this.hot) {
|
|
1385
|
+
const rows2 = await this.hot.query(HAS_CHECKPOINTS, [
|
|
1386
|
+
runId,
|
|
1387
|
+
fromSeq,
|
|
1388
|
+
toSeq,
|
|
1389
|
+
statuses
|
|
1390
|
+
]);
|
|
1391
|
+
return rows2.length > 0;
|
|
1392
|
+
}
|
|
897
1393
|
const rows = await this.db.select({
|
|
898
1394
|
seq: durableStepCheckpoints.seq
|
|
899
|
-
}).from(durableStepCheckpoints).where((0,
|
|
1395
|
+
}).from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.gte)(durableStepCheckpoints.seq, fromSeq), (0, import_drizzle_orm5.lte)(durableStepCheckpoints.seq, toSeq), (0, import_drizzle_orm5.inArray)(durableStepCheckpoints.status, statuses))).orderBy((0, import_drizzle_orm5.desc)(durableStepCheckpoints.seq)).limit(1);
|
|
900
1396
|
return rows.length > 0;
|
|
901
1397
|
}
|
|
902
1398
|
async countCheckpoints(runId, fromSeq, toSeq, statuses) {
|
|
903
1399
|
if (statuses.length === 0) return 0;
|
|
1400
|
+
if (this.hot) {
|
|
1401
|
+
const rows2 = await this.hot.query(COUNT_CHECKPOINTS, [
|
|
1402
|
+
runId,
|
|
1403
|
+
fromSeq,
|
|
1404
|
+
toSeq,
|
|
1405
|
+
statuses
|
|
1406
|
+
]);
|
|
1407
|
+
return Number(rows2[0]?.n ?? 0);
|
|
1408
|
+
}
|
|
904
1409
|
const rows = await this.db.select({
|
|
905
|
-
count:
|
|
906
|
-
}).from(durableStepCheckpoints).where((0,
|
|
1410
|
+
count: import_drizzle_orm5.sql`count(*)`.mapWith(Number)
|
|
1411
|
+
}).from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.gte)(durableStepCheckpoints.seq, fromSeq), (0, import_drizzle_orm5.lte)(durableStepCheckpoints.seq, toSeq), (0, import_drizzle_orm5.inArray)(durableStepCheckpoints.status, statuses)));
|
|
907
1412
|
return rows[0]?.count ?? 0;
|
|
908
1413
|
}
|
|
909
1414
|
async listCheckpointsByNamePrefix(runId, prefixes) {
|
|
910
1415
|
if (prefixes.length === 0) return [];
|
|
911
|
-
const rows = await this.db.select().from(durableStepCheckpoints).where((0,
|
|
1416
|
+
const rows = await this.db.select().from(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.or)(...prefixes.map((p) => (0, import_drizzle_orm5.like)(durableStepCheckpoints.name, `${escapeLike(p)}%`))))).orderBy((0, import_drizzle_orm5.asc)(durableStepCheckpoints.seq));
|
|
912
1417
|
return rows.map(fromCheckpointRow);
|
|
913
1418
|
}
|
|
914
1419
|
/** {@link StateStore.checkpointStats}: one `GROUP BY parallel_group, status, name_kind` over the
|
|
915
1420
|
* run's checkpoints, so a wide fan comes back as a few bucket rows instead of every member. */
|
|
916
1421
|
async checkpointStats(runId, opts) {
|
|
917
1422
|
const c = durableStepCheckpoints;
|
|
918
|
-
const nameKind =
|
|
1423
|
+
const nameKind = import_drizzle_orm5.sql`CASE
|
|
919
1424
|
WHEN ${c.name} LIKE 'signal:child:%' THEN 'child'
|
|
920
1425
|
WHEN ${c.name} LIKE 'fan:%' THEN 'folded'
|
|
921
1426
|
WHEN ${c.name} LIKE 'spawn:%' THEN 'spawn'
|
|
@@ -924,17 +1429,17 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
924
1429
|
group: c.parallelGroup,
|
|
925
1430
|
status: c.status,
|
|
926
1431
|
nameKind,
|
|
927
|
-
count:
|
|
928
|
-
minSeq:
|
|
929
|
-
maxSeq:
|
|
930
|
-
minName:
|
|
931
|
-
maxName:
|
|
932
|
-
minEnqueuedAt:
|
|
933
|
-
minStartedAt:
|
|
934
|
-
maxStartedAt:
|
|
935
|
-
maxFinishedAt:
|
|
936
|
-
sumAttempts:
|
|
937
|
-
}).from(c).where((0,
|
|
1432
|
+
count: import_drizzle_orm5.sql`count(*)`.mapWith(Number),
|
|
1433
|
+
minSeq: import_drizzle_orm5.sql`min(${c.seq})`.mapWith(Number),
|
|
1434
|
+
maxSeq: import_drizzle_orm5.sql`max(${c.seq})`.mapWith(Number),
|
|
1435
|
+
minName: import_drizzle_orm5.sql`min(${c.name})`,
|
|
1436
|
+
maxName: import_drizzle_orm5.sql`max(${c.name})`,
|
|
1437
|
+
minEnqueuedAt: import_drizzle_orm5.sql`min(coalesce(${c.enqueuedAt}, ${c.startedAt}))`.mapWith(c.startedAt),
|
|
1438
|
+
minStartedAt: import_drizzle_orm5.sql`min(${c.startedAt})`.mapWith(c.startedAt),
|
|
1439
|
+
maxStartedAt: import_drizzle_orm5.sql`max(${c.startedAt})`.mapWith(c.startedAt),
|
|
1440
|
+
maxFinishedAt: import_drizzle_orm5.sql`max(${c.finishedAt})`.mapWith(c.finishedAt),
|
|
1441
|
+
sumAttempts: import_drizzle_orm5.sql`sum(${c.attempts})`.mapWith(Number)
|
|
1442
|
+
}).from(c).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(c.runId, runId), opts?.group !== void 0 ? (0, import_drizzle_orm5.eq)(c.parallelGroup, opts.group) : void 0)).groupBy(c.parallelGroup, c.status, import_drizzle_orm5.sql`name_kind`);
|
|
938
1443
|
return rows.map((r) => ({
|
|
939
1444
|
...r,
|
|
940
1445
|
group: r.group ?? null,
|
|
@@ -946,14 +1451,14 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
946
1451
|
async queryCheckpoints(runId, query) {
|
|
947
1452
|
if (query.statuses?.length === 0 || query.namePrefixes?.length === 0) return [];
|
|
948
1453
|
const c = durableStepCheckpoints;
|
|
949
|
-
const base = this.db.select().from(c).where((0,
|
|
950
|
-
(0,
|
|
951
|
-
query.group !== void 0 ? (0,
|
|
1454
|
+
const base = this.db.select().from(c).where((0, import_drizzle_orm5.and)(
|
|
1455
|
+
(0, import_drizzle_orm5.eq)(c.runId, runId),
|
|
1456
|
+
query.group !== void 0 ? (0, import_drizzle_orm5.eq)(c.parallelGroup, query.group) : void 0,
|
|
952
1457
|
// `NOT IN` is never true for a NULL group, so ungrouped rows are kept explicitly.
|
|
953
|
-
query.excludeGroups?.length ? (0,
|
|
954
|
-
query.statuses ? (0,
|
|
955
|
-
query.namePrefixes ? (0,
|
|
956
|
-
)).orderBy((0,
|
|
1458
|
+
query.excludeGroups?.length ? (0, import_drizzle_orm5.or)((0, import_drizzle_orm5.isNull)(c.parallelGroup), (0, import_drizzle_orm5.notInArray)(c.parallelGroup, query.excludeGroups)) : void 0,
|
|
1459
|
+
query.statuses ? (0, import_drizzle_orm5.inArray)(c.status, query.statuses) : void 0,
|
|
1460
|
+
query.namePrefixes ? (0, import_drizzle_orm5.or)(...query.namePrefixes.map((p) => (0, import_drizzle_orm5.like)(c.name, `${escapeLike(p)}%`))) : void 0
|
|
1461
|
+
)).orderBy((0, import_drizzle_orm5.asc)(c.seq)).$dynamic();
|
|
957
1462
|
if (query.limit != null) base.limit(query.limit);
|
|
958
1463
|
if (query.offset != null) base.offset(query.offset);
|
|
959
1464
|
return (await base).map(fromCheckpointRow);
|
|
@@ -973,12 +1478,12 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
973
1478
|
});
|
|
974
1479
|
}
|
|
975
1480
|
async takeSignalWaiter(token) {
|
|
976
|
-
const rows = await this.db.delete(durableSignalWaiters).where((0,
|
|
1481
|
+
const rows = await this.db.delete(durableSignalWaiters).where((0, import_drizzle_orm5.eq)(durableSignalWaiters.token, token)).returning();
|
|
977
1482
|
const row = rows[0];
|
|
978
1483
|
return row ? fromWaiterRow(row) : null;
|
|
979
1484
|
}
|
|
980
1485
|
async listSignalWaiters(prefix) {
|
|
981
|
-
const rows = await this.db.select().from(durableSignalWaiters).where((0,
|
|
1486
|
+
const rows = await this.db.select().from(durableSignalWaiters).where((0, import_drizzle_orm5.like)(durableSignalWaiters.token, `${escapeLike(prefix)}%`));
|
|
982
1487
|
return rows.map(fromWaiterRow);
|
|
983
1488
|
}
|
|
984
1489
|
async registerChildWaits(entries) {
|
|
@@ -1008,23 +1513,23 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1008
1513
|
}
|
|
1009
1514
|
async compactCheckpoints(runId, fromSeq, toSeq, into) {
|
|
1010
1515
|
await this.db.transaction(async (tx) => {
|
|
1011
|
-
await tx.delete(durableStepCheckpoints).where((0,
|
|
1516
|
+
await tx.delete(durableStepCheckpoints).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm5.gte)(durableStepCheckpoints.seq, fromSeq), (0, import_drizzle_orm5.lte)(durableStepCheckpoints.seq, toSeq)));
|
|
1012
1517
|
await upsertCheckpoint(tx, into);
|
|
1013
1518
|
});
|
|
1014
1519
|
}
|
|
1015
1520
|
async listSignalWaitersByRun(runIds) {
|
|
1016
1521
|
if (runIds.length === 0) return [];
|
|
1017
|
-
const rows = await this.db.select().from(durableSignalWaiters).where((0,
|
|
1522
|
+
const rows = await this.db.select().from(durableSignalWaiters).where((0, import_drizzle_orm5.inArray)(durableSignalWaiters.runId, runIds));
|
|
1018
1523
|
return rows.map(fromWaiterRow);
|
|
1019
1524
|
}
|
|
1020
1525
|
async hasSignalWaiter(runId, tokenPrefix) {
|
|
1021
1526
|
const rows = await this.db.select({
|
|
1022
1527
|
token: durableSignalWaiters.token
|
|
1023
|
-
}).from(durableSignalWaiters).where((0,
|
|
1528
|
+
}).from(durableSignalWaiters).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableSignalWaiters.runId, runId), (0, import_drizzle_orm5.like)(durableSignalWaiters.token, `${escapeLike(tokenPrefix)}%`))).limit(1);
|
|
1024
1529
|
return rows.length > 0;
|
|
1025
1530
|
}
|
|
1026
1531
|
async removeSignalWaiter(waiter) {
|
|
1027
|
-
await this.db.delete(durableSignalWaiters).where((0,
|
|
1532
|
+
await this.db.delete(durableSignalWaiters).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableSignalWaiters.token, waiter.token), (0, import_drizzle_orm5.eq)(durableSignalWaiters.runId, waiter.runId), (0, import_drizzle_orm5.eq)(durableSignalWaiters.seq, waiter.seq)));
|
|
1028
1533
|
}
|
|
1029
1534
|
async bufferSignal(token, payload) {
|
|
1030
1535
|
await this.db.insert(durableBufferedSignals).values({
|
|
@@ -1033,21 +1538,84 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1033
1538
|
});
|
|
1034
1539
|
}
|
|
1035
1540
|
async takeBufferedSignal(token) {
|
|
1541
|
+
if (this.hot) {
|
|
1542
|
+
const [row2] = await this.hot.query(TAKE_BUFFERED_SIGNAL, [
|
|
1543
|
+
token
|
|
1544
|
+
]);
|
|
1545
|
+
if (!row2) return null;
|
|
1546
|
+
const payload = row2.payload == null ? null : SIGNAL_PAYLOAD.mapFromDriverValue(row2.payload);
|
|
1547
|
+
return {
|
|
1548
|
+
payload: payload ?? void 0
|
|
1549
|
+
};
|
|
1550
|
+
}
|
|
1551
|
+
const oldest = this.db.select({
|
|
1552
|
+
id: durableBufferedSignals.id
|
|
1553
|
+
}).from(durableBufferedSignals).where((0, import_drizzle_orm5.eq)(durableBufferedSignals.token, token)).orderBy((0, import_drizzle_orm5.asc)(durableBufferedSignals.id)).limit(1).for("update", {
|
|
1554
|
+
skipLocked: true
|
|
1555
|
+
});
|
|
1556
|
+
const rows = await this.db.delete(durableBufferedSignals).where((0, import_drizzle_orm5.inArray)(durableBufferedSignals.id, oldest)).returning({
|
|
1557
|
+
payload: durableBufferedSignals.payload
|
|
1558
|
+
});
|
|
1559
|
+
const row = rows[0];
|
|
1560
|
+
return row ? {
|
|
1561
|
+
payload: row.payload ?? void 0
|
|
1562
|
+
} : null;
|
|
1563
|
+
}
|
|
1564
|
+
async takeOldestBufferedSignal(tokens) {
|
|
1565
|
+
if (tokens.length === 0) return null;
|
|
1036
1566
|
const oldest = this.db.select({
|
|
1037
1567
|
id: durableBufferedSignals.id
|
|
1038
|
-
}).from(durableBufferedSignals).where((0,
|
|
1568
|
+
}).from(durableBufferedSignals).where((0, import_drizzle_orm5.inArray)(durableBufferedSignals.token, [
|
|
1569
|
+
...new Set(tokens)
|
|
1570
|
+
])).orderBy((0, import_drizzle_orm5.asc)(durableBufferedSignals.id)).limit(1).for("update", {
|
|
1039
1571
|
skipLocked: true
|
|
1040
1572
|
});
|
|
1041
|
-
const rows = await this.db.delete(durableBufferedSignals).where((0,
|
|
1573
|
+
const rows = await this.db.delete(durableBufferedSignals).where((0, import_drizzle_orm5.inArray)(durableBufferedSignals.id, oldest)).returning({
|
|
1574
|
+
token: durableBufferedSignals.token,
|
|
1042
1575
|
payload: durableBufferedSignals.payload
|
|
1043
1576
|
});
|
|
1044
1577
|
const row = rows[0];
|
|
1045
1578
|
return row ? {
|
|
1579
|
+
token: row.token,
|
|
1046
1580
|
payload: row.payload ?? void 0
|
|
1047
1581
|
} : null;
|
|
1048
1582
|
}
|
|
1583
|
+
async claimSignalId(token, id, expiresAt, nowMs) {
|
|
1584
|
+
const rows = await this.db.insert(durableSignalIds).values({
|
|
1585
|
+
token,
|
|
1586
|
+
signalId: id,
|
|
1587
|
+
expiresAt: new Date(expiresAt)
|
|
1588
|
+
}).onConflictDoUpdate({
|
|
1589
|
+
target: [
|
|
1590
|
+
durableSignalIds.token,
|
|
1591
|
+
durableSignalIds.signalId
|
|
1592
|
+
],
|
|
1593
|
+
set: {
|
|
1594
|
+
expiresAt: new Date(expiresAt)
|
|
1595
|
+
},
|
|
1596
|
+
setWhere: (0, import_drizzle_orm5.lte)(durableSignalIds.expiresAt, new Date(nowMs))
|
|
1597
|
+
}).returning({
|
|
1598
|
+
token: durableSignalIds.token
|
|
1599
|
+
});
|
|
1600
|
+
return rows.length > 0;
|
|
1601
|
+
}
|
|
1602
|
+
async releaseSignalId(token, id) {
|
|
1603
|
+
await this.db.delete(durableSignalIds).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(durableSignalIds.token, token), (0, import_drizzle_orm5.eq)(durableSignalIds.signalId, id)));
|
|
1604
|
+
}
|
|
1605
|
+
async deleteExpiredSignalIds(nowMs, limit) {
|
|
1606
|
+
const expired = this.db.select({
|
|
1607
|
+
token: durableSignalIds.token,
|
|
1608
|
+
signalId: durableSignalIds.signalId
|
|
1609
|
+
}).from(durableSignalIds).where((0, import_drizzle_orm5.lte)(durableSignalIds.expiresAt, new Date(nowMs))).limit(limit).for("update", {
|
|
1610
|
+
skipLocked: true
|
|
1611
|
+
});
|
|
1612
|
+
const rows = await this.db.delete(durableSignalIds).where(import_drizzle_orm5.sql`(${durableSignalIds.token}, ${durableSignalIds.signalId}) in (${expired})`).returning({
|
|
1613
|
+
token: durableSignalIds.token
|
|
1614
|
+
});
|
|
1615
|
+
return rows.length;
|
|
1616
|
+
}
|
|
1049
1617
|
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
1050
|
-
const rows = await this.db.select().from(durableBufferedSignals).where((0,
|
|
1618
|
+
const rows = await this.db.select().from(durableBufferedSignals).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.like)(durableBufferedSignals.token, `${escapeLike(tokenPrefix)}%`), afterId !== void 0 ? (0, import_drizzle_orm5.gt)(durableBufferedSignals.id, Number(afterId)) : void 0)).orderBy((0, import_drizzle_orm5.asc)(durableBufferedSignals.id)).limit(limit);
|
|
1051
1619
|
return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
|
|
1052
1620
|
id: String(r.id),
|
|
1053
1621
|
token: r.token,
|
|
@@ -1056,7 +1624,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1056
1624
|
}
|
|
1057
1625
|
async deleteBufferedSignals(ids) {
|
|
1058
1626
|
if (ids.length === 0) return;
|
|
1059
|
-
await this.db.delete(durableBufferedSignals).where((0,
|
|
1627
|
+
await this.db.delete(durableBufferedSignals).where((0, import_drizzle_orm5.inArray)(durableBufferedSignals.id, ids.map(Number)));
|
|
1060
1628
|
}
|
|
1061
1629
|
async bufferEvent(input) {
|
|
1062
1630
|
await this.db.insert(durableBufferedEvents).values({
|
|
@@ -1067,7 +1635,20 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1067
1635
|
});
|
|
1068
1636
|
}
|
|
1069
1637
|
async listBufferedEvents(name, limit) {
|
|
1070
|
-
|
|
1638
|
+
if (this.hot) {
|
|
1639
|
+
return (await this.hot.query(LIST_BUFFERED_EVENTS, [
|
|
1640
|
+
name,
|
|
1641
|
+
limit
|
|
1642
|
+
])).map((raw) => {
|
|
1643
|
+
const r = EVENTS.decode(raw);
|
|
1644
|
+
return {
|
|
1645
|
+
id: r.id,
|
|
1646
|
+
payload: r.payload ?? void 0,
|
|
1647
|
+
publishedAt: r.publishedAt.getTime()
|
|
1648
|
+
};
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
const rows = await this.db.select().from(durableBufferedEvents).where((0, import_drizzle_orm5.eq)(durableBufferedEvents.name, name)).orderBy((0, import_drizzle_orm5.asc)(durableBufferedEvents.publishedAt), (0, import_drizzle_orm5.asc)(durableBufferedEvents.id)).limit(limit);
|
|
1071
1652
|
return rows.map((r) => ({
|
|
1072
1653
|
id: r.id,
|
|
1073
1654
|
payload: r.payload ?? void 0,
|
|
@@ -1075,7 +1656,10 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1075
1656
|
}));
|
|
1076
1657
|
}
|
|
1077
1658
|
async removeBufferedEvent(id) {
|
|
1078
|
-
|
|
1659
|
+
if (this.hot) return (await this.hot.query(REMOVE_BUFFERED_EVENT, [
|
|
1660
|
+
id
|
|
1661
|
+
])).length === 1;
|
|
1662
|
+
const rows = await this.db.delete(durableBufferedEvents).where((0, import_drizzle_orm5.eq)(durableBufferedEvents.id, id)).returning({
|
|
1079
1663
|
id: durableBufferedEvents.id
|
|
1080
1664
|
});
|
|
1081
1665
|
return rows.length === 1;
|
|
@@ -1086,6 +1670,68 @@ function escapeLike(value) {
|
|
|
1086
1670
|
}
|
|
1087
1671
|
__name(escapeLike, "escapeLike");
|
|
1088
1672
|
var CHECKPOINT_BATCH_ROWS = 500;
|
|
1673
|
+
var RUNS = new TableCodec(durableWorkflowRuns);
|
|
1674
|
+
var CPS = new TableCodec(durableStepCheckpoints);
|
|
1675
|
+
var CPS_NO_INPUT = new TableCodec(durableStepCheckpoints, [
|
|
1676
|
+
"input"
|
|
1677
|
+
]);
|
|
1678
|
+
var EVENTS = new TableCodec(durableBufferedEvents);
|
|
1679
|
+
var SIGNAL_PAYLOAD = (0, import_drizzle_orm5.getTableColumns)(durableBufferedSignals).payload;
|
|
1680
|
+
var T_RUNS = quote((0, import_drizzle_orm5.getTableName)(durableWorkflowRuns));
|
|
1681
|
+
var T_CPS = quote((0, import_drizzle_orm5.getTableName)(durableStepCheckpoints));
|
|
1682
|
+
var T_EVENTS = quote((0, import_drizzle_orm5.getTableName)(durableBufferedEvents));
|
|
1683
|
+
var T_SIGNALS = quote((0, import_drizzle_orm5.getTableName)(durableBufferedSignals));
|
|
1684
|
+
var T_ATTRIBUTES = quote((0, import_drizzle_orm5.getTableName)(durableRunAttributes));
|
|
1685
|
+
var SELECT_RUN = `SELECT ${RUNS.list} FROM ${T_RUNS} WHERE "id" = $1`;
|
|
1686
|
+
var INSERT_RUN = `INSERT INTO ${T_RUNS} (${RUNS.list}) VALUES (${placeholders(1, RUNS.columns.length)})`;
|
|
1687
|
+
var DELETE_ATTRIBUTES = `DELETE FROM ${T_ATTRIBUTES} WHERE "run_id" = $1`;
|
|
1688
|
+
var LOCK_RUN = `UPDATE ${T_RUNS} SET "locked_by" = $2, "locked_until" = $3 WHERE "id" = $1 AND ("locked_until" IS NULL OR "locked_until" <= $4)`;
|
|
1689
|
+
var RELEASE_LOCK = `UPDATE ${T_RUNS} SET "locked_by" = NULL, "locked_until" = NULL WHERE "id" = $1`;
|
|
1690
|
+
var RENEW_LOCK = `UPDATE ${T_RUNS} SET "locked_until" = $3 WHERE "id" = $1 AND "locked_by" = $2 RETURNING "id"`;
|
|
1691
|
+
var SELECT_CHECKPOINT = `SELECT ${CPS.list} FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = $2 LIMIT 1`;
|
|
1692
|
+
var SELECT_CHECKPOINTS_FOR_SETTLE = `SELECT ${CPS_NO_INPUT.list} FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = ANY($2::integer[]) ORDER BY "seq"`;
|
|
1693
|
+
var SELECT_REPLAY = `SELECT ${CPS_NO_INPUT.list} FROM ${T_CPS} WHERE "run_id" = $1 ORDER BY "seq"`;
|
|
1694
|
+
var SELECT_INPUTS = `SELECT "seq", "input" FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = ANY($2::integer[])`;
|
|
1695
|
+
var CHECKPOINT_VALUES = `(${CPS.list}) VALUES (${placeholders(1, CPS.columns.length)})`;
|
|
1696
|
+
var UPSERT_CHECKPOINT = `INSERT INTO ${T_CPS} ${CHECKPOINT_VALUES} ON CONFLICT ("run_id", "seq") DO UPDATE SET ${CPS.columns.map(([, c]) => `${quote(c.name)} = EXCLUDED.${quote(c.name)}`).join(", ")}`;
|
|
1697
|
+
var INSERT_CHECKPOINT_IF_ABSENT = `INSERT INTO ${T_CPS} ${CHECKPOINT_VALUES} ON CONFLICT ("run_id", "seq") DO NOTHING RETURNING "seq"`;
|
|
1698
|
+
var CHECKPOINT_RANGE = `FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" >= $2 AND "seq" <= $3 AND "status" = ANY($4::text[])`;
|
|
1699
|
+
var HAS_CHECKPOINTS = `SELECT "seq" ${CHECKPOINT_RANGE} ORDER BY "seq" DESC LIMIT 1`;
|
|
1700
|
+
var COUNT_CHECKPOINTS = `SELECT count(*)::integer AS "n" ${CHECKPOINT_RANGE}`;
|
|
1701
|
+
var LIST_BUFFERED_EVENTS = `SELECT ${EVENTS.list} FROM ${T_EVENTS} WHERE "name" = $1 ORDER BY "published_at", "id" LIMIT $2`;
|
|
1702
|
+
var REMOVE_BUFFERED_EVENT = `DELETE FROM ${T_EVENTS} WHERE "id" = $1 RETURNING "id"`;
|
|
1703
|
+
var TAKE_BUFFERED_SIGNAL = `WITH taken AS MATERIALIZED (SELECT "id" FROM ${T_SIGNALS} WHERE "token" = $1 ORDER BY "id" LIMIT 1 FOR UPDATE SKIP LOCKED) DELETE FROM ${T_SIGNALS} WHERE "id" IN (SELECT "id" FROM taken) RETURNING "payload"`;
|
|
1704
|
+
var HOT_SETTLE_MAX_ROWS = 64;
|
|
1705
|
+
var settleTexts = /* @__PURE__ */ new Map();
|
|
1706
|
+
var lockTime = /* @__PURE__ */ __name((ms2) => RUNS.encode("lockedUntil", new Date(ms2)), "lockTime");
|
|
1707
|
+
function settleResultsText(n) {
|
|
1708
|
+
let text2 = settleTexts.get(n);
|
|
1709
|
+
if (text2 !== void 0) return text2;
|
|
1710
|
+
const rows = Array.from({
|
|
1711
|
+
length: n
|
|
1712
|
+
}, (_, r) => {
|
|
1713
|
+
const p = /* @__PURE__ */ __name((k) => `$${r * 8 + k}`, "p");
|
|
1714
|
+
return `(${p(1)}::text, ${p(2)}::integer, ${p(3)}::text, ${p(4)}::jsonb, ${p(5)}::jsonb, ${p(6)}::jsonb, ${p(7)}::timestamptz, ${p(8)}::timestamptz)`;
|
|
1715
|
+
});
|
|
1716
|
+
text2 = `UPDATE ${T_CPS} AS c SET status = v.status, output = CASE WHEN v.status = 'completed' THEN v.output ELSE c.output END, error = v.error, events = COALESCE(v.events, c.events), started_at = COALESCE(v.started_at, c.started_at), finished_at = v.finished_at FROM (VALUES ${rows.join(", ")}) AS v(run_id, seq, status, output, error, events, started_at, finished_at) WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending' AND NOT EXISTS (SELECT 1 FROM ${T_RUNS} AS r WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed')) RETURNING ${CPS_NO_INPUT.columns.map(([, c]) => `c.${quote(c.name)}`).join(", ")}`;
|
|
1717
|
+
settleTexts.set(n, text2);
|
|
1718
|
+
return text2;
|
|
1719
|
+
}
|
|
1720
|
+
__name(settleResultsText, "settleResultsText");
|
|
1721
|
+
function settleResultValues(r) {
|
|
1722
|
+
const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
|
|
1723
|
+
return [
|
|
1724
|
+
r.runId,
|
|
1725
|
+
r.seq,
|
|
1726
|
+
r.status,
|
|
1727
|
+
json(r.status === "completed" ? r.output : null),
|
|
1728
|
+
json(r.error),
|
|
1729
|
+
json(r.events),
|
|
1730
|
+
r.startedAt ? r.startedAt.toISOString() : null,
|
|
1731
|
+
r.finishedAt.toISOString()
|
|
1732
|
+
];
|
|
1733
|
+
}
|
|
1734
|
+
__name(settleResultValues, "settleResultValues");
|
|
1089
1735
|
var rowKey = /* @__PURE__ */ __name((cp) => `${cp.seq}:${cp.runId}`, "rowKey");
|
|
1090
1736
|
function distinctRounds(checkpoints) {
|
|
1091
1737
|
const rounds = [];
|
|
@@ -1106,14 +1752,14 @@ function updateIfPendingFrom(checkpoints) {
|
|
|
1106
1752
|
const ts = /* @__PURE__ */ __name((v) => v == null ? null : v.toISOString(), "ts");
|
|
1107
1753
|
const values = checkpoints.map((cp) => {
|
|
1108
1754
|
const r = toCheckpointRow(cp);
|
|
1109
|
-
return
|
|
1755
|
+
return import_drizzle_orm5.sql`(${r.runId}::text, ${r.seq}::integer, ${r.name}::text, ${r.kind}::text, ${r.stepId}::text, ${r.status}::text, ${json(r.input)}::jsonb, ${json(r.output)}::jsonb, ${json(r.error)}::jsonb, ${json(r.events)}::jsonb, ${r.attempts}::integer, ${r.workerGroup}::text, ${r.parallelGroup}::text, ${ts(r.wakeAt)}::timestamptz, ${ts(r.enqueuedAt)}::timestamptz, ${ts(r.startedAt)}::timestamptz, ${ts(r.finishedAt)}::timestamptz)`;
|
|
1110
1756
|
});
|
|
1111
|
-
return
|
|
1757
|
+
return import_drizzle_orm5.sql`UPDATE ${durableStepCheckpoints} AS c SET
|
|
1112
1758
|
name = v.name, kind = v.kind, step_id = v.step_id, status = v.status, input = v.input,
|
|
1113
1759
|
output = v.output, error = v.error, events = v.events, attempts = v.attempts,
|
|
1114
1760
|
worker_group = v.worker_group, parallel_group = v.parallel_group, wake_at = v.wake_at,
|
|
1115
1761
|
enqueued_at = v.enqueued_at, started_at = v.started_at, finished_at = v.finished_at
|
|
1116
|
-
FROM (VALUES ${
|
|
1762
|
+
FROM (VALUES ${import_drizzle_orm5.sql.join(values, import_drizzle_orm5.sql`, `)}) AS v(run_id, seq, name, kind, step_id, status, input,
|
|
1117
1763
|
output, error, events, attempts, worker_group, parallel_group, wake_at, enqueued_at, started_at,
|
|
1118
1764
|
finished_at)
|
|
1119
1765
|
WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
|
|
@@ -1122,16 +1768,58 @@ function updateIfPendingFrom(checkpoints) {
|
|
|
1122
1768
|
__name(updateIfPendingFrom, "updateIfPendingFrom");
|
|
1123
1769
|
function settleIfPendingFrom(settlements) {
|
|
1124
1770
|
const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
|
|
1125
|
-
const values = settlements.map((s) =>
|
|
1126
|
-
return
|
|
1771
|
+
const values = settlements.map((s) => import_drizzle_orm5.sql`(${s.runId}::text, ${s.seq}::integer, ${s.status}::text, ${json(s.output)}::jsonb, ${json(s.error)}::jsonb, ${json(s.events)}::jsonb, ${s.startedAt.toISOString()}::timestamptz, ${s.finishedAt.toISOString()}::timestamptz)`);
|
|
1772
|
+
return import_drizzle_orm5.sql`UPDATE ${durableStepCheckpoints} AS c SET
|
|
1127
1773
|
status = v.status, output = v.output, error = v.error, events = v.events,
|
|
1128
1774
|
started_at = v.started_at, finished_at = v.finished_at
|
|
1129
|
-
FROM (VALUES ${
|
|
1775
|
+
FROM (VALUES ${import_drizzle_orm5.sql.join(values, import_drizzle_orm5.sql`, `)}) AS v(run_id, seq, status, output, error, events,
|
|
1130
1776
|
started_at, finished_at)
|
|
1131
1777
|
WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
|
|
1132
1778
|
RETURNING c.run_id, c.seq`;
|
|
1133
1779
|
}
|
|
1134
1780
|
__name(settleIfPendingFrom, "settleIfPendingFrom");
|
|
1781
|
+
function oneStatement(rows) {
|
|
1782
|
+
if (rows.length > CHECKPOINT_BATCH_ROWS) return false;
|
|
1783
|
+
const keys = new Set(rows.map(rowKey));
|
|
1784
|
+
return keys.size === rows.length;
|
|
1785
|
+
}
|
|
1786
|
+
__name(oneStatement, "oneStatement");
|
|
1787
|
+
var SETTLED_COLUMNS = (() => {
|
|
1788
|
+
const { input: _input, ...columns } = (0, import_drizzle_orm5.getTableColumns)(durableStepCheckpoints);
|
|
1789
|
+
return columns;
|
|
1790
|
+
})();
|
|
1791
|
+
function settledRowFromDriver(raw) {
|
|
1792
|
+
const row = {
|
|
1793
|
+
input: null
|
|
1794
|
+
};
|
|
1795
|
+
for (const [key, column] of Object.entries(SETTLED_COLUMNS)) {
|
|
1796
|
+
const value = raw[column.name];
|
|
1797
|
+
row[key] = value == null ? null : column.mapFromDriverValue(value);
|
|
1798
|
+
}
|
|
1799
|
+
return row;
|
|
1800
|
+
}
|
|
1801
|
+
__name(settledRowFromDriver, "settledRowFromDriver");
|
|
1802
|
+
function settleResultsFrom(results) {
|
|
1803
|
+
const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
|
|
1804
|
+
const values = results.map((r) => import_drizzle_orm5.sql`(${r.runId}::text, ${r.seq}::integer, ${r.status}::text, ${json(r.status === "completed" ? r.output : null)}::jsonb, ${json(r.error)}::jsonb, ${json(r.events)}::jsonb, ${r.startedAt ? r.startedAt.toISOString() : null}::timestamptz, ${r.finishedAt.toISOString()}::timestamptz)`);
|
|
1805
|
+
const returning = import_drizzle_orm5.sql.join(Object.values(SETTLED_COLUMNS).map((column) => import_drizzle_orm5.sql`c.${import_drizzle_orm5.sql.identifier(column.name)}`), import_drizzle_orm5.sql`, `);
|
|
1806
|
+
return import_drizzle_orm5.sql`UPDATE ${durableStepCheckpoints} AS c SET
|
|
1807
|
+
status = v.status,
|
|
1808
|
+
output = CASE WHEN v.status = 'completed' THEN v.output ELSE c.output END,
|
|
1809
|
+
error = v.error,
|
|
1810
|
+
events = COALESCE(v.events, c.events),
|
|
1811
|
+
started_at = COALESCE(v.started_at, c.started_at),
|
|
1812
|
+
finished_at = v.finished_at
|
|
1813
|
+
FROM (VALUES ${import_drizzle_orm5.sql.join(values, import_drizzle_orm5.sql`, `)}) AS v(run_id, seq, status, output, error, events,
|
|
1814
|
+
started_at, finished_at)
|
|
1815
|
+
WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
|
|
1816
|
+
AND NOT EXISTS (
|
|
1817
|
+
SELECT 1 FROM ${durableWorkflowRuns} AS r
|
|
1818
|
+
WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed')
|
|
1819
|
+
)
|
|
1820
|
+
RETURNING ${returning}`;
|
|
1821
|
+
}
|
|
1822
|
+
__name(settleResultsFrom, "settleResultsFrom");
|
|
1135
1823
|
async function upsertCheckpoint(db, cp) {
|
|
1136
1824
|
const row = toCheckpointRow(cp);
|
|
1137
1825
|
await db.insert(durableStepCheckpoints).values(row).onConflictDoUpdate({
|
|
@@ -1144,7 +1832,7 @@ async function upsertCheckpoint(db, cp) {
|
|
|
1144
1832
|
}
|
|
1145
1833
|
__name(upsertCheckpoint, "upsertCheckpoint");
|
|
1146
1834
|
async function reindexAttributes(db, runId, attributes) {
|
|
1147
|
-
await db.delete(durableRunAttributes).where((0,
|
|
1835
|
+
await db.delete(durableRunAttributes).where((0, import_drizzle_orm5.eq)(durableRunAttributes.runId, runId));
|
|
1148
1836
|
const rows = (0, import_nestjs_durable_core.normalizeAttributeRows)(runId, attributes);
|
|
1149
1837
|
if (rows.length) await db.insert(durableRunAttributes).values(rows);
|
|
1150
1838
|
}
|
|
@@ -1152,11 +1840,11 @@ __name(reindexAttributes, "reindexAttributes");
|
|
|
1152
1840
|
async function deleteRunsCascade(db, runIds) {
|
|
1153
1841
|
await db.select({
|
|
1154
1842
|
id: durableWorkflowRuns.id
|
|
1155
|
-
}).from(durableWorkflowRuns).where((0,
|
|
1156
|
-
await db.delete(durableStepCheckpoints).where((0,
|
|
1157
|
-
await db.delete(durableSignalWaiters).where((0,
|
|
1158
|
-
await db.delete(durableRunAttributes).where((0,
|
|
1159
|
-
await db.delete(durableWorkflowRuns).where((0,
|
|
1843
|
+
}).from(durableWorkflowRuns).where((0, import_drizzle_orm5.inArray)(durableWorkflowRuns.id, runIds)).orderBy((0, import_drizzle_orm5.asc)(durableWorkflowRuns.id)).for("update");
|
|
1844
|
+
await db.delete(durableStepCheckpoints).where((0, import_drizzle_orm5.inArray)(durableStepCheckpoints.runId, runIds));
|
|
1845
|
+
await db.delete(durableSignalWaiters).where((0, import_drizzle_orm5.inArray)(durableSignalWaiters.runId, runIds));
|
|
1846
|
+
await db.delete(durableRunAttributes).where((0, import_drizzle_orm5.inArray)(durableRunAttributes.runId, runIds));
|
|
1847
|
+
await db.delete(durableWorkflowRuns).where((0, import_drizzle_orm5.inArray)(durableWorkflowRuns.id, runIds));
|
|
1160
1848
|
}
|
|
1161
1849
|
__name(deleteRunsCascade, "deleteRunsCascade");
|
|
1162
1850
|
function fromWaiterRow(row) {
|
|
@@ -1187,6 +1875,8 @@ function toRunRow(run) {
|
|
|
1187
1875
|
priority: run.priority ?? null,
|
|
1188
1876
|
namespace: run.namespace || DEFAULT_NAMESPACE,
|
|
1189
1877
|
origin: run.origin ?? null,
|
|
1878
|
+
tenantId: run.tenantId ?? null,
|
|
1879
|
+
tenantKind: run.tenantKind ?? null,
|
|
1190
1880
|
createdAt: run.createdAt,
|
|
1191
1881
|
updatedAt: run.updatedAt
|
|
1192
1882
|
};
|
|
@@ -1210,6 +1900,8 @@ function toRunPatch(patch) {
|
|
|
1210
1900
|
if ("priority" in patch) row.priority = patch.priority ?? null;
|
|
1211
1901
|
if ("namespace" in patch) row.namespace = patch.namespace || DEFAULT_NAMESPACE;
|
|
1212
1902
|
if ("origin" in patch) row.origin = patch.origin ?? null;
|
|
1903
|
+
if ("tenantId" in patch) row.tenantId = patch.tenantId ?? null;
|
|
1904
|
+
if ("tenantKind" in patch) row.tenantKind = patch.tenantKind ?? null;
|
|
1213
1905
|
if (patch.createdAt != null) row.createdAt = patch.createdAt;
|
|
1214
1906
|
if (patch.updatedAt != null) row.updatedAt = patch.updatedAt;
|
|
1215
1907
|
return row;
|
|
@@ -1234,6 +1926,13 @@ function fromRunRow(row) {
|
|
|
1234
1926
|
priority: row.priority ?? void 0,
|
|
1235
1927
|
namespace: row.namespace || DEFAULT_NAMESPACE,
|
|
1236
1928
|
origin: row.origin ?? void 0,
|
|
1929
|
+
// NULL = no tenant; left off entirely so a tenancy-off run reads back unchanged.
|
|
1930
|
+
...row.tenantId != null ? {
|
|
1931
|
+
tenantId: row.tenantId
|
|
1932
|
+
} : {},
|
|
1933
|
+
...row.tenantKind != null ? {
|
|
1934
|
+
tenantKind: row.tenantKind
|
|
1935
|
+
} : {},
|
|
1237
1936
|
createdAt: row.createdAt,
|
|
1238
1937
|
updatedAt: row.updatedAt
|
|
1239
1938
|
};
|
|
@@ -1294,6 +1993,8 @@ function toScheduleRow(r) {
|
|
|
1294
1993
|
tags: r.tags ?? null,
|
|
1295
1994
|
spec: r.spec,
|
|
1296
1995
|
state: r.state,
|
|
1996
|
+
tenantId: r.tenantId ?? null,
|
|
1997
|
+
tenantKind: r.tenantKind ?? null,
|
|
1297
1998
|
createdAt: r.createdAt,
|
|
1298
1999
|
updatedAt: r.updatedAt
|
|
1299
2000
|
};
|
|
@@ -1308,6 +2009,8 @@ function toSchedulePatch(patch) {
|
|
|
1308
2009
|
if ("tags" in patch) set.tags = patch.tags ?? null;
|
|
1309
2010
|
if (patch.spec !== void 0) set.spec = patch.spec;
|
|
1310
2011
|
if (patch.state !== void 0) set.state = patch.state;
|
|
2012
|
+
if ("tenantId" in patch) set.tenantId = patch.tenantId ?? null;
|
|
2013
|
+
if ("tenantKind" in patch) set.tenantKind = patch.tenantKind ?? null;
|
|
1311
2014
|
if (patch.updatedAt !== void 0) set.updatedAt = patch.updatedAt;
|
|
1312
2015
|
return set;
|
|
1313
2016
|
}
|
|
@@ -1324,15 +2027,21 @@ function fromScheduleRow(row) {
|
|
|
1324
2027
|
} : {},
|
|
1325
2028
|
spec: row.spec ?? {},
|
|
1326
2029
|
state: row.state ?? {},
|
|
2030
|
+
...row.tenantId != null ? {
|
|
2031
|
+
tenantId: row.tenantId
|
|
2032
|
+
} : {},
|
|
2033
|
+
...row.tenantKind != null ? {
|
|
2034
|
+
tenantKind: row.tenantKind
|
|
2035
|
+
} : {},
|
|
1327
2036
|
createdAt: row.createdAt,
|
|
1328
2037
|
updatedAt: row.updatedAt
|
|
1329
2038
|
};
|
|
1330
2039
|
}
|
|
1331
2040
|
__name(fromScheduleRow, "fromScheduleRow");
|
|
1332
2041
|
function excludedSet(table, only) {
|
|
1333
|
-
return Object.fromEntries(Object.entries((0,
|
|
2042
|
+
return Object.fromEntries(Object.entries((0, import_drizzle_orm5.getTableColumns)(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
|
|
1334
2043
|
key,
|
|
1335
|
-
|
|
2044
|
+
import_drizzle_orm5.sql.raw(`excluded."${column.name}"`)
|
|
1336
2045
|
]));
|
|
1337
2046
|
}
|
|
1338
2047
|
__name(excludedSet, "excludedSet");
|
|
@@ -1349,6 +2058,7 @@ __name(excludedSet, "excludedSet");
|
|
|
1349
2058
|
durablePgSchema,
|
|
1350
2059
|
durableRunAttributes,
|
|
1351
2060
|
durableSchedules,
|
|
2061
|
+
durableSignalIds,
|
|
1352
2062
|
durableSignalWaiters,
|
|
1353
2063
|
durableStepCheckpoints,
|
|
1354
2064
|
durableWorkflowRuns,
|