rake-db 2.37.12 → 2.37.13

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.mjs CHANGED
@@ -3536,16 +3536,32 @@ const checkMigrationOrder = (config, set, { sequence, map }, force) => {
3536
3536
  return version;
3537
3537
  }
3538
3538
  };
3539
- const changeCache = {};
3539
+ /**
3540
+ * Cache of migration changes, keyed by the loaded migration module.
3541
+ * Different migration sets with the same keys load different modules, so they don't share changes.
3542
+ *
3543
+ * A migration file registers its changes by calling `change` at the top level, which runs only on the first import:
3544
+ * subsequent imports return the already evaluated module and register nothing.
3545
+ * The cache allows running the same migration again, for example, when migrating several databases.
3546
+ */
3547
+ const changesByModule = /* @__PURE__ */ new WeakMap();
3548
+ /**
3549
+ * Cache of migration changes, keyed by the migration path, for loaders that don't return a module, such as file loaders.
3550
+ */
3551
+ const changesByPath = /* @__PURE__ */ new Map();
3540
3552
  const getChanges = async (file, config) => {
3541
3553
  clearChanges();
3542
- let changes = file.path ? changeCache[file.path] : void 0;
3554
+ const module = await file.load();
3555
+ const isModule = typeof module === "object" && module !== null;
3556
+ let changes;
3557
+ if (isModule) changes = changesByModule.get(module);
3558
+ else if (file.path) changes = changesByPath.get(file.path);
3543
3559
  if (!changes) {
3544
- const module = await file.load();
3545
3560
  const exported = module?.default && toArray(module.default);
3546
3561
  if (config?.forceDefaultExports && !exported) throw new RakeDbError(`Missing a default export in ${file.path} migration`);
3547
3562
  changes = exported || getCurrentChanges();
3548
- if (file.path) changeCache[file.path] = changes;
3563
+ if (isModule) changesByModule.set(module, changes);
3564
+ else if (file.path) changesByPath.set(file.path, changes);
3549
3565
  }
3550
3566
  return changes;
3551
3567
  };