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.js CHANGED
@@ -3559,16 +3559,32 @@ const checkMigrationOrder = (config, set, { sequence, map }, force) => {
3559
3559
  return version;
3560
3560
  }
3561
3561
  };
3562
- const changeCache = {};
3562
+ /**
3563
+ * Cache of migration changes, keyed by the loaded migration module.
3564
+ * Different migration sets with the same keys load different modules, so they don't share changes.
3565
+ *
3566
+ * A migration file registers its changes by calling `change` at the top level, which runs only on the first import:
3567
+ * subsequent imports return the already evaluated module and register nothing.
3568
+ * The cache allows running the same migration again, for example, when migrating several databases.
3569
+ */
3570
+ const changesByModule = /* @__PURE__ */ new WeakMap();
3571
+ /**
3572
+ * Cache of migration changes, keyed by the migration path, for loaders that don't return a module, such as file loaders.
3573
+ */
3574
+ const changesByPath = /* @__PURE__ */ new Map();
3563
3575
  const getChanges = async (file, config) => {
3564
3576
  clearChanges();
3565
- let changes = file.path ? changeCache[file.path] : void 0;
3577
+ const module = await file.load();
3578
+ const isModule = typeof module === "object" && module !== null;
3579
+ let changes;
3580
+ if (isModule) changes = changesByModule.get(module);
3581
+ else if (file.path) changes = changesByPath.get(file.path);
3566
3582
  if (!changes) {
3567
- const module = await file.load();
3568
3583
  const exported = module?.default && (0, pqb_internal.toArray)(module.default);
3569
3584
  if (config?.forceDefaultExports && !exported) throw new RakeDbError(`Missing a default export in ${file.path} migration`);
3570
3585
  changes = exported || getCurrentChanges();
3571
- if (file.path) changeCache[file.path] = changes;
3586
+ if (isModule) changesByModule.set(module, changes);
3587
+ else if (file.path) changesByPath.set(file.path, changes);
3572
3588
  }
3573
3589
  return changes;
3574
3590
  };