@oliasoft-open-source/node-json-migrator 5.6.1 → 5.6.3-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 +72 -37
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -15,6 +15,16 @@ import os from "node:os";
15
15
  import tty from "node:tty";
16
16
  import process$2 from "process";
17
17
  import { execSync, spawn } from "child_process";
18
+ //#region node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/array/compact.mjs
19
+ function compact(arr) {
20
+ const result = [];
21
+ for (let i = 0; i < arr.length; i++) {
22
+ const item = arr[i];
23
+ if (item) result.push(item);
24
+ }
25
+ return result;
26
+ }
27
+ //#endregion
18
28
  //#region node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/array/difference.mjs
19
29
  function difference(firstArr, secondArr) {
20
30
  const secondSet = new Set(secondArr);
@@ -4370,6 +4380,7 @@ const updateRecord = async (t, entityTableName, entityColumnNames, entityId, cur
4370
4380
  };
4371
4381
  return t.oneOrNone(query, params);
4372
4382
  };
4383
+ const DatabaseModule = { getPlanFromVersion };
4373
4384
  //#endregion
4374
4385
  //#region src/plan/plan.ts
4375
4386
  /**
@@ -4416,17 +4427,20 @@ const readPlan = async (directory) => {
4416
4427
  }
4417
4428
  };
4418
4429
  /**
4419
- * Parse the plan.json string into a sorted object
4430
+ * Parse and validate a plan without changing its order
4420
4431
  */
