rake-db 2.37.12 → 2.37.14

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
@@ -3,7 +3,7 @@ import path, { join } from "node:path";
3
3
  import { fileURLToPath, pathToFileURL } from "node:url";
4
4
  import { createDbWithAdapter } from "pqb";
5
5
  import fs, { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
6
- const RAKE_DB_LOCK_KEY = "8582141715823621641";
6
+ const RAKE_DB_LOCK_KEY = 8582141715823621641n;
7
7
  const getFirstWordAndRest = (input) => {
8
8
  const i = input.search(/(?=[A-Z])|[-_ ]/);
9
9
  if (i !== -1) {
@@ -65,7 +65,7 @@ const transaction = (adapter, config, fn) => {
65
65
  const searchPath = config.transactionSearchPath;
66
66
  return adapter.transaction(void 0, searchPath ? { setConfig: { search_path: typeof searchPath === "function" ? searchPath() : searchPath } } : void 0, fn);
67
67
  };
68
- const queryLock = (trx) => trx.query(`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`);
68
+ const queryLock = (trx, key = RAKE_DB_LOCK_KEY) => trx.query(`SELECT pg_advisory_xact_lock('${key}')`);
69
69
  const getCliParam = (args, name) => {
70
70
  if (args) {
71
71
  const key = "--" + name;
@@ -3459,7 +3459,7 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo, fo
3459
3459
  set.migrations.reverse();
3460
3460
  }
3461
3461
  }
3462
- if (!skipLock) await queryLock(trx);
3462
+ if (!skipLock) await queryLock(trx, config.migrationLockKey);
3463
3463
  let db;
3464
3464
  const beforeMigrate = config[up ? "beforeMigrate" : "beforeRollback"];
3465
3465
  if (beforeMigrate || config.beforeChange) {
@@ -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
  };