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.d.ts CHANGED
@@ -1777,6 +1777,11 @@ interface MigrateConfigBase extends QueryLogOptions {
1777
1777
  migrationsTable?: string;
1778
1778
  transaction?: 'single' | 'per-migration';
1779
1779
  transactionSearchPath?: SearchPath;
1780
+ /**
1781
+ * Advisory lock key used while migrating or rolling back.
1782
+ * Defaults to rake-db's fixed lock key.
1783
+ */
1784
+ migrationLockKey?: bigint;
1780
1785
  forceDefaultExports?: boolean;
1781
1786
  beforeChange?: ChangeCallback$1;
1782
1787
  afterChange?: ChangeCallback$1;
@@ -1899,6 +1904,11 @@ interface RakeDbCliConfigInputBase<SchemaConfig extends ColumnSchemaConfig, CT =
1899
1904
  };
1900
1905
  noPrimaryKey?: NoPrimaryKeyOption;
1901
1906
  transactionSearchPath?: SearchPath;
1907
+ /**
1908
+ * Advisory lock key used while migrating or rolling back.
1909
+ * Defaults to rake-db's fixed lock key.
1910
+ */
1911
+ migrationLockKey?: bigint;
1902
1912
  /**
1903
1913
  * Throw if a migration doesn't have a default export.
1904
1914
  * This is needed when in your setup you're importing migration files first and execute them later,
@@ -1998,6 +2008,7 @@ interface RakeDbConfig<ColumnTypes = unknown> extends QueryLogOptions {
1998
2008
  afterRollback?: MigrationCallback;
1999
2009
  migrationId: RakeDbMigrationId;
2000
2010
  transactionSearchPath?: SearchPath;
2011
+ migrationLockKey?: bigint;
2001
2012
  forceDefaultExports?: boolean;
2002
2013
  afterChangeCommit?: ChangeCommitCallback;
2003
2014
  basePath: string;
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ let node_url = require("node:url");
26
26
  let pqb = require("pqb");
27
27
  let node_fs_promises = require("node:fs/promises");
28
28
  node_fs_promises = __toESM(node_fs_promises);
29
- const RAKE_DB_LOCK_KEY = "8582141715823621641";
29
+ const RAKE_DB_LOCK_KEY = 8582141715823621641n;
30
30
  const getFirstWordAndRest = (input) => {
31
31
  const i = input.search(/(?=[A-Z])|[-_ ]/);
32
32
  if (i !== -1) {
@@ -88,7 +88,7 @@ const transaction = (adapter, config, fn) => {
88
88
  const searchPath = config.transactionSearchPath;
89
89
  return adapter.transaction(void 0, searchPath ? { setConfig: { search_path: typeof searchPath === "function" ? searchPath() : searchPath } } : void 0, fn);
90
90
  };
91
- const queryLock = (trx) => trx.query(`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`);
91
+ const queryLock = (trx, key = RAKE_DB_LOCK_KEY) => trx.query(`SELECT pg_advisory_xact_lock('${key}')`);
92
92
  const getCliParam = (args, name) => {
93
93
  if (args) {
94
94
  const key = "--" + name;
@@ -3482,7 +3482,7 @@ const migrateOrRollback = async (trx, config, set, versions, count, up, redo, fo
3482
3482
  set.migrations.reverse();
3483
3483
  }
3484
3484
  }
3485
- if (!skipLock) await queryLock(trx);
3485
+ if (!skipLock) await queryLock(trx, config.migrationLockKey);
3486
3486
  let db;
3487
3487
  const beforeMigrate = config[up ? "beforeMigrate" : "beforeRollback"];
3488
3488
  if (beforeMigrate || config.beforeChange) {
@@ -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
  };