@oliasoft-open-source/node-json-migrator 4.2.0 → 4.2.1-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/dist/index.cjs CHANGED
@@ -7374,30 +7374,32 @@ const getVersions = async (db, entity) => {
7374
7374
  };
7375
7375
  return db.any(query, params);
7376
7376
  };
7377
- const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestVersionPlanLength) => {
7377
+ const filterRecordsWithNoPendingMigrations = ({
7378
+ records,
7379
+ latestPlan
7380
+ }) => {
7381
+ return records.filter((record) => {
7382
+ const currentHashes = new Set(
7383
+ (record?.currentPlan ?? []).map((p) => p.fileHash)
7384
+ );
7385
+ return latestPlan.some((p) => !currentHashes.has(p.fileHash));
7386
+ });
7387
+ };
7388
+ const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestPlan) => {
7378
7389
  const query = `
7379
7390
  SELECT
7380
- $(entityIdColumn:name) AS id,
7381
- $(entityNameColumn:name) AS name,
7382
- $(entityCreatedColumn:name) AS created,
7383
- $(entityVersionColumn:name) AS version
7384
- FROM $(entityTable:name)
7385
- WHERE (
7386
- $(entityVersionColumn:name) IS NULL OR $(entityVersionColumn:name) = ''
7387
- OR (
7388
- $(entityVersionColumn:name) <> $(latestVersion)
7389
- AND (
7390
- SELECT
7391
- CASE
7392
- WHEN IS_JSON(plan) THEN JSON_ARRAY_LENGTH(plan::json)
7393
- ELSE 0
7394
- END AS length
7395
- FROM $(entityVersionsTable:name)
7396
- WHERE $(entityVersionsTable:name).version = $(entityVersionColumn:name)
7397
- LIMIT 1
7398
- )::integer <= $(latestVersionPlanLength)
7399
- )
7400
- )
7391
+ e.$(entityIdColumn:name) AS id,
7392
+ e.$(entityNameColumn:name) AS name,
7393
+ e.$(entityCreatedColumn:name) AS created,
7394
+ e.$(entityVersionColumn:name) AS version,
7395
+ v_current.plan::json AS currentplan
7396
+ FROM $(entityTable:name) e
7397
+ LEFT JOIN $(entityVersionsTable:name) v_current
7398
+ ON v_current.version = e.$(entityVersionColumn:name)
7399
+ WHERE
7400
+ e.$(entityVersionColumn:name) IS NULL
7401
+ OR e.$(entityVersionColumn:name) = ''
7402
+ OR e.$(entityVersionColumn:name) <> $(latestVersion)
7401
7403
  `;
7402
7404
  const params = {
7403
7405
  entityTable: entityTableName,
@@ -7406,10 +7408,13 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
7406
7408
  entityNameColumn: entityColumnNames.name,
7407
7409
  entityCreatedColumn: entityColumnNames.created,
7408
7410
  entityVersionColumn: entityColumnNames.version,
7409
- latestVersion,
7410
- latestVersionPlanLength
7411
+ latestVersion
7411
7412
  };
7412
- return db.any(query, params);
7413
+ const records = (await db.any(query, params)).map((r) => ({
7414
+ ...r,
7415
+ currentPlan: r.currentplan
7416
+ }));
7417
+ return filterRecordsWithNoPendingMigrations({ records, latestPlan });
7413
7418
  };