4421
- const parsePlan$1 = (plan) => {
4432
+ const parseAndValidatePlan = (plan, source) => {
4422
4433
  let plannedMigrations;
4423
4434
  try {
4424
4435
  plannedMigrations = JSON.parse(plan);
4425
4436
  } catch {
4426
- throw new Error("Invalid JSON for migrator plan.js (unable to parse file)");
4437
+ throw new Error(`Invalid JSON for ${source} (unable to parse)`);
4427
4438
  }
4428
- if (!validatePlan(plannedMigrations)) throw new Error("Invalid JSON for migrator plan.js (does not match schema)");
4429
- return sortPlanEntries(plannedMigrations);
4439
+ if (!validatePlan(plannedMigrations)) throw new Error(`Invalid JSON for ${source} (does not match schema)`);
4440
+ return plannedMigrations;
4441
+ };
4442
+ const parsePlan$1 = (plan) => {
4443
+ return sortPlanEntries(parseAndValidatePlan(plan, "migrator plan.json"));
4430
4444
  };
4431
4445
  /**
4432
4446
  * Write the plan.json file
@@ -4727,19 +4741,26 @@ const getPlanRevisionsFromGit = async (planFilePath, terminal) => {
4727
4741
  const getPlanFromGit = async (planFilePath, revision, terminal) => {
4728
4742
  return exec(`git show ${revision}:${planFilePath}`, terminal);
4729
4743
  };
4744
+ const parseHistoricalPlan = (rawPlan, revision) => {
4745
+ try {
4746
+ const plan = formatJSON(rawPlan);
4747
+ return {
4748
+ revision,
4749
+ version: hash(plan),
4750
+ plan
4751
+ };
4752
+ } catch {
4753
+ return null;
4754
+ }
4755
+ };
4730
4756
  const getHistoricalPlansFromGit = async (planFilePath) => {
4731
4757
  try {
4732
4758
  if (!gitAvailable()) throw new Error("git must be installed and in PATH");
4733
4759
  const terminal = getTerminal();
4734
4760
  const revisions = await getPlanRevisionsFromGit(planFilePath, terminal);
4735
- return { historicalPlans: await Promise.all(revisions.map(async (revision) => {
4736
- const plan = await getPlanFromGit(planFilePath, revision.version, terminal);
4737
- return {
4738
- revision,
4739
- version: hash(plan),
4740
- plan
4741
- };
4742
- })) };
4761
+ return { historicalPlans: compact(await Promise.all(revisions.map(async (revision) => {
4762
+ return parseHistoricalPlan(await getPlanFromGit(planFilePath, revision.version, terminal), revision);
4763
+ }))) };
4743
4764
  } catch (error) {
4744
4765
  console.error(chalk.red(error));
4745
4766
  throw new Error("Unable to fetch plan.json history from git");
@@ -4748,32 +4769,32 @@ const getHistoricalPlansFromGit = async (planFilePath) => {
4748
4769
  const GitModule = { getHistoricalPlansFromGit };
4749
4770
  //#endregion
4750
4771
  //#region src/executed/executed.ts
4772
+ const readPlanFromDatabase = async ({ database, entity, version }) => {
4773
+ const plan = await DatabaseModule.getPlanFromVersion(database, entity, version);
4774
+ return plan === null ? null : parseAndValidatePlan(plan, `${entity}_versions table`);
4775
+ };
4751
4776
  /**
4752
4777
  * Get the executed migrations for a given payload version
4753
4778
  */
4754
- const getExecutedMigrationsFromVersion = async (database, pgpHelpers, entity, directory, currentVersion, plannedVersion, plannedMigrations, loadHistoricalPlans = getHistoricalPlansFromGit) => {
4779
+ const getExecutedMigrationsFromVersion = async (database, pgpHelpers, entity, directory, currentVersion, plannedVersion, plannedMigrations, loadHistoricalPlans = getHistoricalPlansFromGit, loadExecutedMigrations = readPlanFromDatabase) => {
4755
4780
  if (!currentVersion) return [];
4756
4781
  if (currentVersion === plannedVersion) return plannedMigrations;
4757
4782
  else {
4758
- let planFromVersion = await getPlanFromVersion(database, entity, currentVersion);
4759
- if (planFromVersion === null) {
4760
- console.warn(chalk.yellow(`Version not found in ${entity}_version table, trying git history instead`));
4761
- const { historicalPlans } = await loadHistoricalPlans(`${directory}/plan.json`);
4762
- if (historicalPlans.length) {
4763
- await insertVersions(database, pgpHelpers, entity, historicalPlans);
4764
- planFromVersion = historicalPlans.find((p) => p.version === currentVersion)?.plan || null;
4765
- }
4783
+ const migrations = await loadExecutedMigrations({
4784
+ database,
4785
+ entity,
4786
+ version: currentVersion
4787
+ });
4788
+ if (migrations !== null) return migrations;
4789
+ let planFromVersion = null;
4790
+ console.warn(chalk.yellow(`Version not found in ${entity}_version table, trying git history instead`));
4791
+ const { historicalPlans } = await loadHistoricalPlans(`${directory}/plan.json`);
4792
+ if (historicalPlans.length) {
4793
+ await insertVersions(database, pgpHelpers, entity, historicalPlans);
4794
+ planFromVersion = historicalPlans.find((p) => p.version === currentVersion)?.plan || null;
4766
4795
  }
4767
- if (planFromVersion !== null) {
4768
- let migrationsFromHistory = null;
4769
- try {
4770
- migrationsFromHistory = JSON.parse(planFromVersion);
4771
- } catch {
4772
- throw new Error(`Invalid record in ${entity}_version table (cannot parse JSON)`);
4773
- }
4774
- if (!validatePlan(migrationsFromHistory)) throw new Error(`Invalid plan in ${entity}_version table (does not match schema)`);
4775
- else return migrationsFromHistory;
4776
- } else {
4796
+ if (planFromVersion !== null) return parseAndValidatePlan(planFromVersion, `${entity}_versions table`);
4797
+ else {
4777
4798
  console.error(chalk.red(historyError));
4778
4799
  throw new Error(`Unable to migrate, could not find version history`);
4779
4800
  }
@@ -4796,11 +4817,11 @@ const getPendingMigrations = ({ plannedMigrations, executedMigrations, printPend
4796
4817
  migrator: m.migrator
4797
4818
  }));
4798
4819
  };
4799
- const getPendingMigrators = async ({ config, preparedPlan, transaction, loadHistoricalPlans }) => {
4820
+ const getPendingMigrators = async ({ config, preparedPlan, transaction, loadHistoricalPlans, loadExecutedMigrations }) => {
4800
4821
  const { directory, database, pgpHelpers, entity, version, printPendingFileNames, force } = config;
4801
4822
  const { plannedMigrations, nextVersion } = preparedPlan ?? await PlanModule.getPlannedMigrations({ config });
4802
4823
  const connection = transaction ?? database;
4803
- const executedMigrations = connection ? await getExecutedMigrationsFromVersion(connection, pgpHelpers, entity, directory, version, nextVersion, plannedMigrations, loadHistoricalPlans) : [];
4824
+ const executedMigrations = connection ? await getExecutedMigrationsFromVersion(connection, pgpHelpers, entity, directory, version, nextVersion, plannedMigrations, loadHistoricalPlans, loadExecutedMigrations) : [];
4804
4825
  return {
4805
4826
  pendingMigrators: getPendingMigrations({
4806
4827
  plannedMigrations,
@@ -4823,12 +4844,13 @@ const migrate = async ({ payload, config, metaData }) => {
4823
4844
  metaData
4824
4845
  });
4825
4846
  };
4826
- const migrateWithPlan = async ({ payload, config, metaData, preparedPlan, transaction, loadHistoricalPlans }) => {
4847
+ const migrateWithPlan = async ({ payload, config, metaData, preparedPlan, transaction, loadHistoricalPlans, loadExecutedMigrations }) => {
4827
4848
  const { pendingMigrators, nextVersion } = await getPendingMigrators({
4828
4849
  config,
4829
4850
  preparedPlan,
4830
4851
  transaction,
4831
- loadHistoricalPlans
4852
+ loadHistoricalPlans,
4853
+ loadExecutedMigrations
4832
4854
  });
4833
4855
  return {
4834
4856
  nextPayload: pendingMigrators.length ? PipeModule.pipe(pendingMigrators, payload, metaData) : payload,
@@ -4844,7 +4866,7 @@ const validateConcurrency = (concurrency) => {
4844
4866
  /**
4845
4867
  * Migrate an entity record in the database
4846
4868
  */
4847
- const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, beforeMigrateRecord, afterMigrateRecord }) => {
4869
+ const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, loadExecutedMigrations, beforeMigrateRecord, afterMigrateRecord }) => {
4848
4870
  const { dry } = config;
4849
4871
  console.log(chalk.gray(` ${record.name}`));
4850
4872
  try {
@@ -4861,6 +4883,7 @@ const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistor
4861
4883
  transaction,
4862
4884
  preparedPlan,
4863
4885
  loadHistoricalPlans,
4886
+ loadExecutedMigrations,
4864
4887
  config: {
4865
4888
  ...config,
4866
4889
  version: currentVersion,
@@ -4900,6 +4923,17 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
4900
4923
  const { dry, concurrency = 1 } = config;
4901
4924
  validateConcurrency(concurrency);
4902
4925
  const migrationErrors = [];
4926
+ const loadExecutedMigrations = memoize(async (options) => {
4927
+ const { version } = options;
4928
+ try {
4929
+ const migrations = await readPlanFromDatabase(options);
4930
+ if (migrations === null) loadExecutedMigrations.cache.delete(version);
4931
+ return migrations;
4932
+ } catch (error) {
4933
+ loadExecutedMigrations.cache.delete(version);
4934
+ throw error;
4935
+ }
4936
+ }, { getCacheKey: ({ version }) => version });
4903
4937
  const loadHistoricalPlans = memoize(async (planFilePath) => {
4904
4938
  try {
4905
4939
  return await GitModule.getHistoricalPlansFromGit(planFilePath);
@@ -4922,6 +4956,7 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
4922
4956
  config,
4923
4957
  preparedPlan,
4924
4958
  loadHistoricalPlans,
4959
+ loadExecutedMigrations,
4925
4960
  beforeMigrateRecord,
4926
4961
  afterMigrateRecord
4927
4962
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/node-json-migrator",
3
- "version": "5.6.1",
3
+ "version": "5.6.3-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": {