@dudousxd/nestjs-durable-store-mikro-orm 0.37.0 → 0.39.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 +246 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -1
- package/dist/index.d.ts +37 -1
- package/dist/index.js +244 -42
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -43,6 +43,8 @@ __export(index_exports, {
|
|
|
43
43
|
WorkflowRunRepository: () => WorkflowRunRepository,
|
|
44
44
|
durableEntities: () => durableEntities,
|
|
45
45
|
durableManagedTables: () => durableManagedTables,
|
|
46
|
+
durableSchemaName: () => durableSchemaName,
|
|
47
|
+
durableTableSchema: () => durableTableSchema,
|
|
46
48
|
ensureMikroOrmDurableSchema: () => ensureMikroOrmDurableSchema,
|
|
47
49
|
mikroOrmDeferredAlterWarning: () => mikroOrmDeferredAlterWarning,
|
|
48
50
|
mikroOrmSchemaRepairWarning: () => mikroOrmSchemaRepairWarning
|
|
@@ -226,6 +228,15 @@ var ScheduleEntity = class {
|
|
|
226
228
|
createdAt;
|
|
227
229
|
updatedAt;
|
|
228
230
|
};
|
|
231
|
+
var SCHEMA_NAME = /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
|
232
|
+
function durableSchemaName(schema) {
|
|
233
|
+
if (schema === void 0) return void 0;
|
|
234
|
+
if (!SCHEMA_NAME.test(schema) || schema.length > 63) {
|
|
235
|
+
throw new Error(`[nestjs-durable] invalid schema ${JSON.stringify(schema)}: use letters, digits, _ or $ (not first), at most 63 characters.`);
|
|
236
|
+
}
|
|
237
|
+
return schema;
|
|
238
|
+
}
|
|
239
|
+
__name(durableSchemaName, "durableSchemaName");
|
|
229
240
|
function namespaceFilterCond(args) {
|
|
230
241
|
if (args?.namespace === void 0) return {};
|
|
231
242
|
return {
|
|
@@ -235,9 +246,14 @@ function namespaceFilterCond(args) {
|
|
|
235
246
|
__name(namespaceFilterCond, "namespaceFilterCond");
|
|
236
247
|
function durableEntities(options = {}) {
|
|
237
248
|
const col = columnNamer(options.naming ?? "snake_case");
|
|
249
|
+
const schema = durableSchemaName(options.schema);
|
|
250
|
+
const where = schema === void 0 ? {} : {
|
|
251
|
+
schema
|
|
252
|
+
};
|
|
238
253
|
const workflowRuns = new import_core2.EntitySchema({
|
|
239
254
|
class: WorkflowRunEntity,
|
|
240
255
|
tableName: "durable_workflow_runs",
|
|
256
|
+
...where,
|
|
241
257
|
// Wired per schema rather than once on the class: `durableEntities` can be called again with a
|
|
242
258
|
// different `naming`, and each call builds a distinct EntitySchema over the same class.
|
|
243
259
|
repository: /* @__PURE__ */ __name(() => WorkflowRunRepository, "repository"),
|
|
@@ -429,6 +445,7 @@ function durableEntities(options = {}) {
|
|
|
429
445
|
const stepCheckpoints = new import_core2.EntitySchema({
|
|
430
446
|
class: StepCheckpointEntity,
|
|
431
447
|
tableName: "durable_step_checkpoints",
|
|
448
|
+
...where,
|
|
432
449
|
repository: /* @__PURE__ */ __name(() => StepCheckpointRepository, "repository"),
|
|
433
450
|
// `(runId, parallelGroup, seq)` serves the run detail's per-group reads — `checkpointStats` with a
|
|
434
451
|
// group and `queryCheckpoints` paging one fan in `seq` order — without scanning the whole run.
|
|
@@ -535,6 +552,7 @@ function durableEntities(options = {}) {
|
|
|
535
552
|
const runAttributes = new import_core2.EntitySchema({
|
|
536
553
|
class: RunAttributeEntity,
|
|
537
554
|
tableName: "durable_run_attributes",
|
|
555
|
+
...where,
|
|
538
556
|
repository: /* @__PURE__ */ __name(() => RunAttributeRepository, "repository"),
|
|
539
557
|
// The search-attribute EXISTS join (see the class docstring) pushes equality/range predicates down
|
|
540
558
|
// onto `(key, numValue)` / `(key, strValue)`; mirror the Prisma adapter so those scans stay indexed.
|
|
@@ -581,6 +599,7 @@ function durableEntities(options = {}) {
|
|
|
581
599
|
const signalWaiters = new import_core2.EntitySchema({
|
|
582
600
|
class: SignalWaiterEntity,
|
|
583
601
|
tableName: "durable_signal_waiters",
|
|
602
|
+
...where,
|
|
584
603
|
repository: /* @__PURE__ */ __name(() => SignalWaiterRepository, "repository"),
|
|
585
604
|
// A run's waiters: the `ctx.all` join barrier probes them once per settled child.
|
|
586
605
|
indexes: [
|
|
@@ -618,6 +637,7 @@ function durableEntities(options = {}) {
|
|
|
618
637
|
const bufferedSignals = new import_core2.EntitySchema({
|
|
619
638
|
class: BufferedSignalEntity,
|
|
620
639
|
tableName: "durable_buffered_signals",
|
|
640
|
+
...where,
|
|
621
641
|
repository: /* @__PURE__ */ __name(() => BufferedSignalRepository, "repository"),
|
|
622
642
|
properties: {
|
|
623
643
|
id: {
|
|
@@ -641,6 +661,7 @@ function durableEntities(options = {}) {
|
|
|
641
661
|
const bufferedEvents = new import_core2.EntitySchema({
|
|
642
662
|
class: BufferedEventEntity,
|
|
643
663
|
tableName: "durable_buffered_events",
|
|
664
|
+
...where,
|
|
644
665
|
repository: /* @__PURE__ */ __name(() => BufferedEventRepository, "repository"),
|
|
645
666
|
// `(name, publishedAt)` serves both `listBufferedEvents`'s name-scoped scan and its oldest-first
|
|
646
667
|
// ordering in one index.
|
|
@@ -680,6 +701,7 @@ function durableEntities(options = {}) {
|
|
|
680
701
|
const signalIds = new import_core2.EntitySchema({
|
|
681
702
|
class: SignalIdEntity,
|
|
682
703
|
tableName: "durable_signal_ids",
|
|
704
|
+
...where,
|
|
683
705
|
repository: /* @__PURE__ */ __name(() => SignalIdRepository, "repository"),
|
|
684
706
|
// The expired-id sweep (`expires_at <= now`).
|
|
685
707
|
indexes: [
|
|
@@ -710,6 +732,7 @@ function durableEntities(options = {}) {
|
|
|
710
732
|
const schedules = new import_core2.EntitySchema({
|
|
711
733
|
class: ScheduleEntity,
|
|
712
734
|
tableName: "durable_schedules",
|
|
735
|
+
...where,
|
|
713
736
|
repository: /* @__PURE__ */ __name(() => ScheduleRepository, "repository"),
|
|
714
737
|
// The poll's "due by T" scan: `paused = false AND next_fire_at <= T ORDER BY next_fire_at`.
|
|
715
738
|
indexes: [
|
|
@@ -808,6 +831,28 @@ var import_core3 = require("@mikro-orm/core");
|
|
|
808
831
|
|
|
809
832
|
// src/schema.ts
|
|
810
833
|
var import_node_crypto = require("crypto");
|
|
834
|
+
|
|
835
|
+
// src/lifecycle-index.ts
|
|
836
|
+
var LIFECYCLE_PENDING_TAG = "durable:lifecycle:pending";
|
|
837
|
+
var LIFECYCLE_PENDING_LIKE = `%"${LIFECYCLE_PENDING_TAG}"%`;
|
|
838
|
+
var LIFECYCLE_PENDING_INDEX = "durable_runs_lifecycle_pending_idx";
|
|
839
|
+
async function ensurePgLifecyclePendingIndex(query, opts) {
|
|
840
|
+
const literal = /* @__PURE__ */ __name((value) => `'${value.replace(/'/g, "''")}'`, "literal");
|
|
841
|
+
const scope = opts.schema === void 0 ? "current_schema()" : literal(opts.schema);
|
|
842
|
+
const [state] = await query(`SELECT i.indisvalid AS valid, EXISTS (SELECT 1 FROM pg_stat_progress_create_index p WHERE p.index_relid = c.oid) AS building
|
|
843
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_index i ON i.indexrelid = c.oid
|
|
844
|
+
WHERE c.relname = ${literal(LIFECYCLE_PENDING_INDEX)} AND n.nspname = ${scope}`) ?? [];
|
|
845
|
+
if (state?.valid || state?.building) return false;
|
|
846
|
+
const [tx] = await query("SELECT now() = statement_timestamp() AS autocommit") ?? [];
|
|
847
|
+
const concurrently = tx?.autocommit === false ? "" : "CONCURRENTLY ";
|
|
848
|
+
const qualified = opts.schema === void 0 ? `"${LIFECYCLE_PENDING_INDEX}"` : `"${opts.schema.replace(/"/g, '""')}"."${LIFECYCLE_PENDING_INDEX}"`;
|
|
849
|
+
if (state) await query(`DROP INDEX ${concurrently}IF EXISTS ${qualified}`);
|
|
850
|
+
await query(`CREATE INDEX ${concurrently}IF NOT EXISTS "${LIFECYCLE_PENDING_INDEX}" ON ${opts.table} (${opts.createdAt}) WHERE ${opts.predicate}`);
|
|
851
|
+
return true;
|
|
852
|
+
}
|
|
853
|
+
__name(ensurePgLifecyclePendingIndex, "ensurePgLifecyclePendingIndex");
|
|
854
|
+
|
|
855
|
+
// src/schema.ts
|
|
811
856
|
var DURABLE_TABLE_NAMES = /* @__PURE__ */ new Set([
|
|
812
857
|
"durable_workflow_runs",
|
|
813
858
|
"durable_step_checkpoints",
|
|
@@ -827,7 +872,7 @@ __name(durableManagedTables, "durableManagedTables");
|
|
|
827
872
|
var MARKER_TABLE = "durable_schema_meta";
|
|
828
873
|
var MARKER_ROW_ID = "durable";
|
|
829
874
|
var SCHEMA_LOCK_NAME = "durable_schema";
|
|
830
|
-
var SCHEMA_REVISION =
|
|
875
|
+
var SCHEMA_REVISION = 2;
|
|
831
876
|
var DEFAULT_BLOCKING_ALTER_ROW_LIMIT = 1e5;
|
|
832
877
|
function deferredAlterWarning(store, deferred, limit) {
|
|
833
878
|
return [
|
|
@@ -854,6 +899,7 @@ function mikroOrmSchemaRepairWarning(repairs) {
|
|
|
854
899
|
].join(" ");
|
|
855
900
|
}
|
|
856
901
|
__name(mikroOrmSchemaRepairWarning, "mikroOrmSchemaRepairWarning");
|
|
902
|
+
var ALTER_TABLE_PREFIX = /^alter\s+table\s+(?:[`"']?[\w$]+[`"']?\.)?[`"']?[\w$]+[`"']?\s*/i;
|
|
857
903
|
function unquote(identifier) {
|
|
858
904
|
return identifier.replace(/^[`"']|[`"']$/g, "");
|
|
859
905
|
}
|
|
@@ -870,7 +916,7 @@ function describeDurableStatement(statement) {
|
|
|
870
916
|
if (/^alter\s+table\b/i.test(statement)) {
|
|
871
917
|
const out = [];
|
|
872
918
|
let other = false;
|
|
873
|
-
for (const action of splitTopLevel(statement.replace(
|
|
919
|
+
for (const action of splitTopLevel(statement.replace(ALTER_TABLE_PREFIX, ""))) {
|
|
874
920
|
const index = action.match(/^add\s+(?:unique\s+|fulltext\s+)?(?:index|key)\s+([`"']?[\w$]+[`"']?)/i);
|
|
875
921
|
const constraint = action.match(/^add\s+constraint\s+([`"']?[\w$]+[`"']?)/i);
|
|
876
922
|
const column = action.match(/^add\s+(?:column\s+)?([`"']?[\w$]+[`"']?)/i);
|
|
@@ -890,6 +936,11 @@ function describeDurableStatement(statement) {
|
|
|
890
936
|
];
|
|
891
937
|
}
|
|
892
938
|
__name(describeDurableStatement, "describeDurableStatement");
|
|
939
|
+
function inDurableSchema(statement, where) {
|
|
940
|
+
const match = statement.match(/^(?:create\s+(?:unique\s+)?index\s+.+?\s+on\s+|(?:create|alter)\s+table\s+(?:if\s+not\s+exists\s+)?)(?:[`"']?([\w$]+)[`"']?\.)?[`"']?[\w$]+[`"']?/i);
|
|
941
|
+
return match?.[1] === where.schema;
|
|
942
|
+
}
|
|
943
|
+
__name(inDurableSchema, "inDurableSchema");
|
|
893
944
|
function splitTopLevel(text) {
|
|
894
945
|
const parts = [];
|
|
895
946
|
let depth = 0;
|
|
@@ -920,7 +971,7 @@ function compareStrings(a, b) {
|
|
|
920
971
|
}
|
|
921
972
|
__name(compareStrings, "compareStrings");
|
|
922
973
|
function targetTable(statement) {
|
|
923
|
-
const match = statement.match(/^(?:create\s+(?:unique\s+)?index\s+.+?\s+on\s+|(?:create|alter)\s+table\s+(?:if\s+not\s+exists\s+)?)[`"']?([\w$]+)[`"']?/i);
|
|
974
|
+
const match = statement.match(/^(?:create\s+(?:unique\s+)?index\s+.+?\s+on\s+|(?:create|alter)\s+table\s+(?:if\s+not\s+exists\s+)?)(?:[`"']?[\w$]+[`"']?\.)?[`"']?([\w$]+)[`"']?/i);
|
|
924
975
|
return match?.[1]?.toLowerCase();
|
|
925
976
|
}
|
|
926
977
|
__name(targetTable, "targetTable");
|
|
@@ -989,19 +1040,64 @@ function canonicalize(value) {
|
|
|
989
1040
|
return JSON.stringify(value);
|
|
990
1041
|
}
|
|
991
1042
|
__name(canonicalize, "canonicalize");
|
|
992
|
-
function
|
|
993
|
-
|
|
1043
|
+
function durableTableSchema(orm, configured) {
|
|
1044
|
+
const wanted = durableSchemaName(configured);
|
|
1045
|
+
const runs = [
|
|
1046
|
+
...orm.getMetadata().getAll().values()
|
|
1047
|
+
].find((meta) => meta.class === WorkflowRunEntity || meta.tableName === "durable_workflow_runs");
|
|
1048
|
+
const metaSchema2 = runs?.schema;
|
|
1049
|
+
const registered = metaSchema2 && metaSchema2 !== "*" ? metaSchema2 : void 0;
|
|
1050
|
+
const schema = wanted ?? registered;
|
|
1051
|
+
if (schema !== void 0 && detectSqlDialect(orm) === "sqlite") {
|
|
1052
|
+
throw new Error(`[nestjs-durable] store-mikro-orm: the schema option (${JSON.stringify(schema)}) is not supported on SQLite, which has no schemas. Remove it from the store options and from durableEntities().`);
|
|
1053
|
+
}
|
|
1054
|
+
if (wanted !== void 0 && wanted !== registered) {
|
|
1055
|
+
throw new Error(`[nestjs-durable] store-mikro-orm: the store's schema is ${JSON.stringify(wanted)}, but the durable entities were registered ${registered === void 0 ? "without one" : `with ${JSON.stringify(registered)}`}. Register durableEntities({ schema: ${JSON.stringify(wanted)} }) in your MikroORM config.`);
|
|
1056
|
+
}
|
|
1057
|
+
return schema;
|
|
1058
|
+
}
|
|
1059
|
+
__name(durableTableSchema, "durableTableSchema");
|
|
1060
|
+
function durableLocation(orm, configured) {
|
|
1061
|
+
const schema = durableTableSchema(orm, configured);
|
|
1062
|
+
const platform = orm.em.getPlatform();
|
|
1063
|
+
const quote = /* @__PURE__ */ __name((id) => platform.quoteIdentifier(id), "quote");
|
|
1064
|
+
return {
|
|
1065
|
+
schema,
|
|
1066
|
+
quote,
|
|
1067
|
+
marker: schema === void 0 ? MARKER_TABLE : `${quote(schema)}.${quote(MARKER_TABLE)}`
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
__name(durableLocation, "durableLocation");
|
|
1071
|
+
async function createSchemaIfMissing(orm, dialect, where) {
|
|
1072
|
+
if (where.schema === void 0) return;
|
|
1073
|
+
const connection = orm.em.getConnection();
|
|
1074
|
+
const rows = await connection.execute(dialect === "mysql" ? "select 1 as found from information_schema.schemata where schema_name = ?" : "select 1 as found from pg_namespace where nspname = ?", [
|
|
1075
|
+
where.schema
|
|
1076
|
+
]);
|
|
1077
|
+
if (rows.length > 0) return;
|
|
1078
|
+
const kind = dialect === "mysql" ? "database" : "schema";
|
|
1079
|
+
try {
|
|
1080
|
+
await connection.execute(`create ${kind} if not exists ${where.quote(where.schema)}`);
|
|
1081
|
+
} catch (err) {
|
|
1082
|
+
throw new Error(`[nestjs-durable] store-mikro-orm could not create the ${kind} "${where.schema}" for the durable tables. Create it ahead of time, or grant this role ${dialect === "mysql" ? "CREATE" : "CREATE on the database (GRANT CREATE ON DATABASE <db> TO <role>)"}. Cause: ${err.message}`, {
|
|
1083
|
+
cause: err
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
__name(createSchemaIfMissing, "createSchemaIfMissing");
|
|
1088
|
+
function createMarkerTableSql(marker) {
|
|
1089
|
+
return `create table if not exists ${marker} (id varchar(32) not null primary key, fingerprint varchar(64) not null, applied_at bigint not null)`;
|
|
994
1090
|
}
|
|
995
1091
|
__name(createMarkerTableSql, "createMarkerTableSql");
|
|
996
|
-
async function readStoredFingerprint(orm) {
|
|
997
|
-
const rows = await orm.em.getConnection().execute(`select fingerprint from ${
|
|
1092
|
+
async function readStoredFingerprint(orm, where) {
|
|
1093
|
+
const rows = await orm.em.getConnection().execute(`select fingerprint from ${where.marker} where id = ?`, [
|
|
998
1094
|
MARKER_ROW_ID
|
|
999
1095
|
]);
|
|
1000
1096
|
return rows[0]?.fingerprint ?? void 0;
|
|
1001
1097
|
}
|
|
1002
1098
|
__name(readStoredFingerprint, "readStoredFingerprint");
|
|
1003
|
-
async function writeFingerprint(orm, dialect, fingerprint) {
|
|
1004
|
-
const upsert = dialect === "mysql" ? `insert into ${
|
|
1099
|
+
async function writeFingerprint(orm, dialect, fingerprint, where) {
|
|
1100
|
+
const upsert = dialect === "mysql" ? `insert into ${where.marker} (id, fingerprint, applied_at) values (?, ?, ?) on duplicate key update fingerprint = values(fingerprint), applied_at = values(applied_at)` : `insert into ${where.marker} (id, fingerprint, applied_at) values (?, ?, ?) on conflict (id) do update set fingerprint = excluded.fingerprint, applied_at = excluded.applied_at`;
|
|
1005
1101
|
await orm.em.getConnection().execute(upsert, [
|
|
1006
1102
|
MARKER_ROW_ID,
|
|
1007
1103
|
fingerprint,
|
|
@@ -1009,12 +1105,17 @@ async function writeFingerprint(orm, dialect, fingerprint) {
|
|
|
1009
1105
|
]);
|
|
1010
1106
|
}
|
|
1011
1107
|
__name(writeFingerprint, "writeFingerprint");
|
|
1012
|
-
|
|
1108
|
+
function schemaLockName(where) {
|
|
1109
|
+
return where.schema === void 0 ? SCHEMA_LOCK_NAME : `${SCHEMA_LOCK_NAME}:${where.schema}`;
|
|
1110
|
+
}
|
|
1111
|
+
__name(schemaLockName, "schemaLockName");
|
|
1112
|
+
async function acquireSchemaLock(orm, dialect, where) {
|
|
1113
|
+
const name = schemaLockName(where);
|
|
1013
1114
|
try {
|
|
1014
1115
|
if (dialect === "mysql") {
|
|
1015
|
-
await orm.em.getConnection().execute(`select get_lock('${
|
|
1116
|
+
await orm.em.getConnection().execute(`select get_lock('${name}', 10)`);
|
|
1016
1117
|
} else if (dialect === "postgres") {
|
|
1017
|
-
await orm.em.getConnection().execute(`select pg_advisory_lock(hashtext('${
|
|
1118
|
+
await orm.em.getConnection().execute(`select pg_advisory_lock(hashtext('${name}'))`);
|
|
1018
1119
|
}
|
|
1019
1120
|
} catch (err) {
|
|
1020
1121
|
const message = err instanceof Error ? err.message : String(err);
|
|
@@ -1022,12 +1123,13 @@ async function acquireSchemaLock(orm, dialect) {
|
|
|
1022
1123
|
}
|
|
1023
1124
|
}
|
|
1024
1125
|
__name(acquireSchemaLock, "acquireSchemaLock");
|
|
1025
|
-
async function releaseSchemaLock(orm, dialect) {
|
|
1126
|
+
async function releaseSchemaLock(orm, dialect, where) {
|
|
1127
|
+
const name = schemaLockName(where);
|
|
1026
1128
|
try {
|
|
1027
1129
|
if (dialect === "mysql") {
|
|
1028
|
-
await orm.em.getConnection().execute(`select release_lock('${
|
|
1130
|
+
await orm.em.getConnection().execute(`select release_lock('${name}')`);
|
|
1029
1131
|
} else if (dialect === "postgres") {
|
|
1030
|
-
await orm.em.getConnection().execute(`select pg_advisory_unlock(hashtext('${
|
|
1132
|
+
await orm.em.getConnection().execute(`select pg_advisory_unlock(hashtext('${name}'))`);
|
|
1031
1133
|
}
|
|
1032
1134
|
} catch {
|
|
1033
1135
|
}
|
|
@@ -1036,35 +1138,80 @@ __name(releaseSchemaLock, "releaseSchemaLock");
|
|
|
1036
1138
|
async function ensureMikroOrmDurableSchema(orm, options = {}) {
|
|
1037
1139
|
const logger = options.logger ?? console;
|
|
1038
1140
|
const dialect = detectSqlDialect(orm);
|
|
1039
|
-
|
|
1141
|
+
const where = durableLocation(orm, options.schema);
|
|
1142
|
+
await createSchemaIfMissing(orm, dialect, where);
|
|
1143
|
+
await orm.em.getConnection().execute(createMarkerTableSql(where.marker));
|
|
1040
1144
|
const expected = computeExpectedFingerprint(orm, DURABLE_TABLE_NAMES);
|
|
1041
|
-
const stored = await readStoredFingerprint(orm);
|
|
1145
|
+
const stored = await readStoredFingerprint(orm, where);
|
|
1042
1146
|
if (stored === expected) {
|
|
1043
1147
|
return;
|
|
1044
1148
|
}
|
|
1045
|
-
await acquireSchemaLock(orm, dialect);
|
|
1149
|
+
await acquireSchemaLock(orm, dialect, where);
|
|
1046
1150
|
try {
|
|
1047
|
-
if (await readStoredFingerprint(orm) === expected) {
|
|
1151
|
+
if (await readStoredFingerprint(orm, where) === expected) {
|
|
1048
1152
|
return;
|
|
1049
1153
|
}
|
|
1050
1154
|
const limit = options.blockingAlterRowLimit ?? DEFAULT_BLOCKING_ALTER_ROW_LIMIT;
|
|
1051
|
-
const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit);
|
|
1052
|
-
|
|
1155
|
+
const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit, where);
|
|
1156
|
+
const indexed = await healLifecycleIndex(orm, dialect, where, logger);
|
|
1157
|
+
if (indexed.built && !freshInstall) repairs.push(...indexed.repairs);
|
|
1158
|
+
if (deferred.length === 0 && indexed.ok) await writeFingerprint(orm, dialect, expected, where);
|
|
1053
1159
|
if (repairs.length > 0 && !freshInstall) logger.warn(mikroOrmSchemaRepairWarning(repairs));
|
|
1054
1160
|
if (deferred.length > 0) logger.warn(mikroOrmDeferredAlterWarning(deferred, limit));
|
|
1055
1161
|
} finally {
|
|
1056
|
-
await releaseSchemaLock(orm, dialect);
|
|
1162
|
+
await releaseSchemaLock(orm, dialect, where);
|
|
1057
1163
|
}
|
|
1058
1164
|
}
|
|
1059
1165
|
__name(ensureMikroOrmDurableSchema, "ensureMikroOrmDurableSchema");
|
|
1060
|
-
async function
|
|
1166
|
+
async function healLifecycleIndex(orm, dialect, where, logger) {
|
|
1167
|
+
if (dialect !== "postgres") return {
|
|
1168
|
+
ok: true,
|
|
1169
|
+
built: false,
|
|
1170
|
+
repairs: []
|
|
1171
|
+
};
|
|
1172
|
+
const meta = [
|
|
1173
|
+
...orm.getMetadata().getAll().values()
|
|
1174
|
+
].find((m) => m.class === WorkflowRunEntity || m.tableName === "durable_workflow_runs");
|
|
1175
|
+
if (!meta) return {
|
|
1176
|
+
ok: true,
|
|
1177
|
+
built: false,
|
|
1178
|
+
repairs: []
|
|
1179
|
+
};
|
|
1180
|
+
const column = /* @__PURE__ */ __name((prop) => where.quote(meta.properties?.[prop]?.fieldNames?.[0] ?? prop), "column");
|
|
1181
|
+
const table = where.schema === void 0 ? where.quote(meta.tableName) : `${where.quote(where.schema)}.${where.quote(meta.tableName)}`;
|
|
1182
|
+
try {
|
|
1183
|
+
const built = await ensurePgLifecyclePendingIndex((sql) => orm.em.getConnection().execute(sql), {
|
|
1184
|
+
schema: where.schema,
|
|
1185
|
+
table,
|
|
1186
|
+
createdAt: column("createdAt"),
|
|
1187
|
+
predicate: `${column("tags")}::text LIKE '${LIFECYCLE_PENDING_LIKE}'`
|
|
1188
|
+
});
|
|
1189
|
+
return {
|
|
1190
|
+
ok: true,
|
|
1191
|
+
built,
|
|
1192
|
+
repairs: built ? [
|
|
1193
|
+
`created index ${LIFECYCLE_PENDING_INDEX} on ${meta.tableName}`
|
|
1194
|
+
] : []
|
|
1195
|
+
};
|
|
1196
|
+
} catch (error) {
|
|
1197
|
+
logger.warn(`[nestjs-durable] store-mikro-orm could not create the partial index ${LIFECYCLE_PENDING_INDEX} (the owed-lifecycle listing scans the runs table until it exists); retrying on the next boot: ${error.message}`);
|
|
1198
|
+
return {
|
|
1199
|
+
ok: false,
|
|
1200
|
+
built: false,
|
|
1201
|
+
repairs: []
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
__name(healLifecycleIndex, "healLifecycleIndex");
|
|
1206
|
+
async function healDurableSchema(orm, dialect, blockingAlterRowLimit, where) {
|
|
1061
1207
|
const sql = await orm.schema.getUpdateSchemaSQL({
|
|
1062
1208
|
safe: true
|
|
1063
1209
|
});
|
|
1064
|
-
const
|
|
1210
|
+
const diff = sql.split(";").map((statement) => statement.trim()).filter((statement) => {
|
|
1065
1211
|
const table = targetTable(statement);
|
|
1066
|
-
return table !== void 0 && DURABLE_TABLE_NAMES.has(table);
|
|
1067
|
-
})
|
|
1212
|
+
return table !== void 0 && DURABLE_TABLE_NAMES.has(table) && inDurableSchema(statement, where);
|
|
1213
|
+
});
|
|
1214
|
+
const ours = mergeColumnTypeChanges(dialect === "mysql" && where.schema !== void 0 ? await narrowMysqlSchemaDiff(orm, where.schema, diff) : diff);
|
|
1068
1215
|
const connection = orm.em.getConnection();
|
|
1069
1216
|
const created = new Set(ours.filter((statement) => /^create\s+table\b/i.test(statement)).map((statement) => targetTable(statement)));
|
|
1070
1217
|
const repairs = [];
|
|
@@ -1072,7 +1219,7 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
|
|
|
1072
1219
|
for (const statement of ours) {
|
|
1073
1220
|
const table = targetTable(statement);
|
|
1074
1221
|
if (dialect === "mysql" && !created.has(table) && isColumnTypeChange(statement)) {
|
|
1075
|
-
const rows = await estimatedRowCount(orm, table);
|
|
1222
|
+
const rows = await estimatedRowCount(orm, table, where);
|
|
1076
1223
|
if (rows > blockingAlterRowLimit) {
|
|
1077
1224
|
deferred.push({
|
|
1078
1225
|
table,
|
|
@@ -1094,7 +1241,7 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
|
|
|
1094
1241
|
console.warn(`[nestjs-durable-store-mikro-orm] skipped a non-structural durable-schema statement that failed; the column is left as-is (functional) \u2014 repair the data out of band to converge its type. Statement: ${statement} \u2014 ${message}`);
|
|
1095
1242
|
}
|
|
1096
1243
|
}
|
|
1097
|
-
const converted = await alignDurableCollation(orm);
|
|
1244
|
+
const converted = await alignDurableCollation(orm, where);
|
|
1098
1245
|
for (const { table, from, to } of converted) {
|
|
1099
1246
|
if (!created.has(table)) repairs.push(`converted ${table} collation from ${from} to ${to}`);
|
|
1100
1247
|
}
|
|
@@ -1108,6 +1255,40 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
|
|
|
1108
1255
|
};
|
|
1109
1256
|
}
|
|
1110
1257
|
__name(healDurableSchema, "healDurableSchema");
|
|
1258
|
+
async function narrowMysqlSchemaDiff(orm, schema, diff) {
|
|
1259
|
+
const connection = orm.em.getConnection();
|
|
1260
|
+
const columns = await connection.execute("select table_name as table_name, column_name as column_name from information_schema.columns where table_schema = ?", [
|
|
1261
|
+
schema
|
|
1262
|
+
]);
|
|
1263
|
+
const indexes = await connection.execute("select distinct table_name as table_name, index_name as index_name from information_schema.statistics where table_schema = ?", [
|
|
1264
|
+
schema
|
|
1265
|
+
]);
|
|
1266
|
+
const tables = new Set(columns.map((c) => c.table_name.toLowerCase()));
|
|
1267
|
+
const hasColumn = new Set(columns.map((c) => `${c.table_name}.${c.column_name}`.toLowerCase()));
|
|
1268
|
+
const hasIndex = new Set(indexes.map((i) => `${i.table_name}.${i.index_name}`.toLowerCase()));
|
|
1269
|
+
const out = [];
|
|
1270
|
+
for (const statement of diff) {
|
|
1271
|
+
const table = targetTable(statement);
|
|
1272
|
+
if (!tables.has(table)) {
|
|
1273
|
+
out.push(statement);
|
|
1274
|
+
continue;
|
|
1275
|
+
}
|
|
1276
|
+
if (/^create\s+table\b/i.test(statement)) {
|
|
1277
|
+
const open = statement.indexOf("(");
|
|
1278
|
+
const close = statement.lastIndexOf(")");
|
|
1279
|
+
const prefix = statement.slice(0, open).replace(/^create\s+table\s+/i, "").trim();
|
|
1280
|
+
for (const definition of splitTopLevel(statement.slice(open + 1, close))) {
|
|
1281
|
+
const column = definition.match(/^[`"]([^`"]+)[`"]\s/);
|
|
1282
|
+
if (column?.[1] && !hasColumn.has(`${table}.${column[1]}`.toLowerCase())) out.push(`alter table ${prefix} add column ${definition}`);
|
|
1283
|
+
}
|
|
1284
|
+
continue;
|
|
1285
|
+
}
|
|
1286
|
+
const index = statement.match(/\badd\s+(?:unique\s+|fulltext\s+)?(?:index|key)\s+[`"]([^`"]+)[`"]/i);
|
|
1287
|
+
if (index?.[1] && !hasIndex.has(`${table}.${index[1]}`.toLowerCase())) out.push(statement);
|
|
1288
|
+
}
|
|
1289
|
+
return out;
|
|
1290
|
+
}
|
|
1291
|
+
__name(narrowMysqlSchemaDiff, "narrowMysqlSchemaDiff");
|
|
1111
1292
|
function mergeColumnTypeChanges(statements) {
|
|
1112
1293
|
const merged = [];
|
|
1113
1294
|
const at = /* @__PURE__ */ new Map();
|
|
@@ -1117,7 +1298,7 @@ function mergeColumnTypeChanges(statements) {
|
|
|
1117
1298
|
merged.push(statement);
|
|
1118
1299
|
continue;
|
|
1119
1300
|
}
|
|
1120
|
-
const actions = statement.replace(
|
|
1301
|
+
const actions = statement.replace(ALTER_TABLE_PREFIX, "");
|
|
1121
1302
|
const index = at.get(table);
|
|
1122
1303
|
if (index === void 0) {
|
|
1123
1304
|
at.set(table, merged.length);
|
|
@@ -1131,12 +1312,15 @@ function mergeColumnTypeChanges(statements) {
|
|
|
1131
1312
|
__name(mergeColumnTypeChanges, "mergeColumnTypeChanges");
|
|
1132
1313
|
function isColumnTypeChange(statement) {
|
|
1133
1314
|
if (!/^alter\s+table\b/i.test(statement)) return false;
|
|
1134
|
-
const actions = splitTopLevel(statement.replace(
|
|
1315
|
+
const actions = splitTopLevel(statement.replace(ALTER_TABLE_PREFIX, ""));
|
|
1135
1316
|
return actions.length > 0 && actions.every((action) => /^modify\b/i.test(action));
|
|
1136
1317
|
}
|
|
1137
1318
|
__name(isColumnTypeChange, "isColumnTypeChange");
|
|
1138
|
-
async function estimatedRowCount(orm, table) {
|
|
1139
|
-
const rows = await orm.em.getConnection().execute(
|
|
1319
|
+
async function estimatedRowCount(orm, table, where) {
|
|
1320
|
+
const rows = await orm.em.getConnection().execute(`select table_rows as table_rows from information_schema.tables where table_schema = ${where.schema === void 0 ? "database()" : "?"} and table_name = ? limit 1`, where.schema === void 0 ? [
|
|
1321
|
+
table
|
|
1322
|
+
] : [
|
|
1323
|
+
where.schema,
|
|
1140
1324
|
table
|
|
1141
1325
|
]);
|
|
1142
1326
|
return Number(rows[0]?.table_rows ?? 0);
|
|
@@ -1146,7 +1330,7 @@ function isSafeSqlIdentifier(value) {
|
|
|
1146
1330
|
return /^[A-Za-z0-9_]+$/.test(value);
|
|
1147
1331
|
}
|
|
1148
1332
|
__name(isSafeSqlIdentifier, "isSafeSqlIdentifier");
|
|
1149
|
-
async function alignDurableCollation(orm) {
|
|
1333
|
+
async function alignDurableCollation(orm, where) {
|
|
1150
1334
|
const converted = [];
|
|
1151
1335
|
const platform = String(orm.em.getPlatform().constructor.name).toLowerCase();
|
|
1152
1336
|
if (!platform.includes("mysql") && !platform.includes("maria")) {
|
|
@@ -1163,14 +1347,17 @@ async function alignDurableCollation(orm) {
|
|
|
1163
1347
|
const connection = orm.em.getConnection();
|
|
1164
1348
|
for (const table of DURABLE_TABLE_NAMES) {
|
|
1165
1349
|
try {
|
|
1166
|
-
const rows = await connection.execute(
|
|
1350
|
+
const rows = await connection.execute(`select table_collation as collation from information_schema.tables where table_schema = ${where.schema === void 0 ? "database()" : "?"} and table_name = ? limit 1`, where.schema === void 0 ? [
|
|
1351
|
+
table
|
|
1352
|
+
] : [
|
|
1353
|
+
where.schema,
|
|
1167
1354
|
table
|
|
1168
1355
|
]);
|
|
1169
1356
|
const current = rows[0]?.collation;
|
|
1170
1357
|
if (current == null || current === collate) {
|
|
1171
1358
|
continue;
|
|
1172
1359
|
}
|
|
1173
|
-
await connection.execute(`alter table
|
|
1360
|
+
await connection.execute(`alter table ${where.schema === void 0 ? `\`${table}\`` : `${where.quote(where.schema)}.${where.quote(table)}`} convert to character set ${charset} collate ${collate}`);
|
|
1174
1361
|
converted.push({
|
|
1175
1362
|
table,
|
|
1176
1363
|
from: current,
|
|
@@ -1186,6 +1373,14 @@ async function alignDurableCollation(orm) {
|
|
|
1186
1373
|
__name(alignDurableCollation, "alignDurableCollation");
|
|
1187
1374
|
|
|
1188
1375
|
// src/mikro-orm-state-store.ts
|
|
1376
|
+
function metaSchema(meta) {
|
|
1377
|
+
return meta.schema && meta.schema !== "*" ? meta.schema : void 0;
|
|
1378
|
+
}
|
|
1379
|
+
__name(metaSchema, "metaSchema");
|
|
1380
|
+
function tableRef(quote, table, schema) {
|
|
1381
|
+
return schema === void 0 ? quote(table) : `${quote(schema)}.${quote(table)}`;
|
|
1382
|
+
}
|
|
1383
|
+
__name(tableRef, "tableRef");
|
|
1189
1384
|
var RETENTION_EXCLUDED = [
|
|
1190
1385
|
import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
|
|
1191
1386
|
];
|
|
@@ -1212,6 +1407,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
1212
1407
|
} catch {
|
|
1213
1408
|
return;
|
|
1214
1409
|
}
|
|
1410
|
+
durableTableSchema(orm, opts.schema);
|
|
1215
1411
|
const dialect = platform.includes("postgre") ? "postgres" : platform.includes("mysql") ? "mysql" : void 0;
|
|
1216
1412
|
if (!dialect) return;
|
|
1217
1413
|
const fast = new MikroOrmRoundTrips(() => this.orm.em.fork(), dialect, this.scopeNamespace, this);
|
|
@@ -1250,7 +1446,8 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
1250
1446
|
async ensureSchema() {
|
|
1251
1447
|
await ensureMikroOrmDurableSchema(this.orm, {
|
|
1252
1448
|
logger: this.opts.schemaLogger,
|
|
1253
|
-
blockingAlterRowLimit: this.opts.schemaBlockingAlterRowLimit
|
|
1449
|
+
blockingAlterRowLimit: this.opts.schemaBlockingAlterRowLimit,
|
|
1450
|
+
schema: this.opts.schema
|
|
1254
1451
|
});
|
|
1255
1452
|
}
|
|
1256
1453
|
async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
|
|
@@ -1986,7 +2183,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
1986
2183
|
const cols = this.attributeColumns(em);
|
|
1987
2184
|
const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
|
|
1988
2185
|
const a = quote("a");
|
|
1989
|
-
const join = `${quote
|
|
2186
|
+
const join = `${tableRef(quote, cols.table, cols.schema)} as ${a} join (${runs}) as ${quote("r")} on ${a}.${quote(cols.runId)} = ${quote("r")}.${quote(idCol)}`;
|
|
1990
2187
|
if (axis.field === "attributeKey") {
|
|
1991
2188
|
return {
|
|
1992
2189
|
source: join,
|
|
@@ -2043,7 +2240,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
2043
2240
|
return `${quote("a")}.${quote(col)} ${comparator} ${literal}`;
|
|
2044
2241
|
});
|
|
2045
2242
|
if (comparisons.length === 0) return "1 = 0";
|
|
2046
|
-
const a = quote
|
|
2243
|
+
const a = tableRef(quote, cols.table, cols.schema);
|
|
2047
2244
|
const sub = quote("a");
|
|
2048
2245
|
const outerId = `${quote(alias)}.${quote(cols.runPk)}`;
|
|
2049
2246
|
const match = comparisons.length === 1 ? comparisons[0] : `(${comparisons.join(" OR ")})`;
|
|
@@ -2059,6 +2256,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
2059
2256
|
const runPk = runMeta.properties.id?.fieldNames?.[0] ?? "id";
|
|
2060
2257
|
return {
|
|
2061
2258
|
table: meta.tableName,
|
|
2259
|
+
schema: metaSchema(meta),
|
|
2062
2260
|
runId: field("runId"),
|
|
2063
2261
|
runPk,
|
|
2064
2262
|
key: field("key"),
|
|
@@ -2270,7 +2468,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
|
|
|
2270
2468
|
where += ` and ${c("parallelGroup")} = ?`;
|
|
2271
2469
|
params.push(opts.group);
|
|
2272
2470
|
}
|
|
2273
|
-
const sql = `select ${c("parallelGroup")} as ${quote("grp")}, ${c("status")} as ${quote("status")}, ${nameKind} as ${quote("name_kind")}, count(*) as ${quote("n")}, min(${c("seq")}) as ${quote("min_seq")}, max(${c("seq")}) as ${quote("max_seq")}, min(${c("name")}) as ${quote("min_name")}, max(${c("name")}) as ${quote("max_name")}, min(coalesce(${c("enqueuedAt")}, ${c("startedAt")})) as ${quote("min_enqueued_at")}, min(${c("startedAt")}) as ${quote("min_started_at")}, max(${c("startedAt")}) as ${quote("max_started_at")}, max(${c("finishedAt")}) as ${quote("max_finished_at")}, sum(${c("attempts")}) as ${quote("sum_attempts")} from ${quote
|
|
2471
|
+
const sql = `select ${c("parallelGroup")} as ${quote("grp")}, ${c("status")} as ${quote("status")}, ${nameKind} as ${quote("name_kind")}, count(*) as ${quote("n")}, min(${c("seq")}) as ${quote("min_seq")}, max(${c("seq")}) as ${quote("max_seq")}, min(${c("name")}) as ${quote("min_name")}, max(${c("name")}) as ${quote("max_name")}, min(coalesce(${c("enqueuedAt")}, ${c("startedAt")})) as ${quote("min_enqueued_at")}, min(${c("startedAt")}) as ${quote("min_started_at")}, max(${c("startedAt")}) as ${quote("max_started_at")}, max(${c("finishedAt")}) as ${quote("max_finished_at")}, sum(${c("attempts")}) as ${quote("sum_attempts")} from ${tableRef(quote, meta.tableName, metaSchema(meta))} where ${where} group by ${c("parallelGroup")}, ${c("status")}, ${nameKind}`;
|
|
2274
2472
|
const rows = await em.getConnection().execute(sql, params);
|
|
2275
2473
|
const date = aggregateDateReader(em);
|
|
2276
2474
|
return rows.map((row) => ({
|
|
@@ -2954,7 +3152,8 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
|
|
|
2954
3152
|
return this.q(em, this.field(em, entity, property));
|
|
2955
3153
|
}
|
|
2956
3154
|
table(em, entity) {
|
|
2957
|
-
|
|
3155
|
+
const meta = this.meta(em, entity);
|
|
3156
|
+
return tableRef((id) => this.q(em, id), meta.tableName, metaSchema(meta));
|
|
2958
3157
|
}
|
|
2959
3158
|
/**
|
|
2960
3159
|
* `value` as a raw-SQL parameter for `property` (its custom type's conversion, if any).
|
|
@@ -3017,8 +3216,11 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
|
|
|
3017
3216
|
}
|
|
3018
3217
|
async cpType(em, property) {
|
|
3019
3218
|
const field = this.field(em, StepCheckpointEntity, property);
|
|
3020
|
-
this.cpTypes ??= this.run(em, "SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = ? AND table_schema = current_schema()", [
|
|
3021
|
-
this.meta(em, StepCheckpointEntity).tableName
|
|
3219
|
+
this.cpTypes ??= this.run(em, metaSchema(this.meta(em, StepCheckpointEntity)) === void 0 ? "SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = ? AND table_schema = current_schema()" : "SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = ? AND table_schema = ?", [
|
|
3220
|
+
this.meta(em, StepCheckpointEntity).tableName,
|
|
3221
|
+
...metaSchema(this.meta(em, StepCheckpointEntity)) === void 0 ? [] : [
|
|
3222
|
+
metaSchema(this.meta(em, StepCheckpointEntity))
|
|
3223
|
+
]
|
|
3022
3224
|
]).then((out) => new Map(this.rows(out).map((r) => [
|
|
3023
3225
|
String(r.name),
|
|
3024
3226
|
String(r.type)
|
|
@@ -3184,6 +3386,8 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
|
|
|
3184
3386
|
WorkflowRunRepository,
|
|
3185
3387
|
durableEntities,
|
|
3186
3388
|
durableManagedTables,
|
|
3389
|
+
durableSchemaName,
|
|
3390
|
+
durableTableSchema,
|
|
3187
3391
|
ensureMikroOrmDurableSchema,
|
|
3188
3392
|
mikroOrmDeferredAlterWarning,
|
|
3189
3393
|
mikroOrmSchemaRepairWarning
|