7414
7419
  const getRecord = async (t, entityTableName, entityColumnNames, entityId) => {
7415
7420
  const query = `
@@ -7671,6 +7676,7 @@ const getPlannedMigrations = async ({ config }) => {
7671
7676
  importModule
7672
7677
  );
7673
7678
  return {
7679
+ validatedPlan: validatedPlannedMigrations,
7674
7680
  plannedMigrations: plannedMigrationsWithFunctions,
7675
7681
  nextVersion
7676
7682
  };
@@ -8147,18 +8153,16 @@ const migrateAll = async ({
8147
8153
  afterMigrateRecord,
8148
8154
  onMigrationErrors
8149
8155
  }) => {
8150
- const plan = await getPlannedMigrations({
8156
+ const { validatedPlan: latestPlan, nextVersion: latestVersion } = await getPlannedMigrations({
8151
8157
  config
8152
8158
  });
8153
- const latestVersion = plan.nextVersion;
8154
- const latestVersionPlanLength = plan.plannedMigrations.length;
8155
8159
  const recordList = await getRecordList(
8156
8160
  config?.database,
8157
8161
  config?.entity,
8158
8162
  config?.entityTableName,
8159
8163
  config?.entityColumnNames,
8160
8164
  latestVersion,
8161
- latestVersionPlanLength
8165
+ latestPlan
8162
8166
  );
8163
8167
  await migrateRecords({
8164
8168
  config,
package/dist/index.d.cts CHANGED
@@ -5,6 +5,12 @@ import { IDatabase, IHelpers, ITask } from 'pg-promise';
5
5
  */
6
6
  declare const createMigration: (directory: string, description: string, entity: string) => Promise<void>;
7
7
 
8
+ type TPlanEntry = {
9
+ fileName: string;
10
+ fileHash: string;
11
+ sequence: string;
12
+ };
13
+ type TPlan = TPlanEntry[];
8
14
  type TPayload = Record<string, unknown>;
9
15
  type TMetaData = Record<string, unknown>;
10
16
  type TMigration = {
@@ -19,6 +25,14 @@ type TMigration = {
19
25
  script?: string;
20
26
  migrator?: (payload: TPayload, metaData?: TMetaData) => TPayload;
21
27
  };
28
+ type TRecordDetails = {
29
+ id: number | string;
30
+ name: string;
31
+ created: string | Date;
32
+ version: string;
33
+ currentPlan?: TPlan;
34
+ };
35
+ type TRecordDetailsList = TRecordDetails[];
22
36
  type TEntityColumnNames = {
23
37
  id: string;
24
38
  name: string;
@@ -83,6 +97,11 @@ type TOnMigrationErrors = (params: TOnMigrationErrorsPayload) => Promise<void>;
83
97
  declare const getPlannedMigrations: ({ config }: {
84
98
  config: TConfig;
85
99
  }) => Promise<{
100
+ validatedPlan: {
101
+ fileHash: string;
102
+ fileName: string;
103
+ sequence: string;
104
+ }[];
86
105
  plannedMigrations: TMigration[] & any[];
87
106
  nextVersion: string;
88
107
  }>;
@@ -139,4 +158,4 @@ declare const printVersionHistory: ({ config }: {
139
158
  config: TConfig;
140
159
  }) => Promise<void>;
141
160
 
142
- export { type TAfterMigrateRecord, type TAfterMigrateRecordPayload, type TBeforeMigrateRecord, type TBeforeMigrateRecordPayload, type TConfig, type TEntityColumnNames, type TMetaData, type TMigration, type TMigrationErrorPayload, type TOnMigrationErrors, type TOnMigrationErrorsPayload, type TPayload, type TRecord, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, printVersionHistory };
161
+ export { type TAfterMigrateRecord, type TAfterMigrateRecordPayload, type TBeforeMigrateRecord, type TBeforeMigrateRecordPayload, type TConfig, type TEntityColumnNames, type TMetaData, type TMigration, type TMigrationErrorPayload, type TOnMigrationErrors, type TOnMigrationErrorsPayload, type TPayload, type TPlan, type TPlanEntry, type TRecord, type TRecordDetails, type TRecordDetailsList, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, printVersionHistory };
package/dist/index.d.mts CHANGED
@@ -5,6 +5,12 @@ import { IDatabase, IHelpers, ITask } from 'pg-promise';
5
5
  */
6
6
  declare const createMigration: (directory: string, description: string, entity: string) => Promise<void>;
7
7
 
8
+ type TPlanEntry = {
9
+ fileName: string;
10
+ fileHash: string;
11
+ sequence: string;
12
+ };
13
+ type TPlan = TPlanEntry[];
8
14
  type TPayload = Record<string, unknown>;
9
15
  type TMetaData = Record<string, unknown>;
10
16
  type TMigration = {
@@ -19,6 +25,14 @@ type TMigration = {
19
25
  script?: string;
20
26
  migrator?: (payload: TPayload, metaData?: TMetaData) => TPayload;
21
27
  };
28
+ type TRecordDetails = {
29
+ id: number | string;
30
+ name: string;
31
+ created: string | Date;
32
+ version: string;
33
+ currentPlan?: TPlan;
34
+ };
35
+ type TRecordDetailsList = TRecordDetails[];
22
36
  type TEntityColumnNames = {
23
37
  id: string;
24
38
  name: string;
@@ -83,6 +97,11 @@ type TOnMigrationErrors = (params: TOnMigrationErrorsPayload) => Promise<void>;
83
97
  declare const getPlannedMigrations: ({ config }: {
84
98
  config: TConfig;
85
99
  }) => Promise<{
100
+ validatedPlan: {
101
+ fileHash: string;
102
+ fileName: string;
103
+ sequence: string;
104
+ }[];
86
105
  plannedMigrations: TMigration[] & any[];
87
106
  nextVersion: string;
88
107
  }>;
@@ -139,4 +158,4 @@ declare const printVersionHistory: ({ config }: {
139
158
  config: TConfig;
140
159
  }) => Promise<void>;
141
160
 
142
- export { type TAfterMigrateRecord, type TAfterMigrateRecordPayload, type TBeforeMigrateRecord, type TBeforeMigrateRecordPayload, type TConfig, type TEntityColumnNames, type TMetaData, type TMigration, type TMigrationErrorPayload, type TOnMigrationErrors, type TOnMigrationErrorsPayload, type TPayload, type TRecord, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, printVersionHistory };
161
+ export { type TAfterMigrateRecord, type TAfterMigrateRecordPayload, type TBeforeMigrateRecord, type TBeforeMigrateRecordPayload, type TConfig, type TEntityColumnNames, type TMetaData, type TMigration, type TMigrationErrorPayload, type TOnMigrationErrors, type TOnMigrationErrorsPayload, type TPayload, type TPlan, type TPlanEntry, type TRecord, type TRecordDetails, type TRecordDetailsList, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, printVersionHistory };
package/dist/index.mjs CHANGED
@@ -7353,30 +7353,32 @@ const getVersions = async (db, entity) => {
7353
7353
  };
7354
7354
  return db.any(query, params);
7355
7355
  };
7356
- const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestVersionPlanLength) => {
7356
+ const filterRecordsWithNoPendingMigrations = ({
7357
+ records,
7358
+ latestPlan
7359
+ }) => {
7360
+ return records.filter((record) => {
7361
+ const currentHashes = new Set(
7362
+ (record?.currentPlan ?? []).map((p) => p.fileHash)
7363
+ );
7364
+ return latestPlan.some((p) => !currentHashes.has(p.fileHash));
7365
+ });
7366
+ };
7367
+ const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestPlan) => {
7357
7368
  const query = `
7358
7369
  SELECT
7359
- $(entityIdColumn:name) AS id,
7360
- $(entityNameColumn:name) AS name,
7361
- $(entityCreatedColumn:name) AS created,
7362
- $(entityVersionColumn:name) AS version
7363
- FROM $(entityTable:name)
7364
- WHERE (
7365
- $(entityVersionColumn:name) IS NULL OR $(entityVersionColumn:name) = ''
7366
- OR (
7367
- $(entityVersionColumn:name) <> $(latestVersion)
7368
- AND (
7369
- SELECT
7370
- CASE
7371
- WHEN IS_JSON(plan) THEN JSON_ARRAY_LENGTH(plan::json)
7372
- ELSE 0
7373
- END AS length
7374
- FROM $(entityVersionsTable:name)
7375
- WHERE $(entityVersionsTable:name).version = $(entityVersionColumn:name)
7376
- LIMIT 1
7377
- )::integer <= $(latestVersionPlanLength)
7378
- )
7379
- )
7370
+ e.$(entityIdColumn:name) AS id,
7371
+ e.$(entityNameColumn:name) AS name,
7372
+ e.$(entityCreatedColumn:name) AS created,
7373
+ e.$(entityVersionColumn:name) AS version,
7374
+ v_current.plan::json AS currentplan
7375
+ FROM $(entityTable:name) e
7376
+ LEFT JOIN $(entityVersionsTable:name) v_current
7377
+ ON v_current.version = e.$(entityVersionColumn:name)
7378
+ WHERE
7379
+ e.$(entityVersionColumn:name) IS NULL
7380
+ OR e.$(entityVersionColumn:name) = ''
7381
+ OR e.$(entityVersionColumn:name) <> $(latestVersion)
7380
7382
  `;
7381
7383
  const params = {
7382
7384
  entityTable: entityTableName,
@@ -7385,10 +7387,13 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
7385
7387
  entityNameColumn: entityColumnNames.name,
7386
7388
  entityCreatedColumn: entityColumnNames.created,
7387
7389
  entityVersionColumn: entityColumnNames.version,
7388
- latestVersion,
7389
- latestVersionPlanLength
7390
+ latestVersion
7390
7391
  };
7391
- return db.any(query, params);
7392
+ const records = (await db.any(query, params)).map((r) => ({
7393
+ ...r,
7394
+ currentPlan: r.currentplan
7395
+ }));
7396
+ return filterRecordsWithNoPendingMigrations({ records, latestPlan });
7392
7397
  };
7393
7398
  const getRecord = async (t, entityTableName, entityColumnNames, entityId) => {
7394
7399
  const query = `
@@ -7650,6 +7655,7 @@ const getPlannedMigrations = async ({ config }) => {
7650
7655
  importModule
7651
7656
  );
7652
7657
  return {
7658
+ validatedPlan: validatedPlannedMigrations,
7653
7659
  plannedMigrations: plannedMigrationsWithFunctions,
7654
7660
  nextVersion
7655
7661
  };
@@ -8126,18 +8132,16 @@ const migrateAll = async ({
8126
8132
  afterMigrateRecord,
8127
8133
  onMigrationErrors
8128
8134
  }) => {
8129
- const plan = await getPlannedMigrations({
8135
+ const { validatedPlan: latestPlan, nextVersion: latestVersion } = await getPlannedMigrations({
8130
8136
  config
8131
8137
  });
8132
- const latestVersion = plan.nextVersion;
8133
- const latestVersionPlanLength = plan.plannedMigrations.length;
8134
8138
  const recordList = await getRecordList(
8135
8139
  config?.database,
8136
8140
  config?.entity,
8137
8141
  config?.entityTableName,
8138
8142
  config?.entityColumnNames,
8139
8143
  latestVersion,
8140
- latestVersionPlanLength
8144
+ latestPlan
8141
8145
  );
8142
8146
  await migrateRecords({
8143
8147
  config,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/node-json-migrator",
3
- "version": "4.2.0",
3
+ "version": "4.2.1-beta-1",
4
4
  "description": "A library for JSON migrations",
5
5
  "homepage": "https://oliasoft-open-source.gitlab.io/node-postgresql-migrator",
6
6
  "bugs": {