@forklaunch/core 4.2.0 → 4.3.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.
@@ -2107,6 +2107,43 @@ function wrapEmWithTenantContext(em, tenantId) {
2107
2107
  function rebind(result, target, proxy) {
2108
2108
  return result === target ? proxy : result;
2109
2109
  }
2110
+
2111
+ // src/persistence/migrationLock.ts
2112
+ async function runMigrationsWithLock(orm, options = {}) {
2113
+ const run = options.run ?? (() => {
2114
+ const migrator = orm.migrator ?? orm.getMigrator?.();
2115
+ if (!migrator) {
2116
+ throw new Error(
2117
+ "runMigrationsWithLock: the ORM has no Migrator extension; add `extensions: [Migrator]` to the config"
2118
+ );
2119
+ }
2120
+ return migrator.up();
2121
+ });
2122
+ const log = options.log ?? (() => void 0);
2123
+ const isPostgres = orm.em.getPlatform().getDefaultSchemaName?.() === "public";
2124
+ if (!isPostgres) {
2125
+ await run();
2126
+ return;
2127
+ }
2128
+ const connection = orm.em.getConnection();
2129
+ await connection.transactional(async (trx) => {
2130
+ const name = options.lockName ?? `${(await connection.execute(
2131
+ "select current_database() as name",
2132
+ [],
2133
+ "get",
2134
+ trx
2135
+ )).name}:migrations`;
2136
+ log(`[migrations] waiting for lock ${name}`);
2137
+ await connection.execute(
2138
+ "select pg_advisory_xact_lock(hashtext(?))",
2139
+ [name],
2140
+ "run",
2141
+ trx
2142
+ );
2143
+ log(`[migrations] holding lock ${name}`);
2144
+ await run();
2145
+ });
2146
+ }
2110
2147
  export {
2111
2148
  Anon,
2112
2149
  BLIND_INDEX_PREFIX,
@@ -2166,6 +2203,7 @@ export {
2166
2203
  reencryptEncryptedColumns,
2167
2204
  registerEncryptor,
2168
2205
  rotationTotals,
2206
+ runMigrationsWithLock,
2169
2207
  sealedAnon,
2170
2208
  setEncryptionTenantId,
2171
2209
  setupRls,