@oliasoft-open-source/node-json-migrator 5.6.0-beta-1 → 5.6.1-beta-1

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.
Files changed (2) hide show
  1. package/dist/index.mjs +22 -14
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -4176,6 +4176,17 @@ const throwIfFilesNotFound = (migrationEntries) => {
4176
4176
  throw new Error("Migration files from plan.json are missing from filesystem");
4177
4177
  }
4178
4178
  };
4179
+ /** Throws if active migration files are missing from plan.json. */
4180
+ const throwIfFilesNotInPlan = (filePaths, migrationEntries) => {
4181
+ const missingFileNames = filePaths.map((filePath) => {
4182
+ return path.basename(filePath);
4183
+ }).filter((fileName) => {
4184
+ return !migrationEntries.some((entry) => {
4185
+ return entry.fileName === fileName;
4186
+ });
4187
+ });
4188
+ if (missingFileNames.length) throw new Error(`Migration files are missing from plan.json: ${missingFileNames.join(", ")}. Add them to plan.json and re-run the migrator.`);
4189
+ };
4179
4190
  //#endregion
4180
4191
  //#region src/database/database.ts
4181
4192
  const migrationTablesExist = (db, entity) => {
@@ -4333,7 +4344,10 @@ const updateRecord = async (t, entityTableName, entityColumnNames, entityId, cur
4333
4344
  $(versionColumn:name) = $(nextVersion)
4334
4345
  ${useEtag ? ", $(etagColumn:name) = $(etagColumn:name) + 1" : ""}
4335
4346
  WHERE $(idColumn:name) = $(entityId)
4336
- AND $(versionColumn:name) = $(currentVersion)
4347
+ AND (
4348
+ $(versionColumn:name) = $(currentVersion)
4349
+ OR ($(versionColumn:name) IS NULL AND $(currentVersion) IS NULL)
4350
+ )
4337
4351
  ${useEtag ? "AND $(etagColumn:name) = $(etag)" : ""}
4338
4352
  RETURNING *
4339
4353
  `;
@@ -4438,21 +4452,12 @@ const getPlannedMigrations = async ({ config }) => {
4438
4452
  if (!await migrationTablesExist(database, entity)) await createMigrationsTables(database, entity);
4439
4453
  }
4440
4454
  const rawPlan = await readPlan(directory);
4441
- const parsedPlan = parsePlan$1(rawPlan);
4442
- const plannedMigrations = parsedPlan.map((m) => ({
4455
+ const plannedMigrations = parsePlan$1(rawPlan).map((m) => ({
4443
4456
  fileHashFromPlan: m.fileHash,
4444
4457
  fileName: m.fileName,
4445
4458
  sequence: m.sequence
4446
4459
  }));
4447
4460
  const filePaths = await getMigrationFilePaths(directory);
4448
- const missingFileNames = filePaths.map((filePath) => {
4449
- return path.basename(filePath);
4450
- }).filter((fileName) => {
4451
- return !parsedPlan.some((entry) => {
4452
- return entry.fileName === fileName;
4453
- });
4454
- });
4455
- if (missingFileNames.length) throw new Error(`Migration files are missing from plan.json: ${missingFileNames.join(", ")}. Add them to plan.json and re-run the migrator.`);
4456
4461
  const skippedFilePaths = await getSkippedFilePaths(directory);
4457
4462
  const plannedMigrationsWithFileEntries = await Promise.all(plannedMigrations.map(async (m) => {
4458
4463
  const { fileName } = m;
@@ -4488,6 +4493,7 @@ const getPlannedMigrations = async ({ config }) => {
4488
4493
  sequenceFromHistory: historicalMigration?.sequence || null
4489
4494
  };
4490
4495
  });
4496
+ throwIfFilesNotInPlan(filePaths, plannedMigrationsWithHistory);
4491
4497
  throwIfFilesNotFound(plannedMigrationsWithHistory);
4492
4498
  throwIfFileNamesInvalid(plannedMigrationsWithHistory);
4493
4499
  throwIfFileNamesNotUnique(plannedMigrationsWithHistory);
@@ -4833,7 +4839,7 @@ const validateConcurrency = (concurrency) => {
4833
4839
  /**
4834
4840
  * Migrate an entity record in the database
4835
4841
  */
4836
- const migrateRecord = async ({ record, config, preparedPlan, loadHistoricalPlans, beforeMigrateRecord, afterMigrateRecord }) => {
4842
+ const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, beforeMigrateRecord, afterMigrateRecord }) => {
4837
4843
  const { dry } = config;
4838
4844
  console.log(chalk.gray(` ${record.name}`));
4839
4845
  try {
@@ -4859,7 +4865,7 @@ const migrateRecord = async ({ record, config, preparedPlan, loadHistoricalPlans
4859
4865
  });
4860
4866
  if (!dry) {
4861
4867
  const nextRecord = await updateRecord(transaction, config?.entityTableName, config?.entityColumnNames, record.id, currentVersion, nextData, nextVersion, etag);
4862
- if (!nextRecord) throw new Error("Failed to patch record with newest migration - The record was probably updated while migrating...");
4868
+ if (!nextRecord) throw new Error(`Failed to patch record with newest migration - The record was probably updated while migrating... Expected version=${JSON.stringify(currentVersion)}, etag=${JSON.stringify(etag)}.`);
4863
4869
  await afterMigrateRecord({
4864
4870
  currentRecord,
4865
4871
  nextRecord,
@@ -4877,7 +4883,8 @@ const migrateRecord = async ({ record, config, preparedPlan, loadHistoricalPlans
4877
4883
  error,
4878
4884
  stackTrace: error?.stack
4879
4885
  };
4880
- console.log(chalk.red(` ${capitalize(config.entity)} migration with id ${record.id} failed:\n ${error?.stack}`));
4886
+ const retryStatus = attempt < MAX_ATTEMPTS ? "retrying" : "no retries left";
4887
+ console.log(chalk.red(` ${capitalize(config.entity)} migration with id ${record.id} failed: attempt ${attempt}/${MAX_ATTEMPTS}; ${retryStatus}.\n ${error?.stack}`));
4881
4888
  return migrationErrorPayload;
4882
4889
  }
4883
4890
  };
@@ -4904,6 +4911,7 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
4904
4911
  if (attempt > 1) console.log(chalk.gray(`Retrying migration of ${record.name} (attempt ${attempt}/${MAX_ATTEMPTS}).`));
4905
4912
  const migrationErrorPayload = await migrateRecord({
4906
4913
  record,
4914
+ attempt,
4907
4915
  config,
4908
4916
  preparedPlan,
4909
4917
  loadHistoricalPlans,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/node-json-migrator",
3
- "version": "5.6.0-beta-1",
3
+ "version": "5.6.1-beta-1",
4
4
  "description": "A library for JSON migrations",
5
5
  "homepage": "https://oliasoft-open-source.gitlab.io/node-json-migrator",
6
6
  "bugs": {