@oliasoft-open-source/node-json-migrator 5.6.4 → 5.7.0-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.
- package/README.md +9 -0
- package/dist/index.d.mts +7 -1
- package/dist/index.mjs +28 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -181,6 +181,15 @@ and [merge requests](https://gitlab.com/oliasoft-open-source/node-json-migrator/
|
|
|
181
181
|
- to build: `npm run buld` (this transpile a production build to the `dist` directory)
|
|
182
182
|
- the build `dist` directory should be committed
|
|
183
183
|
|
|
184
|
+
## Migrating selected records
|
|
185
|
+
|
|
186
|
+
Pass `recordIds` to `migrateAll()` to restrict the usual migration eligibility checks to a list of IDs.
|
|
187
|
+
An empty list selects zero records. Omitting `recordIds` checks the entire entity table.
|
|
188
|
+
|
|
189
|
+
For larger selections, prepare the plan once with `getPlannedMigrations({ config })` and pass that
|
|
190
|
+
`preparedPlan` to each `migrateAll({ recordIds, preparedPlan, ... })` call. Use the same migration configuration and
|
|
191
|
+
plan for the duration of the run. Each call processes one selection with the usual transaction, retry and error handling.
|
|
192
|
+
|
|
184
193
|
## Concurrent database migrations
|
|
185
194
|
|
|
186
195
|
`migrateAll()` accepts `config.concurrency`, a positive integer that defaults to `1` (sequential processing).
|
package/dist/index.d.mts
CHANGED
|
@@ -42,6 +42,7 @@ type TRecordDetails = {
|
|
|
42
42
|
version: string;
|
|
43
43
|
currentPlan?: TPlan;
|
|
44
44
|
};
|
|
45
|
+
type TRecordIds = readonly TRecordDetails['id'][];
|
|
45
46
|
type TRecordDetailsList = TRecordDetails[];
|
|
46
47
|
type TEntityColumnNames = {
|
|
47
48
|
id: string;
|
|
@@ -110,6 +111,7 @@ declare const getPlannedMigrations: ({
|
|
|
110
111
|
nextVersion: string;
|
|
111
112
|
planHasChanged: boolean;
|
|
112
113
|
}>;
|
|
114
|
+
type TPreparedPlan = Awaited<ReturnType<typeof getPlannedMigrations>>;
|
|
113
115
|
//#endregion
|
|
114
116
|
//#region src/plan/planned-version.d.ts
|
|
115
117
|
declare const getPlannedVersion: ({
|
|
@@ -141,15 +143,19 @@ declare const migrate: ({
|
|
|
141
143
|
*/
|
|
142
144
|
declare const migrateAll: ({
|
|
143
145
|
config,
|
|
146
|
+
recordIds,
|
|
147
|
+
preparedPlan,
|
|
144
148
|
beforeMigrateRecord,
|
|
145
149
|
afterMigrateRecord,
|
|
146
150
|
onMigrationErrors
|
|
147
151
|
}: {
|
|
148
152
|
config: TConfig;
|
|
153
|
+
recordIds?: TRecordIds;
|
|
154
|
+
preparedPlan?: TPreparedPlan;
|
|
149
155
|
beforeMigrateRecord: TBeforeMigrateRecord;
|
|
150
156
|
afterMigrateRecord: TAfterMigrateRecord;
|
|
151
157
|
onMigrationErrors: TOnMigrationErrors;
|
|
152
|
-
}) => Promise<
|
|
158
|
+
}) => Promise<number>;
|
|
153
159
|
//#endregion
|
|
154
160
|
//#region src/pipe/pipe.d.ts
|
|
155
161
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -4299,7 +4299,8 @@ const filterRecordsWithNoPendingMigrations = ({ records, latestPlan }) => {
|
|
|
4299
4299
|
return latestPlan.some((p) => !currentHashes.has(p.fileHash));
|
|
4300
4300
|
});
|
|
4301
4301
|
};
|
|
4302
|
-
const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestPlan) => {
|
|
4302
|
+
const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestPlan, recordIds) => {
|
|
4303
|
+
if (recordIds?.length === 0) return [];
|
|
4303
4304
|
const query = `
|
|
4304
4305
|
SELECT
|
|
4305
4306
|
e.$(entityIdColumn:name) AS id,
|
|
@@ -4310,10 +4311,12 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
|
|
|
4310
4311
|
FROM $(entityTable:name) e
|
|
4311
4312
|
LEFT JOIN $(entityVersionsTable:name) v_current
|
|
4312
4313
|
ON v_current.version = e.$(entityVersionColumn:name)
|
|
4313
|
-
WHERE
|
|
4314
|
+
WHERE (
|
|
4314
4315
|
e.$(entityVersionColumn:name) IS NULL
|
|
4315
4316
|
OR e.$(entityVersionColumn:name) = ''
|
|
4316
4317
|
OR e.$(entityVersionColumn:name) <> $(latestVersion)
|
|
4318
|
+
)
|
|
4319
|
+
${recordIds === void 0 ? "" : "AND e.$(entityIdColumn:name) IN ($(recordIds:csv))"}
|
|
4317
4320
|
`;
|
|
4318
4321
|
const params = {
|
|
4319
4322
|
entityTable: entityTableName,
|
|
@@ -4322,7 +4325,8 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
|
|
|
4322
4325
|
entityNameColumn: entityColumnNames.name,
|
|
4323
4326
|
entityCreatedColumn: entityColumnNames.created,
|
|
4324
4327
|
entityVersionColumn: entityColumnNames.version,
|
|
4325
|
-
latestVersion
|
|
4328
|
+
latestVersion,
|
|
4329
|
+
recordIds
|
|
4326
4330
|
};
|
|
4327
4331
|
return filterRecordsWithNoPendingMigrations({
|
|
4328
4332
|
records: (await db.any(query, params)).map((r) => ({
|
|
@@ -4750,7 +4754,8 @@ const parseHistoricalPlan = (rawPlan, revision) => {
|
|
|
4750
4754
|
plan
|
|
4751
4755
|
};
|
|
4752
4756
|
} catch {
|
|
4753
|
-
|
|
4757
|
+
const detail = rawPlan.includes("\0") ? " (contains NUL bytes)" : "";
|
|
4758
|
+
console.warn(`Skipping invalid historical plan.json at commit ${revision.version}${detail}`);
|
|
4754
4759
|
return null;
|
|
4755
4760
|
}
|
|
4756
4761
|
};
|
|
@@ -4791,18 +4796,22 @@ const getExecutedMigrationsFromVersion = async (database, pgpHelpers, entity, di
|
|
|
4791
4796
|
version: currentVersion
|
|
4792
4797
|
});
|
|
4793
4798
|
if (migrations !== null) return migrations;
|
|
4794
|
-
let planFromVersion = null;
|
|
4795
4799
|
console.warn(chalk.yellow(`Version not found in ${entity}_version table, trying git history instead`));
|
|
4796
4800
|
const { historicalPlans } = await loadHistoricalPlans(`${directory}/plan.json`);
|
|
4797
|
-
|
|
4798
|
-
|
|
4799
|
-
planFromVersion = historicalPlans.find((p) => p.version === currentVersion)?.plan || null;
|
|
4800
|
-
}
|
|
4801
|
-
if (planFromVersion !== null) return parseAndValidatePlan(planFromVersion, `${entity}_versions table`);
|
|
4802
|
-
else {
|
|
4801
|
+
const planFromVersion = historicalPlans.find((p) => p.version === currentVersion)?.plan;
|
|
4802
|
+
if (planFromVersion === void 0) {
|
|
4803
4803
|
console.error(chalk.red(historyError));
|
|
4804
4804
|
throw new Error(`Unable to migrate, could not find version history`);
|
|
4805
4805
|
}
|
|
4806
|
+
const executedMigrations = parseAndValidatePlan(planFromVersion, `${entity} plan.json from Git history`);
|
|
4807
|
+
try {
|
|
4808
|
+
await database.tx(async (transaction) => {
|
|
4809
|
+
await insertVersions(transaction, pgpHelpers, entity, historicalPlans);
|
|
4810
|
+
});
|
|
4811
|
+
} catch (error) {
|
|
4812
|
+
console.warn(`Unable to cache Git history for ${entity} (${historicalPlans.length} plans, code: ${error?.code ?? "unknown"}, message: ${error?.message ?? error}). Using the Git plan for version ${currentVersion}.`);
|
|
4813
|
+
}
|
|
4814
|
+
return executedMigrations;
|
|
4806
4815
|
}
|
|
4807
4816
|
};
|
|
4808
4817
|
//#endregion
|
|
@@ -4987,18 +4996,21 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
|
|
|
4987
4996
|
/**
|
|
4988
4997
|
* Migrate all records (only use via command line interface)
|
|
4989
4998
|
*/
|
|
4990
|
-
const migrateAll = async ({ config, beforeMigrateRecord, afterMigrateRecord, onMigrationErrors }) => {
|
|
4999
|
+
const migrateAll = async ({ config, recordIds, preparedPlan, beforeMigrateRecord, afterMigrateRecord, onMigrationErrors }) => {
|
|
4991
5000
|
validateConcurrency(config.concurrency ?? 1);
|
|
4992
|
-
const
|
|
4993
|
-
const { validatedPlan: latestPlan, nextVersion: latestVersion } =
|
|
5001
|
+
const plan = preparedPlan ?? await PlanModule.getPlannedMigrations({ config });
|
|
5002
|
+
const { validatedPlan: latestPlan, nextVersion: latestVersion } = plan;
|
|
5003
|
+
const recordList = await getRecordList(config?.database, config?.entity, config?.entityTableName, config?.entityColumnNames, latestVersion, latestPlan, recordIds);
|
|
5004
|
+
if (recordIds !== void 0 && recordList.length === 0) return 0;
|
|
4994
5005
|
await migrateRecords({
|
|
4995
5006
|
config,
|
|
4996
|
-
preparedPlan,
|
|
4997
|
-
recordList
|
|
5007
|
+
preparedPlan: plan,
|
|
5008
|
+
recordList,
|
|
4998
5009
|
beforeMigrateRecord,
|
|
4999
5010
|
afterMigrateRecord,
|
|
5000
5011
|
onMigrationErrors
|
|
5001
5012
|
});
|
|
5013
|
+
return recordList.length;
|
|
5002
5014
|
};
|
|
5003
5015
|
//#endregion
|
|
5004
5016
|
//#region src/history/history.ts
|
package/package.json
CHANGED