@oliasoft-open-source/node-json-migrator 5.6.1-beta-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 +56 -31
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -4344,6 +4344,11 @@ 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
|
+
-- 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
|
|
4347
4352
|
AND (
|
|
4348
4353
|
$(versionColumn:name) = $(currentVersion)
|
|
4349
4354
|
OR ($(versionColumn:name) IS NULL AND $(currentVersion) IS NULL)
|
|
@@ -4365,6 +4370,7 @@ const updateRecord = async (t, entityTableName, entityColumnNames, entityId, cur
|
|
|
4365
4370
|
};
|
|
4366
4371
|
return t.oneOrNone(query, params);
|
|
4367
4372
|
};
|
|
4373
|
+
const DatabaseModule = { getPlanFromVersion };
|
|
4368
4374
|
//#endregion
|
|
4369
4375
|
//#region src/plan/plan.ts
|
|
4370
4376
|
/**
|
|
@@ -4411,17 +4417,20 @@ const readPlan = async (directory) => {
|
|
|
4411
4417
|
}
|
|
4412
4418
|
};
|
|
4413
4419
|
/**
|
|
4414
|
-
* Parse
|
|
4420
|
+
* Parse and validate a plan without changing its order
|
|
4415
4421
|
*/
|
|
4416
|
-
const
|
|
4422
|
+
const parseAndValidatePlan = (plan, source) => {
|
|
4417
4423
|
let plannedMigrations;
|
|
4418
4424
|
try {
|
|
4419
4425
|
plannedMigrations = JSON.parse(plan);
|
|
4420
4426
|
} catch {
|
|
4421
|
-
throw new Error(
|
|
4427
|
+
throw new Error(`Invalid JSON for ${source} (unable to parse)`);
|
|
4422
4428
|
}
|
|
4423
|
-
if (!validatePlan(plannedMigrations)) throw new Error(
|
|
4424
|
-
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"));
|
|
4425
4434
|
};
|
|
4426
4435
|
/**
|
|
4427
4436
|
* Write the plan.json file
|
|
@@ -4743,32 +4752,32 @@ const getHistoricalPlansFromGit = async (planFilePath) => {
|
|
|
4743
4752
|
const GitModule = { getHistoricalPlansFromGit };
|
|
4744
4753
|
//#endregion
|
|
4745
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
|
+
};
|
|
4746
4759
|
/**
|
|
4747
4760
|
* Get the executed migrations for a given payload version
|
|
4748
4761
|
*/
|
|
4749
|
-
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) => {
|
|
4750
4763
|
if (!currentVersion) return [];
|
|
4751
4764
|
if (currentVersion === plannedVersion) return plannedMigrations;
|
|
4752
4765
|
else {
|
|
4753
|
-
|
|
4754
|
-
|
|
4755
|
-
|
|
4756
|
-
|
|
4757
|
-
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
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;
|
|
4761
4778
|
}
|
|
4762
|
-
if (planFromVersion !== null) {
|
|
4763
|
-
|
|
4764
|
-
try {
|
|
4765
|
-
migrationsFromHistory = JSON.parse(planFromVersion);
|
|
4766
|
-
} catch {
|
|
4767
|
-
throw new Error(`Invalid record in ${entity}_version table (cannot parse JSON)`);
|
|
4768
|
-
}
|
|
4769
|
-
if (!validatePlan(migrationsFromHistory)) throw new Error(`Invalid plan in ${entity}_version table (does not match schema)`);
|
|
4770
|
-
else return migrationsFromHistory;
|
|
4771
|
-
} else {
|
|
4779
|
+
if (planFromVersion !== null) return parseAndValidatePlan(planFromVersion, `${entity}_versions table`);
|
|
4780
|
+
else {
|
|
4772
4781
|
console.error(chalk.red(historyError));
|
|
4773
4782
|
throw new Error(`Unable to migrate, could not find version history`);
|
|
4774
4783
|
}
|
|
@@ -4791,11 +4800,11 @@ const getPendingMigrations = ({ plannedMigrations, executedMigrations, printPend
|
|
|
4791
4800
|
migrator: m.migrator
|
|
4792
4801
|
}));
|
|
4793
4802
|
};
|
|
4794
|
-
const getPendingMigrators = async ({ config, preparedPlan, transaction, loadHistoricalPlans }) => {
|
|
4803
|
+
const getPendingMigrators = async ({ config, preparedPlan, transaction, loadHistoricalPlans, loadExecutedMigrations }) => {
|
|
4795
4804
|
const { directory, database, pgpHelpers, entity, version, printPendingFileNames, force } = config;
|
|
4796
4805
|
const { plannedMigrations, nextVersion } = preparedPlan ?? await PlanModule.getPlannedMigrations({ config });
|
|
4797
4806
|
const connection = transaction ?? database;
|
|
4798
|
-
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) : [];
|
|
4799
4808
|
return {
|
|
4800
4809
|
pendingMigrators: getPendingMigrations({
|
|
4801
4810
|
plannedMigrations,
|
|
@@ -4818,12 +4827,13 @@ const migrate = async ({ payload, config, metaData }) => {
|
|
|
4818
4827
|
metaData
|
|
4819
4828
|
});
|
|
4820
4829
|
};
|
|
4821
|
-
const migrateWithPlan = async ({ payload, config, metaData, preparedPlan, transaction, loadHistoricalPlans }) => {
|
|
4830
|
+
const migrateWithPlan = async ({ payload, config, metaData, preparedPlan, transaction, loadHistoricalPlans, loadExecutedMigrations }) => {
|
|
4822
4831
|
const { pendingMigrators, nextVersion } = await getPendingMigrators({
|
|
4823
4832
|
config,
|
|
4824
4833
|
preparedPlan,
|
|
4825
4834
|
transaction,
|
|
4826
|
-
loadHistoricalPlans
|
|
4835
|
+
loadHistoricalPlans,
|
|
4836
|
+
loadExecutedMigrations
|
|
4827
4837
|
});
|
|
4828
4838
|
return {
|
|
4829
4839
|
nextPayload: pendingMigrators.length ? PipeModule.pipe(pendingMigrators, payload, metaData) : payload,
|
|
@@ -4839,7 +4849,7 @@ const validateConcurrency = (concurrency) => {
|
|
|
4839
4849
|
/**
|
|
4840
4850
|
* Migrate an entity record in the database
|
|
4841
4851
|
*/
|
|
4842
|
-
const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, beforeMigrateRecord, afterMigrateRecord }) => {
|
|
4852
|
+
const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistoricalPlans, loadExecutedMigrations, beforeMigrateRecord, afterMigrateRecord }) => {
|
|
4843
4853
|
const { dry } = config;
|
|
4844
4854
|
console.log(chalk.gray(` ${record.name}`));
|
|
4845
4855
|
try {
|
|
@@ -4856,6 +4866,7 @@ const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistor
|
|
|
4856
4866
|
transaction,
|
|
4857
4867
|
preparedPlan,
|
|
4858
4868
|
loadHistoricalPlans,
|
|
4869
|
+
loadExecutedMigrations,
|
|
4859
4870
|
config: {
|
|
4860
4871
|
...config,
|
|
4861
4872
|
version: currentVersion,
|
|
@@ -4865,7 +4876,10 @@ const migrateRecord = async ({ record, attempt, config, preparedPlan, loadHistor
|
|
|
4865
4876
|
});
|
|
4866
4877
|
if (!dry) {
|
|
4867
4878
|
const nextRecord = await updateRecord(transaction, config?.entityTableName, config?.entityColumnNames, record.id, currentVersion, nextData, nextVersion, etag);
|
|
4868
|
-
if (!nextRecord)
|
|
4879
|
+
if (!nextRecord) {
|
|
4880
|
+
const expectedEtag = typeof etag === "number" ? `, etag=${JSON.stringify(etag)}` : "";
|
|
4881
|
+
throw new Error(`Failed to patch record with newest migration - The record was probably updated while migrating... Expected version=${JSON.stringify(currentVersion)}${expectedEtag}.`);
|
|
4882
|
+
}
|
|
4869
4883
|
await afterMigrateRecord({
|
|
4870
4884
|
currentRecord,
|
|
4871
4885
|
nextRecord,
|
|
@@ -4892,6 +4906,17 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
|
|
|
4892
4906
|
const { dry, concurrency = 1 } = config;
|
|
4893
4907
|
validateConcurrency(concurrency);
|
|
4894
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 });
|
|
4895
4920
|
const loadHistoricalPlans = memoize(async (planFilePath) => {
|
|
4896
4921
|
try {
|
|
4897
4922
|
return await GitModule.getHistoricalPlansFromGit(planFilePath);
|
|
@@ -4908,13 +4933,13 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
|
|
|
4908
4933
|
let attempt = 0;
|
|
4909
4934
|
while (attempt < MAX_ATTEMPTS) {
|
|
4910
4935
|
attempt += 1;
|
|
4911
|
-
if (attempt > 1) console.log(chalk.gray(`Retrying migration of ${record.name} (attempt ${attempt}/${MAX_ATTEMPTS}).`));
|
|
4912
4936
|
const migrationErrorPayload = await migrateRecord({
|
|
4913
4937
|
record,
|
|
4914
4938
|
attempt,
|
|
4915
4939
|
config,
|
|
4916
4940
|
preparedPlan,
|
|
4917
4941
|
loadHistoricalPlans,
|
|
4942
|
+
loadExecutedMigrations,
|
|
4918
4943
|
beforeMigrateRecord,
|
|
4919
4944
|
afterMigrateRecord
|
|
4920
4945
|
});
|
package/package.json
CHANGED