rake-db 2.13.0 → 2.14.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.
package/dist/index.d.ts CHANGED
@@ -1060,7 +1060,7 @@ declare const rollback: MigrateFn;
1060
1060
  * Takes the same options as {@link migrate}.
1061
1061
  */
1062
1062
  declare const redo: MigrateFn;
1063
- declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, RakeDbColumnTypes>, files: MigrationItem[], count: number, asts: RakeDbAst[], up: boolean) => Promise<void>;
1063
+ declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, RakeDbColumnTypes>, files: MigrationItem[], count: number, asts: RakeDbAst[], up: boolean, skipLock?: boolean) => Promise<void>;
1064
1064
  declare const changeCache: Record<string, ChangeCallback<RakeDbColumnTypes>[] | undefined>;
1065
1065
 
1066
1066
  /**
@@ -1103,6 +1103,8 @@ declare const rakeDb: RakeDbFn;
1103
1103
 
1104
1104
  declare const saveMigratedVersion: <SchemaConfig extends ColumnSchemaConfig, CT>(db: SilentQueries, version: string, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
1105
1105
  declare const removeMigratedVersion: <SchemaConfig extends ColumnSchemaConfig, CT>(db: SilentQueries, version: string, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
1106
+ declare class NoMigrationsTableError extends Error {
1107
+ }
1106
1108
  declare const getMigratedVersionsMap: <SchemaConfig extends ColumnSchemaConfig, CT>(db: TransactionAdapter, config: RakeDbConfig<SchemaConfig, CT>) => Promise<Record<string, boolean>>;
1107
1109
 
1108
- export { AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DbMigration, DropMode, Migration, MigrationColumnTypes, RAKE_DB_LOCK_KEY, RakeDbAst, RakeDbConfig, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
1110
+ export { AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DbMigration, DropMode, Migration, MigrationColumnTypes, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAst, RakeDbConfig, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
package/dist/index.js CHANGED
@@ -2205,6 +2205,8 @@ const removeMigratedVersion = async (db, version, config) => {
2205
2205
  })} WHERE version = '${version}'`
2206
2206
  );
2207
2207
  };
2208
+ class NoMigrationsTableError extends Error {
2209
+ }
2208
2210
  const getMigratedVersionsMap = async (db, config) => {
2209
2211
  try {
2210
2212
  const result = await db.arrays(
@@ -2213,10 +2215,10 @@ const getMigratedVersionsMap = async (db, config) => {
2213
2215
  return Object.fromEntries(result.rows.map((row) => [row[0], true]));
2214
2216
  } catch (err) {
2215
2217
  if (err.code === "42P01") {
2216
- await createSchemaMigrations(db, config);
2217
- return {};
2218
+ throw new NoMigrationsTableError();
2219
+ } else {
2220
+ throw err;
2218
2221
  }
2219
- throw err;
2220
2222
  }
2221
2223
  };
2222
2224
 
@@ -2251,9 +2253,6 @@ function makeMigrateFn(defaultCount, up, fn) {
2251
2253
  const adapter = new pqb.Adapter(opts);
2252
2254
  try {
2253
2255
  await adapter.transaction(begin, async (trx) => {
2254
- await trx.query(
2255
- `SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`
2256
- );
2257
2256
  await fn(
2258
2257
  trx,
2259
2258
  conf,
@@ -2262,6 +2261,16 @@ function makeMigrateFn(defaultCount, up, fn) {
2262
2261
  localAsts
2263
2262
  );
2264
2263
  });
2264
+ } catch (err) {
2265
+ if (err instanceof NoMigrationsTableError) {
2266
+ await adapter.transaction(begin, async (trx) => {
2267
+ const config2 = conf;
2268
+ await createSchemaMigrations(trx, config2);
2269
+ await fn(trx, config2, files, count != null ? count : defaultCount, localAsts);
2270
+ });
2271
+ } else {
2272
+ throw err;
2273
+ }
2265
2274
  } finally {
2266
2275
  await adapter.close();
2267
2276
  }
@@ -2292,7 +2301,7 @@ const redo = makeMigrateFn(
2292
2301
  async (trx, config, files, count, asts) => {
2293
2302
  await migrateOrRollback(trx, config, files, count, asts, false);
2294
2303
  files.reverse();
2295
- await migrateOrRollback(trx, config, files, count, asts, true);
2304
+ await migrateOrRollback(trx, config, files, count, asts, true, true);
2296
2305
  files.reverse();
2297
2306
  }
2298
2307
  );
@@ -2312,8 +2321,11 @@ function prepareConfig(config, args, count) {
2312
2321
  delete config.appCodeUpdater;
2313
2322
  return config;
2314
2323
  }
2315
- const migrateOrRollback = async (trx, config, files, count, asts, up) => {
2324
+ const migrateOrRollback = async (trx, config, files, count, asts, up, skipLock) => {
2316
2325
  var _a, _b, _c;
2326
+ if (!skipLock) {
2327
+ await trx.query(`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`);
2328
+ }
2317
2329
  let db;
2318
2330
  await ((_a = config[up ? "beforeMigrate" : "beforeRollback"]) == null ? void 0 : _a.call(config, db != null ? db : db = getDb(trx)));
2319
2331
  const migratedVersions = await getMigratedVersionsMap(trx, config);
@@ -3908,6 +3920,7 @@ Migrate and rollback common arguments:
3908
3920
  `;
3909
3921
 
3910
3922
  exports.Migration = Migration;
3923
+ exports.NoMigrationsTableError = NoMigrationsTableError;
3911
3924
  exports.RAKE_DB_LOCK_KEY = RAKE_DB_LOCK_KEY;
3912
3925
  exports.changeCache = changeCache;
3913
3926
  exports.createDb = createDb;