@dudousxd/nestjs-durable-store-mikro-orm 0.38.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.js CHANGED
@@ -781,6 +781,28 @@ import { LockMode, UniqueConstraintViolationException, raw } from "@mikro-orm/co
781
781
 
782
782
  // src/schema.ts
783
783
  import { createHash } from "node:crypto";
784
+
785
+ // src/lifecycle-index.ts
786
+ var LIFECYCLE_PENDING_TAG = "durable:lifecycle:pending";
787
+ var LIFECYCLE_PENDING_LIKE = `%"${LIFECYCLE_PENDING_TAG}"%`;
788
+ var LIFECYCLE_PENDING_INDEX = "durable_runs_lifecycle_pending_idx";
789
+ async function ensurePgLifecyclePendingIndex(query, opts) {
790
+ const literal = /* @__PURE__ */ __name((value) => `'${value.replace(/'/g, "''")}'`, "literal");
791
+ const scope = opts.schema === void 0 ? "current_schema()" : literal(opts.schema);
792
+ 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
793
+ FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace JOIN pg_index i ON i.indexrelid = c.oid
794
+ WHERE c.relname = ${literal(LIFECYCLE_PENDING_INDEX)} AND n.nspname = ${scope}`) ?? [];
795
+ if (state?.valid || state?.building) return false;
796
+ const [tx] = await query("SELECT now() = statement_timestamp() AS autocommit") ?? [];
797
+ const concurrently = tx?.autocommit === false ? "" : "CONCURRENTLY ";
798
+ const qualified = opts.schema === void 0 ? `"${LIFECYCLE_PENDING_INDEX}"` : `"${opts.schema.replace(/"/g, '""')}"."${LIFECYCLE_PENDING_INDEX}"`;
799
+ if (state) await query(`DROP INDEX ${concurrently}IF EXISTS ${qualified}`);
800
+ await query(`CREATE INDEX ${concurrently}IF NOT EXISTS "${LIFECYCLE_PENDING_INDEX}" ON ${opts.table} (${opts.createdAt}) WHERE ${opts.predicate}`);
801
+ return true;
802
+ }
803
+ __name(ensurePgLifecyclePendingIndex, "ensurePgLifecyclePendingIndex");
804
+
805
+ // src/schema.ts
784
806
  var DURABLE_TABLE_NAMES = /* @__PURE__ */ new Set([
785
807
  "durable_workflow_runs",
786
808
  "durable_step_checkpoints",
@@ -800,7 +822,7 @@ __name(durableManagedTables, "durableManagedTables");
800
822
  var MARKER_TABLE = "durable_schema_meta";
801
823
  var MARKER_ROW_ID = "durable";
802
824
  var SCHEMA_LOCK_NAME = "durable_schema";
803
- var SCHEMA_REVISION = 1;
825
+ var SCHEMA_REVISION = 2;
804
826
  var DEFAULT_BLOCKING_ALTER_ROW_LIMIT = 1e5;
805
827
  function deferredAlterWarning(store, deferred, limit) {
806
828
  return [
@@ -1081,7 +1103,9 @@ async function ensureMikroOrmDurableSchema(orm, options = {}) {
1081
1103
  }
1082
1104
  const limit = options.blockingAlterRowLimit ?? DEFAULT_BLOCKING_ALTER_ROW_LIMIT;
1083
1105
  const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit, where);
1084
- if (deferred.length === 0) await writeFingerprint(orm, dialect, expected, where);
1106
+ const indexed = await healLifecycleIndex(orm, dialect, where, logger);
1107
+ if (indexed.built && !freshInstall) repairs.push(...indexed.repairs);
1108
+ if (deferred.length === 0 && indexed.ok) await writeFingerprint(orm, dialect, expected, where);
1085
1109
  if (repairs.length > 0 && !freshInstall) logger.warn(mikroOrmSchemaRepairWarning(repairs));
1086
1110
  if (deferred.length > 0) logger.warn(mikroOrmDeferredAlterWarning(deferred, limit));
1087
1111
  } finally {
@@ -1089,6 +1113,46 @@ async function ensureMikroOrmDurableSchema(orm, options = {}) {
1089
1113
  }
1090
1114
  }
1091
1115
  __name(ensureMikroOrmDurableSchema, "ensureMikroOrmDurableSchema");
1116
+ async function healLifecycleIndex(orm, dialect, where, logger) {
1117
+ if (dialect !== "postgres") return {
1118
+ ok: true,
1119
+ built: false,
1120
+ repairs: []
1121
+ };
1122
+ const meta = [
1123
+ ...orm.getMetadata().getAll().values()
1124
+ ].find((m) => m.class === WorkflowRunEntity || m.tableName === "durable_workflow_runs");
1125
+ if (!meta) return {
1126
+ ok: true,
1127
+ built: false,
1128
+ repairs: []
1129
+ };
1130
+ const column = /* @__PURE__ */ __name((prop) => where.quote(meta.properties?.[prop]?.fieldNames?.[0] ?? prop), "column");
1131
+ const table = where.schema === void 0 ? where.quote(meta.tableName) : `${where.quote(where.schema)}.${where.quote(meta.tableName)}`;
1132
+ try {
1133
+ const built = await ensurePgLifecyclePendingIndex((sql) => orm.em.getConnection().execute(sql), {
1134
+ schema: where.schema,
1135
+ table,
1136
+ createdAt: column("createdAt"),
1137
+ predicate: `${column("tags")}::text LIKE '${LIFECYCLE_PENDING_LIKE}'`
1138
+ });
1139
+ return {
1140
+ ok: true,
1141
+ built,
1142
+ repairs: built ? [
1143
+ `created index ${LIFECYCLE_PENDING_INDEX} on ${meta.tableName}`
1144
+ ] : []
1145
+ };
1146
+ } catch (error) {
1147
+ 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}`);
1148
+ return {
1149
+ ok: false,
1150
+ built: false,
1151
+ repairs: []
1152
+ };
1153
+ }
1154
+ }
1155
+ __name(healLifecycleIndex, "healLifecycleIndex");
1092
1156
  async function healDurableSchema(orm, dialect, blockingAlterRowLimit, where) {
1093
1157
  const sql = await orm.schema.getUpdateSchemaSQL({
1094
1158
  safe: true