@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.js CHANGED
@@ -178,6 +178,15 @@ var ScheduleEntity = class {
178
178
  createdAt;
179
179
  updatedAt;
180
180
  };
181
+ var SCHEMA_NAME = /^[A-Za-z_][A-Za-z0-9_$]*$/;
182
+ function durableSchemaName(schema) {
183
+ if (schema === void 0) return void 0;
184
+ if (!SCHEMA_NAME.test(schema) || schema.length > 63) {
185
+ throw new Error(`[nestjs-durable] invalid schema ${JSON.stringify(schema)}: use letters, digits, _ or $ (not first), at most 63 characters.`);
186
+ }
187
+ return schema;
188
+ }
189
+ __name(durableSchemaName, "durableSchemaName");
181
190
  function namespaceFilterCond(args) {
182
191
  if (args?.namespace === void 0) return {};
183
192
  return {
@@ -187,9 +196,14 @@ function namespaceFilterCond(args) {
187
196
  __name(namespaceFilterCond, "namespaceFilterCond");
188
197
  function durableEntities(options = {}) {
189
198
  const col = columnNamer(options.naming ?? "snake_case");
199
+ const schema = durableSchemaName(options.schema);
200
+ const where = schema === void 0 ? {} : {
201
+ schema
202
+ };
190
203
  const workflowRuns = new EntitySchema({
191
204
  class: WorkflowRunEntity,
192
205
  tableName: "durable_workflow_runs",
206
+ ...where,
193
207
  // Wired per schema rather than once on the class: `durableEntities` can be called again with a
194
208
  // different `naming`, and each call builds a distinct EntitySchema over the same class.
195
209
  repository: /* @__PURE__ */ __name(() => WorkflowRunRepository, "repository"),
@@ -381,6 +395,7 @@ function durableEntities(options = {}) {
381
395
  const stepCheckpoints = new EntitySchema({
382
396
  class: StepCheckpointEntity,
383
397
  tableName: "durable_step_checkpoints",
398
+ ...where,
384
399
  repository: /* @__PURE__ */ __name(() => StepCheckpointRepository, "repository"),
385
400
  // `(runId, parallelGroup, seq)` serves the run detail's per-group reads — `checkpointStats` with a
386
401
  // group and `queryCheckpoints` paging one fan in `seq` order — without scanning the whole run.
@@ -487,6 +502,7 @@ function durableEntities(options = {}) {
487
502
  const runAttributes = new EntitySchema({
488
503
  class: RunAttributeEntity,
489
504
  tableName: "durable_run_attributes",
505
+ ...where,
490
506
  repository: /* @__PURE__ */ __name(() => RunAttributeRepository, "repository"),
491
507
  // The search-attribute EXISTS join (see the class docstring) pushes equality/range predicates down
492
508
  // onto `(key, numValue)` / `(key, strValue)`; mirror the Prisma adapter so those scans stay indexed.
@@ -533,6 +549,7 @@ function durableEntities(options = {}) {
533
549
  const signalWaiters = new EntitySchema({
534
550
  class: SignalWaiterEntity,
535
551
  tableName: "durable_signal_waiters",
552
+ ...where,
536
553
  repository: /* @__PURE__ */ __name(() => SignalWaiterRepository, "repository"),
537
554
  // A run's waiters: the `ctx.all` join barrier probes them once per settled child.
538
555
  indexes: [
@@ -570,6 +587,7 @@ function durableEntities(options = {}) {
570
587
  const bufferedSignals = new EntitySchema({
571
588
  class: BufferedSignalEntity,
572
589
  tableName: "durable_buffered_signals",
590
+ ...where,
573
591
  repository: /* @__PURE__ */ __name(() => BufferedSignalRepository, "repository"),
574
592
  properties: {
575
593
  id: {
@@ -593,6 +611,7 @@ function durableEntities(options = {}) {
593
611
  const bufferedEvents = new EntitySchema({
594
612
  class: BufferedEventEntity,
595
613
  tableName: "durable_buffered_events",
614
+ ...where,
596
615
  repository: /* @__PURE__ */ __name(() => BufferedEventRepository, "repository"),
597
616
  // `(name, publishedAt)` serves both `listBufferedEvents`'s name-scoped scan and its oldest-first
598
617
  // ordering in one index.
@@ -632,6 +651,7 @@ function durableEntities(options = {}) {
632
651
  const signalIds = new EntitySchema({
633
652
  class: SignalIdEntity,
634
653
  tableName: "durable_signal_ids",
654
+ ...where,
635
655
  repository: /* @__PURE__ */ __name(() => SignalIdRepository, "repository"),
636
656
  // The expired-id sweep (`expires_at <= now`).
637
657
  indexes: [
@@ -662,6 +682,7 @@ function durableEntities(options = {}) {
662
682
  const schedules = new EntitySchema({
663
683
  class: ScheduleEntity,
664
684
  tableName: "durable_schedules",
685
+ ...where,
665
686
  repository: /* @__PURE__ */ __name(() => ScheduleRepository, "repository"),
666
687
  // The poll's "due by T" scan: `paused = false AND next_fire_at <= T ORDER BY next_fire_at`.
667
688
  indexes: [
@@ -760,6 +781,28 @@ import { LockMode, UniqueConstraintViolationException, raw } from "@mikro-orm/co
760
781
 
761
782
  // src/schema.ts
762
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
763
806
  var DURABLE_TABLE_NAMES = /* @__PURE__ */ new Set([
764
807
  "durable_workflow_runs",
765
808
  "durable_step_checkpoints",
@@ -779,7 +822,7 @@ __name(durableManagedTables, "durableManagedTables");
779
822
  var MARKER_TABLE = "durable_schema_meta";
780
823
  var MARKER_ROW_ID = "durable";
781
824
  var SCHEMA_LOCK_NAME = "durable_schema";
782
- var SCHEMA_REVISION = 1;
825
+ var SCHEMA_REVISION = 2;
783
826
  var DEFAULT_BLOCKING_ALTER_ROW_LIMIT = 1e5;
784
827
  function deferredAlterWarning(store, deferred, limit) {
785
828
  return [
@@ -806,6 +849,7 @@ function mikroOrmSchemaRepairWarning(repairs) {
806
849
  ].join(" ");
807
850
  }
808
851
  __name(mikroOrmSchemaRepairWarning, "mikroOrmSchemaRepairWarning");
852
+ var ALTER_TABLE_PREFIX = /^alter\s+table\s+(?:[`"']?[\w$]+[`"']?\.)?[`"']?[\w$]+[`"']?\s*/i;
809
853
  function unquote(identifier) {
810
854
  return identifier.replace(/^[`"']|[`"']$/g, "");
811
855
  }
@@ -822,7 +866,7 @@ function describeDurableStatement(statement) {
822
866
  if (/^alter\s+table\b/i.test(statement)) {
823
867
  const out = [];
824
868
  let other = false;
825
- for (const action of splitTopLevel(statement.replace(/^alter\s+table\s+[`"']?[\w$]+[`"']?\s*/i, ""))) {
869
+ for (const action of splitTopLevel(statement.replace(ALTER_TABLE_PREFIX, ""))) {
826
870
  const index = action.match(/^add\s+(?:unique\s+|fulltext\s+)?(?:index|key)\s+([`"']?[\w$]+[`"']?)/i);
827
871
  const constraint = action.match(/^add\s+constraint\s+([`"']?[\w$]+[`"']?)/i);
828
872
  const column = action.match(/^add\s+(?:column\s+)?([`"']?[\w$]+[`"']?)/i);
@@ -842,6 +886,11 @@ function describeDurableStatement(statement) {
842
886
  ];
843
887
  }
844
888
  __name(describeDurableStatement, "describeDurableStatement");
889
+ function inDurableSchema(statement, where) {
890
+ 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);
891
+ return match?.[1] === where.schema;
892
+ }
893
+ __name(inDurableSchema, "inDurableSchema");
845
894
  function splitTopLevel(text) {
846
895
  const parts = [];
847
896
  let depth = 0;
@@ -872,7 +921,7 @@ function compareStrings(a, b) {
872
921
  }
873
922
  __name(compareStrings, "compareStrings");
874
923
  function targetTable(statement) {
875
- 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);
924
+ 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);
876
925
  return match?.[1]?.toLowerCase();
877
926
  }
878
927
  __name(targetTable, "targetTable");
@@ -941,19 +990,64 @@ function canonicalize(value) {
941
990
  return JSON.stringify(value);
942
991
  }
943
992
  __name(canonicalize, "canonicalize");
944
- function createMarkerTableSql() {
945
- return `create table if not exists ${MARKER_TABLE} (id varchar(32) not null primary key, fingerprint varchar(64) not null, applied_at bigint not null)`;
993
+ function durableTableSchema(orm, configured) {
994
+ const wanted = durableSchemaName(configured);
995
+ const runs = [
996
+ ...orm.getMetadata().getAll().values()
997
+ ].find((meta) => meta.class === WorkflowRunEntity || meta.tableName === "durable_workflow_runs");
998
+ const metaSchema2 = runs?.schema;
999
+ const registered = metaSchema2 && metaSchema2 !== "*" ? metaSchema2 : void 0;
1000
+ const schema = wanted ?? registered;
1001
+ if (schema !== void 0 && detectSqlDialect(orm) === "sqlite") {
1002
+ 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().`);
1003
+ }
1004
+ if (wanted !== void 0 && wanted !== registered) {
1005
+ 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.`);
1006
+ }
1007
+ return schema;
1008
+ }
1009
+ __name(durableTableSchema, "durableTableSchema");
1010
+ function durableLocation(orm, configured) {
1011
+ const schema = durableTableSchema(orm, configured);
1012
+ const platform = orm.em.getPlatform();
1013
+ const quote = /* @__PURE__ */ __name((id) => platform.quoteIdentifier(id), "quote");
1014
+ return {
1015
+ schema,
1016
+ quote,
1017
+ marker: schema === void 0 ? MARKER_TABLE : `${quote(schema)}.${quote(MARKER_TABLE)}`
1018
+ };
1019
+ }
1020
+ __name(durableLocation, "durableLocation");
1021
+ async function createSchemaIfMissing(orm, dialect, where) {
1022
+ if (where.schema === void 0) return;
1023
+ const connection = orm.em.getConnection();
1024
+ 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 = ?", [
1025
+ where.schema
1026
+ ]);
1027
+ if (rows.length > 0) return;
1028
+ const kind = dialect === "mysql" ? "database" : "schema";
1029
+ try {
1030
+ await connection.execute(`create ${kind} if not exists ${where.quote(where.schema)}`);
1031
+ } catch (err) {
1032
+ 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}`, {
1033
+ cause: err
1034
+ });
1035
+ }
1036
+ }
1037
+ __name(createSchemaIfMissing, "createSchemaIfMissing");
1038
+ function createMarkerTableSql(marker) {
1039
+ return `create table if not exists ${marker} (id varchar(32) not null primary key, fingerprint varchar(64) not null, applied_at bigint not null)`;
946
1040
  }
947
1041
  __name(createMarkerTableSql, "createMarkerTableSql");
948
- async function readStoredFingerprint(orm) {
949
- const rows = await orm.em.getConnection().execute(`select fingerprint from ${MARKER_TABLE} where id = ?`, [
1042
+ async function readStoredFingerprint(orm, where) {
1043
+ const rows = await orm.em.getConnection().execute(`select fingerprint from ${where.marker} where id = ?`, [
950
1044
  MARKER_ROW_ID
951
1045
  ]);
952
1046
  return rows[0]?.fingerprint ?? void 0;
953
1047
  }
954
1048
  __name(readStoredFingerprint, "readStoredFingerprint");
955
- async function writeFingerprint(orm, dialect, fingerprint) {
956
- const upsert = dialect === "mysql" ? `insert into ${MARKER_TABLE} (id, fingerprint, applied_at) values (?, ?, ?) on duplicate key update fingerprint = values(fingerprint), applied_at = values(applied_at)` : `insert into ${MARKER_TABLE} (id, fingerprint, applied_at) values (?, ?, ?) on conflict (id) do update set fingerprint = excluded.fingerprint, applied_at = excluded.applied_at`;
1049
+ async function writeFingerprint(orm, dialect, fingerprint, where) {
1050
+ 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`;
957
1051
  await orm.em.getConnection().execute(upsert, [
958
1052
  MARKER_ROW_ID,
959
1053
  fingerprint,
@@ -961,12 +1055,17 @@ async function writeFingerprint(orm, dialect, fingerprint) {
961
1055
  ]);
962
1056
  }
963
1057
  __name(writeFingerprint, "writeFingerprint");
964
- async function acquireSchemaLock(orm, dialect) {
1058
+ function schemaLockName(where) {
1059
+ return where.schema === void 0 ? SCHEMA_LOCK_NAME : `${SCHEMA_LOCK_NAME}:${where.schema}`;
1060
+ }
1061
+ __name(schemaLockName, "schemaLockName");
1062
+ async function acquireSchemaLock(orm, dialect, where) {
1063
+ const name = schemaLockName(where);
965
1064
  try {
966
1065
  if (dialect === "mysql") {
967
- await orm.em.getConnection().execute(`select get_lock('${SCHEMA_LOCK_NAME}', 10)`);
1066
+ await orm.em.getConnection().execute(`select get_lock('${name}', 10)`);
968
1067
  } else if (dialect === "postgres") {
969
- await orm.em.getConnection().execute(`select pg_advisory_lock(hashtext('${SCHEMA_LOCK_NAME}'))`);
1068
+ await orm.em.getConnection().execute(`select pg_advisory_lock(hashtext('${name}'))`);
970
1069
  }
971
1070
  } catch (err) {
972
1071
  const message = err instanceof Error ? err.message : String(err);
@@ -974,12 +1073,13 @@ async function acquireSchemaLock(orm, dialect) {
974
1073
  }
975
1074
  }
976
1075
  __name(acquireSchemaLock, "acquireSchemaLock");
977
- async function releaseSchemaLock(orm, dialect) {
1076
+ async function releaseSchemaLock(orm, dialect, where) {
1077
+ const name = schemaLockName(where);
978
1078
  try {
979
1079
  if (dialect === "mysql") {
980
- await orm.em.getConnection().execute(`select release_lock('${SCHEMA_LOCK_NAME}')`);
1080
+ await orm.em.getConnection().execute(`select release_lock('${name}')`);
981
1081
  } else if (dialect === "postgres") {
982
- await orm.em.getConnection().execute(`select pg_advisory_unlock(hashtext('${SCHEMA_LOCK_NAME}'))`);
1082
+ await orm.em.getConnection().execute(`select pg_advisory_unlock(hashtext('${name}'))`);
983
1083
  }
984
1084
  } catch {
985
1085
  }
@@ -988,35 +1088,80 @@ __name(releaseSchemaLock, "releaseSchemaLock");
988
1088
  async function ensureMikroOrmDurableSchema(orm, options = {}) {
989
1089
  const logger = options.logger ?? console;
990
1090
  const dialect = detectSqlDialect(orm);
991
- await orm.em.getConnection().execute(createMarkerTableSql());
1091
+ const where = durableLocation(orm, options.schema);
1092
+ await createSchemaIfMissing(orm, dialect, where);
1093
+ await orm.em.getConnection().execute(createMarkerTableSql(where.marker));
992
1094
  const expected = computeExpectedFingerprint(orm, DURABLE_TABLE_NAMES);
993
- const stored = await readStoredFingerprint(orm);
1095
+ const stored = await readStoredFingerprint(orm, where);
994
1096
  if (stored === expected) {
995
1097
  return;
996
1098
  }
997
- await acquireSchemaLock(orm, dialect);
1099
+ await acquireSchemaLock(orm, dialect, where);
998
1100
  try {
999
- if (await readStoredFingerprint(orm) === expected) {
1101
+ if (await readStoredFingerprint(orm, where) === expected) {
1000
1102
  return;
1001
1103
  }
1002
1104
  const limit = options.blockingAlterRowLimit ?? DEFAULT_BLOCKING_ALTER_ROW_LIMIT;
1003
- const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit);
1004
- if (deferred.length === 0) await writeFingerprint(orm, dialect, expected);
1105
+ const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit, 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);
1005
1109
  if (repairs.length > 0 && !freshInstall) logger.warn(mikroOrmSchemaRepairWarning(repairs));
1006
1110
  if (deferred.length > 0) logger.warn(mikroOrmDeferredAlterWarning(deferred, limit));
1007
1111
  } finally {
1008
- await releaseSchemaLock(orm, dialect);
1112
+ await releaseSchemaLock(orm, dialect, where);
1009
1113
  }
1010
1114
  }
1011
1115
  __name(ensureMikroOrmDurableSchema, "ensureMikroOrmDurableSchema");
1012
- async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
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");
1156
+ async function healDurableSchema(orm, dialect, blockingAlterRowLimit, where) {
1013
1157
  const sql = await orm.schema.getUpdateSchemaSQL({
1014
1158
  safe: true
1015
1159
  });
1016
- const ours = mergeColumnTypeChanges(sql.split(";").map((statement) => statement.trim()).filter((statement) => {
1160
+ const diff = sql.split(";").map((statement) => statement.trim()).filter((statement) => {
1017
1161
  const table = targetTable(statement);
1018
- return table !== void 0 && DURABLE_TABLE_NAMES.has(table);
1019
- }));
1162
+ return table !== void 0 && DURABLE_TABLE_NAMES.has(table) && inDurableSchema(statement, where);
1163
+ });
1164
+ const ours = mergeColumnTypeChanges(dialect === "mysql" && where.schema !== void 0 ? await narrowMysqlSchemaDiff(orm, where.schema, diff) : diff);
1020
1165
  const connection = orm.em.getConnection();
1021
1166
  const created = new Set(ours.filter((statement) => /^create\s+table\b/i.test(statement)).map((statement) => targetTable(statement)));
1022
1167
  const repairs = [];
@@ -1024,7 +1169,7 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
1024
1169
  for (const statement of ours) {
1025
1170
  const table = targetTable(statement);
1026
1171
  if (dialect === "mysql" && !created.has(table) && isColumnTypeChange(statement)) {
1027
- const rows = await estimatedRowCount(orm, table);
1172
+ const rows = await estimatedRowCount(orm, table, where);
1028
1173
  if (rows > blockingAlterRowLimit) {
1029
1174
  deferred.push({
1030
1175
  table,
@@ -1046,7 +1191,7 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
1046
1191
  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}`);
1047
1192
  }
1048
1193
  }
1049
- const converted = await alignDurableCollation(orm);
1194
+ const converted = await alignDurableCollation(orm, where);
1050
1195
  for (const { table, from, to } of converted) {
1051
1196
  if (!created.has(table)) repairs.push(`converted ${table} collation from ${from} to ${to}`);
1052
1197
  }
@@ -1060,6 +1205,40 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
1060
1205
  };
1061
1206
  }
1062
1207
  __name(healDurableSchema, "healDurableSchema");
1208
+ async function narrowMysqlSchemaDiff(orm, schema, diff) {
1209
+ const connection = orm.em.getConnection();
1210
+ const columns = await connection.execute("select table_name as table_name, column_name as column_name from information_schema.columns where table_schema = ?", [
1211
+ schema
1212
+ ]);
1213
+ const indexes = await connection.execute("select distinct table_name as table_name, index_name as index_name from information_schema.statistics where table_schema = ?", [
1214
+ schema
1215
+ ]);
1216
+ const tables = new Set(columns.map((c) => c.table_name.toLowerCase()));
1217
+ const hasColumn = new Set(columns.map((c) => `${c.table_name}.${c.column_name}`.toLowerCase()));
1218
+ const hasIndex = new Set(indexes.map((i) => `${i.table_name}.${i.index_name}`.toLowerCase()));
1219
+ const out = [];
1220
+ for (const statement of diff) {
1221
+ const table = targetTable(statement);
1222
+ if (!tables.has(table)) {
1223
+ out.push(statement);
1224
+ continue;
1225
+ }
1226
+ if (/^create\s+table\b/i.test(statement)) {
1227
+ const open = statement.indexOf("(");
1228
+ const close = statement.lastIndexOf(")");
1229
+ const prefix = statement.slice(0, open).replace(/^create\s+table\s+/i, "").trim();
1230
+ for (const definition of splitTopLevel(statement.slice(open + 1, close))) {
1231
+ const column = definition.match(/^[`"]([^`"]+)[`"]\s/);
1232
+ if (column?.[1] && !hasColumn.has(`${table}.${column[1]}`.toLowerCase())) out.push(`alter table ${prefix} add column ${definition}`);
1233
+ }
1234
+ continue;
1235
+ }
1236
+ const index = statement.match(/\badd\s+(?:unique\s+|fulltext\s+)?(?:index|key)\s+[`"]([^`"]+)[`"]/i);
1237
+ if (index?.[1] && !hasIndex.has(`${table}.${index[1]}`.toLowerCase())) out.push(statement);
1238
+ }
1239
+ return out;
1240
+ }
1241
+ __name(narrowMysqlSchemaDiff, "narrowMysqlSchemaDiff");
1063
1242
  function mergeColumnTypeChanges(statements) {
1064
1243
  const merged = [];
1065
1244
  const at = /* @__PURE__ */ new Map();
@@ -1069,7 +1248,7 @@ function mergeColumnTypeChanges(statements) {
1069
1248
  merged.push(statement);
1070
1249
  continue;
1071
1250
  }
1072
- const actions = statement.replace(/^alter\s+table\s+[`"']?[\w$]+[`"']?\s*/i, "");
1251
+ const actions = statement.replace(ALTER_TABLE_PREFIX, "");
1073
1252
  const index = at.get(table);
1074
1253
  if (index === void 0) {
1075
1254
  at.set(table, merged.length);
@@ -1083,12 +1262,15 @@ function mergeColumnTypeChanges(statements) {
1083
1262
  __name(mergeColumnTypeChanges, "mergeColumnTypeChanges");
1084
1263
  function isColumnTypeChange(statement) {
1085
1264
  if (!/^alter\s+table\b/i.test(statement)) return false;
1086
- const actions = splitTopLevel(statement.replace(/^alter\s+table\s+[`"']?[\w$]+[`"']?\s*/i, ""));
1265
+ const actions = splitTopLevel(statement.replace(ALTER_TABLE_PREFIX, ""));
1087
1266
  return actions.length > 0 && actions.every((action) => /^modify\b/i.test(action));
1088
1267
  }
1089
1268
  __name(isColumnTypeChange, "isColumnTypeChange");
1090
- async function estimatedRowCount(orm, table) {
1091
- const rows = await orm.em.getConnection().execute("select table_rows as table_rows from information_schema.tables where table_schema = database() and table_name = ? limit 1", [
1269
+ async function estimatedRowCount(orm, table, where) {
1270
+ 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 ? [
1271
+ table
1272
+ ] : [
1273
+ where.schema,
1092
1274
  table
1093
1275
  ]);
1094
1276
  return Number(rows[0]?.table_rows ?? 0);
@@ -1098,7 +1280,7 @@ function isSafeSqlIdentifier(value) {
1098
1280
  return /^[A-Za-z0-9_]+$/.test(value);
1099
1281
  }
1100
1282
  __name(isSafeSqlIdentifier, "isSafeSqlIdentifier");
1101
- async function alignDurableCollation(orm) {
1283
+ async function alignDurableCollation(orm, where) {
1102
1284
  const converted = [];
1103
1285
  const platform = String(orm.em.getPlatform().constructor.name).toLowerCase();
1104
1286
  if (!platform.includes("mysql") && !platform.includes("maria")) {
@@ -1115,14 +1297,17 @@ async function alignDurableCollation(orm) {
1115
1297
  const connection = orm.em.getConnection();
1116
1298
  for (const table of DURABLE_TABLE_NAMES) {
1117
1299
  try {
1118
- const rows = await connection.execute("select table_collation as collation from information_schema.tables where table_schema = database() and table_name = ? limit 1", [
1300
+ 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 ? [
1301
+ table
1302
+ ] : [
1303
+ where.schema,
1119
1304
  table
1120
1305
  ]);
1121
1306
  const current = rows[0]?.collation;
1122
1307
  if (current == null || current === collate) {
1123
1308
  continue;
1124
1309
  }
1125
- await connection.execute(`alter table \`${table}\` convert to character set ${charset} collate ${collate}`);
1310
+ await connection.execute(`alter table ${where.schema === void 0 ? `\`${table}\`` : `${where.quote(where.schema)}.${where.quote(table)}`} convert to character set ${charset} collate ${collate}`);
1126
1311
  converted.push({
1127
1312
  table,
1128
1313
  from: current,
@@ -1138,6 +1323,14 @@ async function alignDurableCollation(orm) {
1138
1323
  __name(alignDurableCollation, "alignDurableCollation");
1139
1324
 
1140
1325
  // src/mikro-orm-state-store.ts
1326
+ function metaSchema(meta) {
1327
+ return meta.schema && meta.schema !== "*" ? meta.schema : void 0;
1328
+ }
1329
+ __name(metaSchema, "metaSchema");
1330
+ function tableRef(quote, table, schema) {
1331
+ return schema === void 0 ? quote(table) : `${quote(schema)}.${quote(table)}`;
1332
+ }
1333
+ __name(tableRef, "tableRef");
1141
1334
  var RETENTION_EXCLUDED = [
1142
1335
  LIFECYCLE_REGISTRY_WORKFLOW
1143
1336
  ];
@@ -1164,6 +1357,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1164
1357
  } catch {
1165
1358
  return;
1166
1359
  }
1360
+ durableTableSchema(orm, opts.schema);
1167
1361
  const dialect = platform.includes("postgre") ? "postgres" : platform.includes("mysql") ? "mysql" : void 0;
1168
1362
  if (!dialect) return;
1169
1363
  const fast = new MikroOrmRoundTrips(() => this.orm.em.fork(), dialect, this.scopeNamespace, this);
@@ -1202,7 +1396,8 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1202
1396
  async ensureSchema() {
1203
1397
  await ensureMikroOrmDurableSchema(this.orm, {
1204
1398
  logger: this.opts.schemaLogger,
1205
- blockingAlterRowLimit: this.opts.schemaBlockingAlterRowLimit
1399
+ blockingAlterRowLimit: this.opts.schemaBlockingAlterRowLimit,
1400
+ schema: this.opts.schema
1206
1401
  });
1207
1402
  }
1208
1403
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
@@ -1938,7 +2133,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1938
2133
  const cols = this.attributeColumns(em);
1939
2134
  const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
1940
2135
  const a = quote("a");
1941
- const join = `${quote(cols.table)} as ${a} join (${runs}) as ${quote("r")} on ${a}.${quote(cols.runId)} = ${quote("r")}.${quote(idCol)}`;
2136
+ const join = `${tableRef(quote, cols.table, cols.schema)} as ${a} join (${runs}) as ${quote("r")} on ${a}.${quote(cols.runId)} = ${quote("r")}.${quote(idCol)}`;
1942
2137
  if (axis.field === "attributeKey") {
1943
2138
  return {
1944
2139
  source: join,
@@ -1995,7 +2190,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1995
2190
  return `${quote("a")}.${quote(col)} ${comparator} ${literal}`;
1996
2191
  });
1997
2192
  if (comparisons.length === 0) return "1 = 0";
1998
- const a = quote(cols.table);
2193
+ const a = tableRef(quote, cols.table, cols.schema);
1999
2194
  const sub = quote("a");
2000
2195
  const outerId = `${quote(alias)}.${quote(cols.runPk)}`;
2001
2196
  const match = comparisons.length === 1 ? comparisons[0] : `(${comparisons.join(" OR ")})`;
@@ -2011,6 +2206,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2011
2206
  const runPk = runMeta.properties.id?.fieldNames?.[0] ?? "id";
2012
2207
  return {
2013
2208
  table: meta.tableName,
2209
+ schema: metaSchema(meta),
2014
2210
  runId: field("runId"),
2015
2211
  runPk,
2016
2212
  key: field("key"),
@@ -2222,7 +2418,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2222
2418
  where += ` and ${c("parallelGroup")} = ?`;
2223
2419
  params.push(opts.group);
2224
2420
  }
2225
- 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(meta.tableName)} where ${where} group by ${c("parallelGroup")}, ${c("status")}, ${nameKind}`;
2421
+ 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}`;
2226
2422
  const rows = await em.getConnection().execute(sql, params);
2227
2423
  const date = aggregateDateReader(em);
2228
2424
  return rows.map((row) => ({
@@ -2906,7 +3102,8 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
2906
3102
  return this.q(em, this.field(em, entity, property));
2907
3103
  }
2908
3104
  table(em, entity) {
2909
- return this.q(em, this.meta(em, entity).tableName);
3105
+ const meta = this.meta(em, entity);
3106
+ return tableRef((id) => this.q(em, id), meta.tableName, metaSchema(meta));
2910
3107
  }
2911
3108
  /**
2912
3109
  * `value` as a raw-SQL parameter for `property` (its custom type's conversion, if any).
@@ -2969,8 +3166,11 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
2969
3166
  }
2970
3167
  async cpType(em, property) {
2971
3168
  const field = this.field(em, StepCheckpointEntity, property);
2972
- 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()", [
2973
- this.meta(em, StepCheckpointEntity).tableName
3169
+ 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 = ?", [
3170
+ this.meta(em, StepCheckpointEntity).tableName,
3171
+ ...metaSchema(this.meta(em, StepCheckpointEntity)) === void 0 ? [] : [
3172
+ metaSchema(this.meta(em, StepCheckpointEntity))
3173
+ ]
2974
3174
  ]).then((out) => new Map(this.rows(out).map((r) => [
2975
3175
  String(r.name),
2976
3176
  String(r.type)
@@ -3135,6 +3335,8 @@ export {
3135
3335
  WorkflowRunRepository,
3136
3336
  durableEntities,
3137
3337
  durableManagedTables,
3338
+ durableSchemaName,
3339
+ durableTableSchema,
3138
3340
  ensureMikroOrmDurableSchema,
3139
3341
  mikroOrmDeferredAlterWarning,
3140
3342
  mikroOrmSchemaRepairWarning