@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.cjs CHANGED
@@ -831,6 +831,28 @@ var import_core3 = require("@mikro-orm/core");
831
831
 
832
832
  // src/schema.ts
833
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
834
856
  var DURABLE_TABLE_NAMES = /* @__PURE__ */ new Set([
835
857
  "durable_workflow_runs",
836
858
  "durable_step_checkpoints",
@@ -850,7 +872,7 @@ __name(durableManagedTables, "durableManagedTables");
850
872
  var MARKER_TABLE = "durable_schema_meta";
851
873
  var MARKER_ROW_ID = "durable";
852
874
  var SCHEMA_LOCK_NAME = "durable_schema";
853
- var SCHEMA_REVISION = 1;
875
+ var SCHEMA_REVISION = 2;
854
876
  var DEFAULT_BLOCKING_ALTER_ROW_LIMIT = 1e5;
855
877
  function deferredAlterWarning(store, deferred, limit) {
856
878
  return [
@@ -1131,7 +1153,9 @@ async function ensureMikroOrmDurableSchema(orm, options = {}) {
1131
1153
  }
1132
1154
  const limit = options.blockingAlterRowLimit ?? DEFAULT_BLOCKING_ALTER_ROW_LIMIT;
1133
1155
  const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit, where);
1134
- if (deferred.length === 0) await writeFingerprint(orm, dialect, expected, 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);
1135
1159
  if (repairs.length > 0 && !freshInstall) logger.warn(mikroOrmSchemaRepairWarning(repairs));
1136
1160
  if (deferred.length > 0) logger.warn(mikroOrmDeferredAlterWarning(deferred, limit));
1137
1161
  } finally {
@@ -1139,6 +1163,46 @@ async function ensureMikroOrmDurableSchema(orm, options = {}) {
1139
1163
  }
1140
1164
  }
1141
1165
  __name(ensureMikroOrmDurableSchema, "ensureMikroOrmDurableSchema");
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");
1142
1206
  async function healDurableSchema(orm, dialect, blockingAlterRowLimit, where) {
1143
1207
  const sql = await orm.schema.getUpdateSchemaSQL({
1144
1208
  safe: true