@dudousxd/nestjs-durable-store-mikro-orm 0.37.0 → 0.38.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
@@ -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: [
@@ -854,6 +877,7 @@ function mikroOrmSchemaRepairWarning(repairs) {
854
877
  ].join(" ");
855
878
  }
856
879
  __name(mikroOrmSchemaRepairWarning, "mikroOrmSchemaRepairWarning");
880
+ var ALTER_TABLE_PREFIX = /^alter\s+table\s+(?:[`"']?[\w$]+[`"']?\.)?[`"']?[\w$]+[`"']?\s*/i;
857
881
  function unquote(identifier) {
858
882
  return identifier.replace(/^[`"']|[`"']$/g, "");
859
883
  }
@@ -870,7 +894,7 @@ function describeDurableStatement(statement) {
870
894
  if (/^alter\s+table\b/i.test(statement)) {
871
895
  const out = [];
872
896
  let other = false;
873
- for (const action of splitTopLevel(statement.replace(/^alter\s+table\s+[`"']?[\w$]+[`"']?\s*/i, ""))) {
897
+ for (const action of splitTopLevel(statement.replace(ALTER_TABLE_PREFIX, ""))) {
874
898
  const index = action.match(/^add\s+(?:unique\s+|fulltext\s+)?(?:index|key)\s+([`"']?[\w$]+[`"']?)/i);
875
899
  const constraint = action.match(/^add\s+constraint\s+([`"']?[\w$]+[`"']?)/i);
876
900
  const column = action.match(/^add\s+(?:column\s+)?([`"']?[\w$]+[`"']?)/i);
@@ -890,6 +914,11 @@ function describeDurableStatement(statement) {
890
914
  ];
891
915
  }
892
916
  __name(describeDurableStatement, "describeDurableStatement");
917
+ function inDurableSchema(statement, where) {
918
+ 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);
919
+ return match?.[1] === where.schema;
920
+ }
921
+ __name(inDurableSchema, "inDurableSchema");
893
922
  function splitTopLevel(text) {
894
923
  const parts = [];
895
924
  let depth = 0;
@@ -920,7 +949,7 @@ function compareStrings(a, b) {
920
949
  }
921
950
  __name(compareStrings, "compareStrings");
922
951
  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);
952
+ 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
953
  return match?.[1]?.toLowerCase();
925
954
  }
926
955
  __name(targetTable, "targetTable");
@@ -989,19 +1018,64 @@ function canonicalize(value) {
989
1018
  return JSON.stringify(value);
990
1019
  }
991
1020
  __name(canonicalize, "canonicalize");
992
- function createMarkerTableSql() {
993
- 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)`;
1021
+ function durableTableSchema(orm, configured) {
1022
+ const wanted = durableSchemaName(configured);
1023
+ const runs = [
1024
+ ...orm.getMetadata().getAll().values()
1025
+ ].find((meta) => meta.class === WorkflowRunEntity || meta.tableName === "durable_workflow_runs");
1026
+ const metaSchema2 = runs?.schema;
1027
+ const registered = metaSchema2 && metaSchema2 !== "*" ? metaSchema2 : void 0;
1028
+ const schema = wanted ?? registered;
1029
+ if (schema !== void 0 && detectSqlDialect(orm) === "sqlite") {
1030
+ 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().`);
1031
+ }
1032
+ if (wanted !== void 0 && wanted !== registered) {
1033
+ 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.`);
1034
+ }
1035
+ return schema;
1036
+ }
1037
+ __name(durableTableSchema, "durableTableSchema");
1038
+ function durableLocation(orm, configured) {
1039
+ const schema = durableTableSchema(orm, configured);
1040
+ const platform = orm.em.getPlatform();
1041
+ const quote = /* @__PURE__ */ __name((id) => platform.quoteIdentifier(id), "quote");
1042
+ return {
1043
+ schema,
1044
+ quote,
1045
+ marker: schema === void 0 ? MARKER_TABLE : `${quote(schema)}.${quote(MARKER_TABLE)}`
1046
+ };
1047
+ }
1048
+ __name(durableLocation, "durableLocation");
1049
+ async function createSchemaIfMissing(orm, dialect, where) {
1050
+ if (where.schema === void 0) return;
1051
+ const connection = orm.em.getConnection();
1052
+ 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 = ?", [
1053
+ where.schema
1054
+ ]);
1055
+ if (rows.length > 0) return;
1056
+ const kind = dialect === "mysql" ? "database" : "schema";
1057
+ try {
1058
+ await connection.execute(`create ${kind} if not exists ${where.quote(where.schema)}`);
1059
+ } catch (err) {
1060
+ 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}`, {
1061
+ cause: err
1062
+ });
1063
+ }
1064
+ }
1065
+ __name(createSchemaIfMissing, "createSchemaIfMissing");
1066
+ function createMarkerTableSql(marker) {
1067
+ 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
1068
  }
995
1069
  __name(createMarkerTableSql, "createMarkerTableSql");
996
- async function readStoredFingerprint(orm) {
997
- const rows = await orm.em.getConnection().execute(`select fingerprint from ${MARKER_TABLE} where id = ?`, [
1070
+ async function readStoredFingerprint(orm, where) {
1071
+ const rows = await orm.em.getConnection().execute(`select fingerprint from ${where.marker} where id = ?`, [
998
1072
  MARKER_ROW_ID
999
1073
  ]);
1000
1074
  return rows[0]?.fingerprint ?? void 0;
1001
1075
  }
1002
1076
  __name(readStoredFingerprint, "readStoredFingerprint");
1003
- async function writeFingerprint(orm, dialect, fingerprint) {
1004
- 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`;
1077
+ async function writeFingerprint(orm, dialect, fingerprint, where) {
1078
+ 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
1079
  await orm.em.getConnection().execute(upsert, [
1006
1080
  MARKER_ROW_ID,
1007
1081
  fingerprint,
@@ -1009,12 +1083,17 @@ async function writeFingerprint(orm, dialect, fingerprint) {
1009
1083
  ]);
1010
1084
  }
1011
1085
  __name(writeFingerprint, "writeFingerprint");
1012
- async function acquireSchemaLock(orm, dialect) {
1086
+ function schemaLockName(where) {
1087
+ return where.schema === void 0 ? SCHEMA_LOCK_NAME : `${SCHEMA_LOCK_NAME}:${where.schema}`;
1088
+ }
1089
+ __name(schemaLockName, "schemaLockName");
1090
+ async function acquireSchemaLock(orm, dialect, where) {
1091
+ const name = schemaLockName(where);
1013
1092
  try {
1014
1093
  if (dialect === "mysql") {
1015
- await orm.em.getConnection().execute(`select get_lock('${SCHEMA_LOCK_NAME}', 10)`);
1094
+ await orm.em.getConnection().execute(`select get_lock('${name}', 10)`);
1016
1095
  } else if (dialect === "postgres") {
1017
- await orm.em.getConnection().execute(`select pg_advisory_lock(hashtext('${SCHEMA_LOCK_NAME}'))`);
1096
+ await orm.em.getConnection().execute(`select pg_advisory_lock(hashtext('${name}'))`);
1018
1097
  }
1019
1098
  } catch (err) {
1020
1099
  const message = err instanceof Error ? err.message : String(err);
@@ -1022,12 +1101,13 @@ async function acquireSchemaLock(orm, dialect) {
1022
1101
  }
1023
1102
  }
1024
1103
  __name(acquireSchemaLock, "acquireSchemaLock");
1025
- async function releaseSchemaLock(orm, dialect) {
1104
+ async function releaseSchemaLock(orm, dialect, where) {
1105
+ const name = schemaLockName(where);
1026
1106
  try {
1027
1107
  if (dialect === "mysql") {
1028
- await orm.em.getConnection().execute(`select release_lock('${SCHEMA_LOCK_NAME}')`);
1108
+ await orm.em.getConnection().execute(`select release_lock('${name}')`);
1029
1109
  } else if (dialect === "postgres") {
1030
- await orm.em.getConnection().execute(`select pg_advisory_unlock(hashtext('${SCHEMA_LOCK_NAME}'))`);
1110
+ await orm.em.getConnection().execute(`select pg_advisory_unlock(hashtext('${name}'))`);
1031
1111
  }
1032
1112
  } catch {
1033
1113
  }
@@ -1036,35 +1116,38 @@ __name(releaseSchemaLock, "releaseSchemaLock");
1036
1116
  async function ensureMikroOrmDurableSchema(orm, options = {}) {
1037
1117
  const logger = options.logger ?? console;
1038
1118
  const dialect = detectSqlDialect(orm);
1039
- await orm.em.getConnection().execute(createMarkerTableSql());
1119
+ const where = durableLocation(orm, options.schema);
1120
+ await createSchemaIfMissing(orm, dialect, where);
1121
+ await orm.em.getConnection().execute(createMarkerTableSql(where.marker));
1040
1122
  const expected = computeExpectedFingerprint(orm, DURABLE_TABLE_NAMES);
1041
- const stored = await readStoredFingerprint(orm);
1123
+ const stored = await readStoredFingerprint(orm, where);
1042
1124
  if (stored === expected) {
1043
1125
  return;
1044
1126
  }
1045
- await acquireSchemaLock(orm, dialect);
1127
+ await acquireSchemaLock(orm, dialect, where);
1046
1128
  try {
1047
- if (await readStoredFingerprint(orm) === expected) {
1129
+ if (await readStoredFingerprint(orm, where) === expected) {
1048
1130
  return;
1049
1131
  }
1050
1132
  const limit = options.blockingAlterRowLimit ?? DEFAULT_BLOCKING_ALTER_ROW_LIMIT;
1051
- const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit);
1052
- if (deferred.length === 0) await writeFingerprint(orm, dialect, expected);
1133
+ const { repairs, freshInstall, deferred } = await healDurableSchema(orm, dialect, limit, where);
1134
+ if (deferred.length === 0) await writeFingerprint(orm, dialect, expected, where);
1053
1135
  if (repairs.length > 0 && !freshInstall) logger.warn(mikroOrmSchemaRepairWarning(repairs));
1054
1136
  if (deferred.length > 0) logger.warn(mikroOrmDeferredAlterWarning(deferred, limit));
1055
1137
  } finally {
1056
- await releaseSchemaLock(orm, dialect);
1138
+ await releaseSchemaLock(orm, dialect, where);
1057
1139
  }
1058
1140
  }
1059
1141
  __name(ensureMikroOrmDurableSchema, "ensureMikroOrmDurableSchema");
1060
- async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
1142
+ async function healDurableSchema(orm, dialect, blockingAlterRowLimit, where) {
1061
1143
  const sql = await orm.schema.getUpdateSchemaSQL({
1062
1144
  safe: true
1063
1145
  });
1064
- const ours = mergeColumnTypeChanges(sql.split(";").map((statement) => statement.trim()).filter((statement) => {
1146
+ const diff = sql.split(";").map((statement) => statement.trim()).filter((statement) => {
1065
1147
  const table = targetTable(statement);
1066
- return table !== void 0 && DURABLE_TABLE_NAMES.has(table);
1067
- }));
1148
+ return table !== void 0 && DURABLE_TABLE_NAMES.has(table) && inDurableSchema(statement, where);
1149
+ });
1150
+ const ours = mergeColumnTypeChanges(dialect === "mysql" && where.schema !== void 0 ? await narrowMysqlSchemaDiff(orm, where.schema, diff) : diff);
1068
1151
  const connection = orm.em.getConnection();
1069
1152
  const created = new Set(ours.filter((statement) => /^create\s+table\b/i.test(statement)).map((statement) => targetTable(statement)));
1070
1153
  const repairs = [];
@@ -1072,7 +1155,7 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
1072
1155
  for (const statement of ours) {
1073
1156
  const table = targetTable(statement);
1074
1157
  if (dialect === "mysql" && !created.has(table) && isColumnTypeChange(statement)) {
1075
- const rows = await estimatedRowCount(orm, table);
1158
+ const rows = await estimatedRowCount(orm, table, where);
1076
1159
  if (rows > blockingAlterRowLimit) {
1077
1160
  deferred.push({
1078
1161
  table,
@@ -1094,7 +1177,7 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
1094
1177
  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
1178
  }
1096
1179
  }
1097
- const converted = await alignDurableCollation(orm);
1180
+ const converted = await alignDurableCollation(orm, where);
1098
1181
  for (const { table, from, to } of converted) {
1099
1182
  if (!created.has(table)) repairs.push(`converted ${table} collation from ${from} to ${to}`);
1100
1183
  }
@@ -1108,6 +1191,40 @@ async function healDurableSchema(orm, dialect, blockingAlterRowLimit) {
1108
1191
  };
1109
1192
  }
1110
1193
  __name(healDurableSchema, "healDurableSchema");
1194
+ async function narrowMysqlSchemaDiff(orm, schema, diff) {
1195
+ const connection = orm.em.getConnection();
1196
+ const columns = await connection.execute("select table_name as table_name, column_name as column_name from information_schema.columns where table_schema = ?", [
1197
+ schema
1198
+ ]);
1199
+ const indexes = await connection.execute("select distinct table_name as table_name, index_name as index_name from information_schema.statistics where table_schema = ?", [
1200
+ schema
1201
+ ]);
1202
+ const tables = new Set(columns.map((c) => c.table_name.toLowerCase()));
1203
+ const hasColumn = new Set(columns.map((c) => `${c.table_name}.${c.column_name}`.toLowerCase()));
1204
+ const hasIndex = new Set(indexes.map((i) => `${i.table_name}.${i.index_name}`.toLowerCase()));
1205
+ const out = [];
1206
+ for (const statement of diff) {
1207
+ const table = targetTable(statement);
1208
+ if (!tables.has(table)) {
1209
+ out.push(statement);
1210
+ continue;
1211
+ }
1212
+ if (/^create\s+table\b/i.test(statement)) {
1213
+ const open = statement.indexOf("(");
1214
+ const close = statement.lastIndexOf(")");
1215
+ const prefix = statement.slice(0, open).replace(/^create\s+table\s+/i, "").trim();
1216
+ for (const definition of splitTopLevel(statement.slice(open + 1, close))) {
1217
+ const column = definition.match(/^[`"]([^`"]+)[`"]\s/);
1218
+ if (column?.[1] && !hasColumn.has(`${table}.${column[1]}`.toLowerCase())) out.push(`alter table ${prefix} add column ${definition}`);
1219
+ }
1220
+ continue;
1221
+ }
1222
+ const index = statement.match(/\badd\s+(?:unique\s+|fulltext\s+)?(?:index|key)\s+[`"]([^`"]+)[`"]/i);
1223
+ if (index?.[1] && !hasIndex.has(`${table}.${index[1]}`.toLowerCase())) out.push(statement);
1224
+ }
1225
+ return out;
1226
+ }
1227
+ __name(narrowMysqlSchemaDiff, "narrowMysqlSchemaDiff");
1111
1228
  function mergeColumnTypeChanges(statements) {
1112
1229
  const merged = [];
1113
1230
  const at = /* @__PURE__ */ new Map();
@@ -1117,7 +1234,7 @@ function mergeColumnTypeChanges(statements) {
1117
1234
  merged.push(statement);
1118
1235
  continue;
1119
1236
  }
1120
- const actions = statement.replace(/^alter\s+table\s+[`"']?[\w$]+[`"']?\s*/i, "");
1237
+ const actions = statement.replace(ALTER_TABLE_PREFIX, "");
1121
1238
  const index = at.get(table);
1122
1239
  if (index === void 0) {
1123
1240
  at.set(table, merged.length);
@@ -1131,12 +1248,15 @@ function mergeColumnTypeChanges(statements) {
1131
1248
  __name(mergeColumnTypeChanges, "mergeColumnTypeChanges");
1132
1249
  function isColumnTypeChange(statement) {
1133
1250
  if (!/^alter\s+table\b/i.test(statement)) return false;
1134
- const actions = splitTopLevel(statement.replace(/^alter\s+table\s+[`"']?[\w$]+[`"']?\s*/i, ""));
1251
+ const actions = splitTopLevel(statement.replace(ALTER_TABLE_PREFIX, ""));
1135
1252
  return actions.length > 0 && actions.every((action) => /^modify\b/i.test(action));
1136
1253
  }
1137
1254
  __name(isColumnTypeChange, "isColumnTypeChange");
1138
- async function estimatedRowCount(orm, table) {
1139
- 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", [
1255
+ async function estimatedRowCount(orm, table, where) {
1256
+ 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 ? [
1257
+ table
1258
+ ] : [
1259
+ where.schema,
1140
1260
  table
1141
1261
  ]);
1142
1262
  return Number(rows[0]?.table_rows ?? 0);
@@ -1146,7 +1266,7 @@ function isSafeSqlIdentifier(value) {
1146
1266
  return /^[A-Za-z0-9_]+$/.test(value);
1147
1267
  }
1148
1268
  __name(isSafeSqlIdentifier, "isSafeSqlIdentifier");
1149
- async function alignDurableCollation(orm) {
1269
+ async function alignDurableCollation(orm, where) {
1150
1270
  const converted = [];
1151
1271
  const platform = String(orm.em.getPlatform().constructor.name).toLowerCase();
1152
1272
  if (!platform.includes("mysql") && !platform.includes("maria")) {
@@ -1163,14 +1283,17 @@ async function alignDurableCollation(orm) {
1163
1283
  const connection = orm.em.getConnection();
1164
1284
  for (const table of DURABLE_TABLE_NAMES) {
1165
1285
  try {
1166
- const rows = await connection.execute("select table_collation as collation from information_schema.tables where table_schema = database() and table_name = ? limit 1", [
1286
+ 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 ? [
1287
+ table
1288
+ ] : [
1289
+ where.schema,
1167
1290
  table
1168
1291
  ]);
1169
1292
  const current = rows[0]?.collation;
1170
1293
  if (current == null || current === collate) {
1171
1294
  continue;
1172
1295
  }
1173
- await connection.execute(`alter table \`${table}\` convert to character set ${charset} collate ${collate}`);
1296
+ 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
1297
  converted.push({
1175
1298
  table,
1176
1299
  from: current,
@@ -1186,6 +1309,14 @@ async function alignDurableCollation(orm) {
1186
1309
  __name(alignDurableCollation, "alignDurableCollation");
1187
1310
 
1188
1311
  // src/mikro-orm-state-store.ts
1312
+ function metaSchema(meta) {
1313
+ return meta.schema && meta.schema !== "*" ? meta.schema : void 0;
1314
+ }
1315
+ __name(metaSchema, "metaSchema");
1316
+ function tableRef(quote, table, schema) {
1317
+ return schema === void 0 ? quote(table) : `${quote(schema)}.${quote(table)}`;
1318
+ }
1319
+ __name(tableRef, "tableRef");
1189
1320
  var RETENTION_EXCLUDED = [
1190
1321
  import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
1191
1322
  ];
@@ -1212,6 +1343,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1212
1343
  } catch {
1213
1344
  return;
1214
1345
  }
1346
+ durableTableSchema(orm, opts.schema);
1215
1347
  const dialect = platform.includes("postgre") ? "postgres" : platform.includes("mysql") ? "mysql" : void 0;
1216
1348
  if (!dialect) return;
1217
1349
  const fast = new MikroOrmRoundTrips(() => this.orm.em.fork(), dialect, this.scopeNamespace, this);
@@ -1250,7 +1382,8 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1250
1382
  async ensureSchema() {
1251
1383
  await ensureMikroOrmDurableSchema(this.orm, {
1252
1384
  logger: this.opts.schemaLogger,
1253
- blockingAlterRowLimit: this.opts.schemaBlockingAlterRowLimit
1385
+ blockingAlterRowLimit: this.opts.schemaBlockingAlterRowLimit,
1386
+ schema: this.opts.schema
1254
1387
  });
1255
1388
  }
1256
1389
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
@@ -1986,7 +2119,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1986
2119
  const cols = this.attributeColumns(em);
1987
2120
  const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
1988
2121
  const a = quote("a");
1989
- const join = `${quote(cols.table)} as ${a} join (${runs}) as ${quote("r")} on ${a}.${quote(cols.runId)} = ${quote("r")}.${quote(idCol)}`;
2122
+ 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
2123
  if (axis.field === "attributeKey") {
1991
2124
  return {
1992
2125
  source: join,
@@ -2043,7 +2176,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2043
2176
  return `${quote("a")}.${quote(col)} ${comparator} ${literal}`;
2044
2177
  });
2045
2178
  if (comparisons.length === 0) return "1 = 0";
2046
- const a = quote(cols.table);
2179
+ const a = tableRef(quote, cols.table, cols.schema);
2047
2180
  const sub = quote("a");
2048
2181
  const outerId = `${quote(alias)}.${quote(cols.runPk)}`;
2049
2182
  const match = comparisons.length === 1 ? comparisons[0] : `(${comparisons.join(" OR ")})`;
@@ -2059,6 +2192,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2059
2192
  const runPk = runMeta.properties.id?.fieldNames?.[0] ?? "id";
2060
2193
  return {
2061
2194
  table: meta.tableName,
2195
+ schema: metaSchema(meta),
2062
2196
  runId: field("runId"),
2063
2197
  runPk,
2064
2198
  key: field("key"),
@@ -2270,7 +2404,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2270
2404
  where += ` and ${c("parallelGroup")} = ?`;
2271
2405
  params.push(opts.group);
2272
2406
  }
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(meta.tableName)} where ${where} group by ${c("parallelGroup")}, ${c("status")}, ${nameKind}`;
2407
+ 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
2408
  const rows = await em.getConnection().execute(sql, params);
2275
2409
  const date = aggregateDateReader(em);
2276
2410
  return rows.map((row) => ({
@@ -2954,7 +3088,8 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
2954
3088
  return this.q(em, this.field(em, entity, property));
2955
3089
  }
2956
3090
  table(em, entity) {
2957
- return this.q(em, this.meta(em, entity).tableName);
3091
+ const meta = this.meta(em, entity);
3092
+ return tableRef((id) => this.q(em, id), meta.tableName, metaSchema(meta));
2958
3093
  }
2959
3094
  /**
2960
3095
  * `value` as a raw-SQL parameter for `property` (its custom type's conversion, if any).
@@ -3017,8 +3152,11 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
3017
3152
  }
3018
3153
  async cpType(em, property) {
3019
3154
  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
3155
+ 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 = ?", [
3156
+ this.meta(em, StepCheckpointEntity).tableName,
3157
+ ...metaSchema(this.meta(em, StepCheckpointEntity)) === void 0 ? [] : [
3158
+ metaSchema(this.meta(em, StepCheckpointEntity))
3159
+ ]
3022
3160
  ]).then((out) => new Map(this.rows(out).map((r) => [
3023
3161
  String(r.name),
3024
3162
  String(r.type)
@@ -3184,6 +3322,8 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
3184
3322
  WorkflowRunRepository,
3185
3323
  durableEntities,
3186
3324
  durableManagedTables,
3325
+ durableSchemaName,
3326
+ durableTableSchema,
3187
3327
  ensureMikroOrmDurableSchema,
3188
3328
  mikroOrmDeferredAlterWarning,
3189
3329
  mikroOrmSchemaRepairWarning