@oliasoft-open-source/node-json-migrator 5.6.1 → 5.6.2
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.mjs +47 -29
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -4370,6 +4370,7 @@ const updateRecord = async (t, entityTableName, entityColumnNames, entityId, cur
|
|
|
4370
4370
|
};
|
|
4371
4371
|
return t.oneOrNone(query, params);
|
|
4372
4372
|
};
|
|
4373
|
+
const DatabaseModule = { getPlanFromVersion };
|
|
4373
4374
|
//#endregion
|
|
4374
4375
|
//#region src/plan/plan.ts
|
|
4375
4376
|
/**
|
|
@@ -4416,17 +4417,20 @@ const readPlan = async (directory) => {
|
|
|
4416
4417
|
}
|
|
4417
4418
|
};
|
|
4418
4419
|
/**
|
|
4419
|
-
* Parse
|
|
4420
|
+
* Parse and validate a plan without changing its order
|
|
4420
4421
|
*/
|
|
4421
|
-
const
|
|
4422
|
+
const parseAndValidatePlan = (plan, source) => {
|
|
4422
4423
|
let plannedMigrations;
|
|
4423
4424
|
try {
|
|
4424
4425
|
plannedMigrations = JSON.parse(plan);
|
|
4425
4426
|
} catch {
|
|
4426
|
-
throw new Error(
|
|
4427
|
+
throw new Error(`Invalid JSON for ${source} (unable to parse)`);
|
|
4427
4428
|
}
|
|
4428
|
-
if (!validatePlan(plannedMigrations)) throw new Error(
|
|
4429
|
-
return
|
|
4429
|
+
if (!validatePlan(plannedMigrations)) throw new Error(`Invalid JSON for ${source} (does not match schema)`);
|
|
4430
|
+
return plannedMigrations;
|
|
4431
|
+
};
|
|
4432
|
+
const parsePlan$1 = (plan) => {
|
|
4433
|
+
return sortPlanEntries(parseAndValidatePlan(plan, "migrator plan.json"));
|
|
4430
4434
|
};
|
|
4431
4435
|
/**
|
|
4432
4436
|
* Write the plan.json file
|
|
@@ -4748,32 +4752,32 @@ const getHistoricalPlansFromGit = async (planFilePath) => {
|
|
|
4748
4752
|
const GitModule = { getHistoricalPlansFromGit };
|
|
4749
4753
|
//#endregion
|
|
4750
4754
|
//#region src/executed/executed.ts
|
|
4755
|
+
const readPlanFromDatabase = async ({ database, entity, version }) => {
|
|
4756
|
+
const plan = await DatabaseModule.getPlanFromVersion(database, entity, version);
|
|
4757
|
+
return plan === null ? null : parseAndValidatePlan(plan, `${entity}_versions table`);
|
|
4758
|
+
};
|
|
4751
4759
|
/**
|
|
4752
4760
|
* Get the executed migrations for a given payload version
|
|
4753
4761
|
*/
|
|
4754
|
-
const getExecutedMigrationsFromVersion = async (database, pgpHelpers, entity, directory, currentVersion, plannedVersion, plannedMigrations, loadHistoricalPlans = getHistoricalPlansFromGit) => {
|
|
4762
|
+
const getExecutedMigrationsFromVersion = async (database, pgpHelpers, entity, directory, currentVersion, plannedVersion, plannedMigrations, loadHistoricalPlans = getHistoricalPlansFromGit, loadExecutedMigrations = readPlanFromDatabase) => {
|
|
4755
4763
|
if (!currentVersion) return [];
|
|
4756
4764
|
if (currentVersion === plannedVersion) return plannedMigrations;
|
|
4757
4765
|
else {
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
|
|
4766
|
+
const migrations = await loadExecutedMigrations({
|
|
4767
|
+
database,
|
|
4768
|
+
entity,
|
|
4769
|
+
version: currentVersion
|
|
4770
|
+
});
|
|
4771
|
+
if (migrations !== null) return migrations;
|
|
4772
|
+
let planFromVersion = null;
|
|
4773
|
+
console.warn(chalk.yellow(`Version not found in ${entity}_version table, trying git history instead`));
|
|
4774
|
+
const { historicalPlans } = await loadHistoricalPlans(`${directory}/plan.json`);
|
|
4775
|
+
if (historicalPlans.length) {
|
|
4776
|
+
await insertVersions(database, pgpHelpers, entity, historicalPlans);
|
|
4777
|
+
planFromVersion = historicalPlans.find((p) => p.version === currentVersion)?.plan || null;
|
|
4766
4778
|
}
|
|
4767
|
-
if (planFromVersion !== null) {
|
|
4768
|
-
|
|
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 {
|
|
4779
|
+
if (planFromVersion !== null) return parseAndValidatePlan(planFromVersion, `${entity}_versions table`);
|
|
4780
|
+
else {
|
|
4777
4781
|
console.error(chalk.red(historyError));
|
|
4778
4782
|
throw new Error(`Unable to migrate, could not find version history`);
|
|
4779
4783
|
}
|
|
@@ -4796,11 +4800,11 @@ const getPendingMigrations = ({ plannedMigrations, executedMigrations, printPend
|
|
|
4796
4800
|
migrator: m.migrator
|
|
4797
4801
|
}));
|
|
4798
4802
|
};
|
|
4799
|
-
const getPendingMigrators = async ({ config, preparedPlan, transaction, loadHistoricalPlans }) => {
|
|
4803
|
+
const getPendingMigrators = async ({ config, preparedPlan, transaction, loadHistoricalPlans, loadExecutedMigrations }) => {
|
|
4800
4804
|
const { directory, database, pgpHelpers, entity, version, printPendingFileNames, force } = config;
|
|
4801
4805
|
const { plannedMigrations, nextVersion } = preparedPlan ?? await PlanModule.getPlannedMigrations({ config });
|
|
4802
4806
|
const connection = transaction ?? database;
|
|
4803
|
-
const executedMigrations = connection ? await getExecutedMigrationsFromVersion(connection, pgpHelpers, entity, directory, version, nextVersion, plannedMigrations, loadHistoricalPlans) : [];
|
|
4807
|
+
const executedMigrations = connection ? await getExecutedMigrationsFromVersion(connection, pgpHelpers, entity, directory, version, nextVersion, plannedMigrations, loadHistoricalPlans, loadExecutedMigrations) : [];
|
|
4804
4808
|
return {
|
|
4805
4809
|
pendingMigrators: getPendingMigrations({
|
|
4806
4810
|
plannedMigrations,
|
|
@@ -4823,12 +4827,13 @@ const migrate = async ({ payload, config, metaData }) => {
|
|
|
4823
4827
|
metaData
|
|
4824
4828
|
});
|
|
4825
4829
|
};
|
|
4826
|
-
const migrateWithPlan = async ({ payload, config, metaData, preparedPlan, transaction, loadHistoricalPlans }) => {
|
|
4830
|
+
const migrateWithPlan = async ({ payload, config, metaData, preparedPlan, transaction, loadHistoricalPlans, loadExecutedMigrations }) => {
|
|
4827
4831
|
const { pendingMigrators, nextVersion } = await getPendingMigrators({
|
|
4828
4832
|
config,
|
|
4829
4833
|
preparedPlan,
|
|
4830
4834
|
transaction,
|
|
4831
|
-
loadHistoricalPlans
|
|
4835
|
+
loadHistoricalPlans,
|
|
4836
|
+
loadExecutedMigrations
|
|
4832
4837
|
});
|
|
4833
4838
|
return {
|
|
4834
4839
|
nextPayload: pendingMigrators.length ? PipeModule.pipe(pendingMigrators, payload, metaData) : payload,
|
|
@@ -4844,7 +4849,7 @@ const validateConcurrency = (concurrency) => {
|
|
|
4844
4849
|
/**
|
|
4845
4850
|
* Migrate an entity record in the database
|
|
4846
4851
|
*/
|
|
4847
|
-
const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, beforeMigrateRecord, afterMigrateRecord }) => {
|
|
4852
|
+
const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, loadExecutedMigrations, beforeMigrateRecord, afterMigrateRecord }) => {
|
|
4848
4853
|
const { dry } = config;
|
|
4849
4854
|
console.log(chalk.gray(` ${record.name}`));
|
|
4850
4855
|
try {
|
|
@@ -4861,6 +4866,7 @@ const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistor
|
|
|
4861
4866
|
transaction,
|
|
4862
4867
|
preparedPlan,
|
|
4863
4868
|
loadHistoricalPlans,
|
|
4869
|
+
loadExecutedMigrations,
|
|
4864
4870
|
config: {
|
|
4865
4871
|
...config,
|
|
4866
4872
|
version: currentVersion,
|
|
@@ -4900,6 +4906,17 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
|
|
|
4900
4906
|
const { dry, concurrency = 1 } = config;
|
|
4901
4907
|
validateConcurrency(concurrency);
|
|
4902
4908
|
const migrationErrors = [];
|
|
4909
|
+
const loadExecutedMigrations = memoize(async (options) => {
|
|
4910
|
+
const { version } = options;
|
|
4911
|
+
try {
|
|
4912
|
+
const migrations = await readPlanFromDatabase(options);
|
|
4913
|
+
if (migrations === null) loadExecutedMigrations.cache.delete(version);
|
|
4914
|
+
return migrations;
|
|
4915
|
+
} catch (error) {
|
|
4916
|
+
loadExecutedMigrations.cache.delete(version);
|
|
4917
|
+
throw error;
|
|
4918
|
+
}
|
|
4919
|
+
}, { getCacheKey: ({ version }) => version });
|
|
4903
4920
|
const loadHistoricalPlans = memoize(async (planFilePath) => {
|
|
4904
4921
|
try {
|
|
4905
4922
|
return await GitModule.getHistoricalPlansFromGit(planFilePath);
|
|
@@ -4922,6 +4939,7 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
|
|
|
4922
4939
|
config,
|
|
4923
4940
|
preparedPlan,
|
|
4924
4941
|
loadHistoricalPlans,
|
|
4942
|
+
loadExecutedMigrations,
|
|
4925
4943
|
beforeMigrateRecord,
|
|
4926
4944
|
afterMigrateRecord
|
|
4927
4945
|
});
|