import { MigrationInterface , QueryRunner } from 'typeorm' ; import { Countries } from '@energyweb/utils-general' ; type OldOrg = { id : number ; country : number ; signatoryCountry : number ; } ; type NewOrg = { id : number ; country : string ; signatoryCountry : string ; } ; export class IsoOrganization1608286180141 implements MigrationInterface { name = 'IsoOrganization1608286180141' ; public async up ( queryRunner ) { const organizations = await queryRunner . query ( `SELECT "id", "country", "signatoryCountry" FROM "platform_organization"` ) ; const ids = organizations . reduce ( ( set , org ) => { set . add ( org . country ) ; set . add ( org . signatoryCountry ) ; return set ; } , new Set < number > ( ) ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ADD "countryCode" character varying` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ADD "signatoryCountryCode" character varying` ) ; for ( const countryId of ids ) { const countryCode = Countries . find ( ( c ) => c . id === countryId ) . code ; await queryRunner . query ( `UPDATE "platform_organization" SET "countryCode"='${ countryCode }' WHERE "country"=${ countryId }` ) ; await queryRunner . query ( `UPDATE "platform_organization" SET "signatoryCountryCode"='${ countryCode }' WHERE "signatoryCountry"=${ countryId }` ) ; } await queryRunner . query ( `ALTER TABLE "platform_organization" DROP COLUMN "country"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" DROP COLUMN "signatoryCountry"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" RENAME COLUMN "countryCode" to "country"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" RENAME COLUMN "signatoryCountryCode" to  "signatoryCountry"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ALTER COLUMN "country" SET NOT NULL` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ALTER COLUMN "signatoryCountry" SET NOT NULL` ) ; } public async down ( queryRunner ) { const organizations = await queryRunner . query ( `SELECT "id", "country", "signatoryCountry" FROM "platform_organization"` ) ; const codes = organizations . reduce ( ( set , org ) => { set . add ( org . country ) ; set . add ( org . signatoryCountry ) ; return set ; } , new Set < string > ( ) ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ADD "countryId" integer` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ADD "signatoryCountryId" integer` ) ; for ( const code of codes ) { const countryId = Countries . find ( ( c ) => c . code === code ) . id ; await queryRunner . query ( `UPDATE "platform_organization" SET "countryId"=${ countryId } WHERE "country"='${ code }'` ) ; await queryRunner . query ( `UPDATE "platform_organization" SET "signatoryCountryId"=${ countryId } WHERE "signatoryCountry"='${ code }'` ) ; } await queryRunner . query ( `ALTER TABLE "platform_organization" DROP COLUMN "country"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" DROP COLUMN "signatoryCountry"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" RENAME COLUMN "countryId" to "country"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" RENAME COLUMN "signatoryCountryId" to  "signatoryCountry"` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ALTER COLUMN "country" SET NOT NULL` ) ; await queryRunner . query ( `ALTER TABLE "platform_organization" ALTER COLUMN "signatoryCountry" SET NOT NULL` ) ; } }