-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
49 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
migration/1715728347907-AddCalculatedFieldAsColumnsForProject.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,52 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddCalculatedFieldAsColumnsForProject1715728347907 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
ALTER TABLE "project" | ||
ADD COLUMN IF NOT EXISTS "sumDonationValueUsdForActiveQfRound" DOUBLE PRECISION DEFAULT 0; | ||
`); | ||
|
||
await queryRunner.query(` | ||
ALTER TABLE "project" | ||
ADD COLUMN IF NOT EXISTS "sumDonationValueUsd" DOUBLE PRECISION DEFAULT 0; | ||
`); | ||
|
||
// Add new integer columns for counting unique donors with 'IF NOT EXISTS' | ||
await queryRunner.query(` | ||
ALTER TABLE "project" | ||
ADD COLUMN IF NOT EXISTS "countUniqueDonorsForActiveQfRound" INTEGER DEFAULT 0; | ||
`); | ||
|
||
await queryRunner.query(` | ||
ALTER TABLE "project" | ||
ADD COLUMN IF NOT EXISTS "countUniqueDonors" INTEGER DEFAULT 0; | ||
`); | ||
|
||
await queryRunner.query(` | ||
UPDATE "project" | ||
SET "countUniqueDonors" = pds."uniqueDonorsCount", | ||
"sumDonationValueUsd" = pds."sumVerifiedDonations" | ||
FROM "project_donation_summary_view" AS pds | ||
WHERE "project"."id" = pds."projectId"; | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// Use 'IF EXISTS' in the DROP statement to avoid errors in case the column does not exist | ||
await queryRunner.query( | ||
`ALTER TABLE "project" DROP COLUMN IF EXISTS "sumDonationValueUsdForActiveQfRound"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "project" DROP COLUMN IF EXISTS "sumDonationValueUsd"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "project" DROP COLUMN IF EXISTS "countUniqueDonorsForActiveQfRound"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "project" DROP COLUMN IF EXISTS "countUniqueDonors"`, | ||
); | ||
} | ||
} |
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
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
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
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
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
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