@oliasoft-open-source/node-json-migrator 5.6.0 → 5.6.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 +17 -5
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -4344,7 +4344,15 @@ const updateRecord = async (t, entityTableName, entityColumnNames, entityId, cur
4344
4344
  $(versionColumn:name) = $(nextVersion)
4345
4345
  ${useEtag ? ", $(etagColumn:name) = $(etagColumn:name) + 1" : ""}
4346
4346
  WHERE $(idColumn:name) = $(entityId)
4347
- AND $(versionColumn:name) = $(currentVersion)
4347
+ -- OW-28171: Only update if the version has not changed since we read the record.
4348
+ -- In SQL, version = NULL is never true, so two NULL versions need an explicit check.
4349
+ -- IS NOT DISTINCT FROM provides null-safe equality, but pg-mem does not support it.
4350
+ -- The condition below selects the same rows in WHERE.
4351
+ -- https://www.postgresql.org/docs/current/functions-comparison.html
4352
+ AND (
4353
+ $(versionColumn:name) = $(currentVersion)
4354
+ OR ($(versionColumn:name) IS NULL AND $(currentVersion) IS NULL)
4355
+ )
4348
4356
  ${useEtag ? "AND $(etagColumn:name) = $(etag)" : ""}
4349
4357
  RETURNING *
4350
4358
  `;
@@ -4836,7 +4844,7 @@ const validateConcurrency = (concurrency) => {
4836
4844
  /**
4837
4845
  * Migrate an entity record in the database
4838
4846
  */
4839
- const migrateRecord = async ({ record, config, preparedPlan, loadHistoricalPlans, beforeMigrateRecord, afterMigrateRecord }) => {
4847
+ const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, beforeMigrateRecord, afterMigrateRecord }) => {
4840
4848
  const { dry } = config;
4841
4849
  console.log(chalk.gray(` ${record.name}`));
4842
4850
  try {
@@ -4862,7 +4870,10 @@ const migrateRecord = async ({ record, config, preparedPlan, loadHistoricalPlans
4862
4870
  });
4863
4871
  if (!dry) {
4864
4872
  const nextRecord = await updateRecord(transaction, config?.entityTableName, config?.entityColumnNames, record.id, currentVersion, nextData, nextVersion, etag);
4865
- if (!nextRecord) throw new Error("Failed to patch record with newest migration - The record was probably updated while migrating...");
4873
+ if (!nextRecord) {
4874
+ const expectedEtag = typeof etag === "number" ? `, etag=${JSON.stringify(etag)}` : "";
4875
+ throw new Error(`Failed to patch record with newest migration - The record was probably updated while migrating... Expected version=${JSON.stringify(currentVersion)}${expectedEtag}.`);
4876
+ }
4866
4877
  await afterMigrateRecord({
4867
4878
  currentRecord,
4868
4879
  nextRecord,
@@ -4880,7 +4891,8 @@ const migrateRecord = async ({ record, config, preparedPlan, loadHistoricalPlans
4880
4891
  error,
4881
4892
  stackTrace: error?.stack
4882
4893
  };
4883
- console.log(chalk.red(` ${capitalize(config.entity)} migration with id ${record.id} failed:\n ${error?.stack}`));
4894
+ const retryStatus = attempt < MAX_ATTEMPTS ? "retrying" : "no retries left";
4895
+ console.log(chalk.red(` ${capitalize(config.entity)} migration with id ${record.id} failed: attempt ${attempt}/${MAX_ATTEMPTS}; ${retryStatus}.\n ${error?.stack}`));
4884
4896
  return migrationErrorPayload;
4885
4897
  }
4886
4898
  };
@@ -4904,9 +4916,9 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
4904
4916
  let attempt = 0;
4905
4917
  while (attempt < MAX_ATTEMPTS) {
4906
4918
  attempt += 1;
4907
- if (attempt > 1) console.log(chalk.gray(`Retrying migration of ${record.name} (attempt ${attempt}/${MAX_ATTEMPTS}).`));
4908
4919
  const migrationErrorPayload = await migrateRecord({
4909
4920
  record,
4921
+ attempt,
4910
4922
  config,
4911
4923
  preparedPlan,
4912
4924
  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",
3
+ "version": "5.6.1",
4
4
  "description": "A library for JSON migrations",
5
5
  "homepage": "https://oliasoft-open-source.gitlab.io/node-json-migrator",
6
6
  "bugs": {