-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from energywebfoundation/synced-status
- Loading branch information
Showing
14 changed files
with
532 additions
and
185 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
packages/origin-247-certificate/migrations/1641991678453-AddIsSyncedToReadModel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import { groupBy } from 'lodash'; | ||
|
||
export class AddIsSyncedToReadModel1641991678453 implements MigrationInterface { | ||
name = 'AddIsSyncedToReadModel1641991678453'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "certificate_read_model" ADD "isSynced" boolean`); | ||
await this.setSyncStatus(queryRunner); | ||
await queryRunner.query( | ||
`ALTER TABLE "certificate_read_model" ALTER COLUMN "isSynced" SET NOT NULL` | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "certificate_read_model" DROP COLUMN "isSynced"`); | ||
} | ||
|
||
private async setSyncStatus(queryRunner: QueryRunner): Promise<void> { | ||
const events = await queryRunner.query(`SELECT * FROM certificate_event`); | ||
|
||
const eventsByCertificate = groupBy(events, 'internalCertificateId'); | ||
|
||
for (const [internalCertificateId, events] of Object.entries(eventsByCertificate)) { | ||
const isSynced = this.reduceIsSynced(events); | ||
|
||
await queryRunner.query( | ||
`UPDATE "certificate_read_model" SET "isSynced" = $1 WHERE "internalCertificateId" = $2`, | ||
[isSynced, internalCertificateId] | ||
); | ||
} | ||
} | ||
|
||
private reduceIsSynced(events: any[]): boolean { | ||
const toPersistCounter = events.reduce((toPersistCounter, event) => { | ||
switch (event.type) { | ||
case 'Issued': | ||
case 'Transferred': | ||
case 'Claimed': | ||
return toPersistCounter + 1; | ||
case 'ClaimPersisted': | ||
case 'IssuancePersisted': | ||
case 'TransferPersisted': | ||
return toPersistCounter - 1; | ||
case 'PersistError': | ||
return toPersistCounter; | ||
default: | ||
toPersistCounter; | ||
} | ||
}, 0); | ||
|
||
if (toPersistCounter > 0) { | ||
return false; | ||
} else if (toPersistCounter === 0) { | ||
return true; | ||
} else { | ||
throw new Error( | ||
`Too many persisted events for certificate ${events[0].internalCertificateId}` | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.