-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 (grapher) turn related questions into an object
- Loading branch information
1 parent
c9de5c6
commit ed6ea01
Showing
14 changed files
with
217 additions
and
147 deletions.
There are no files selected for viewing
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
91 changes: 91 additions & 0 deletions
91
db/migration/1730722779919-TurnRelatedQuestionIntoObject.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,91 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
|
||
export class TurnRelatedQuestionIntoObject1730722779919 | ||
implements MigrationInterface | ||
{ | ||
private async updateSchema( | ||
queryRunner: QueryRunner, | ||
newSchema: string | ||
): Promise<void> { | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs cc | ||
SET cc.patch = JSON_SET(cc.patch, '$.$schema', ?), | ||
cc.full = JSON_SET(cc.full, '$.$schema', ?) | ||
`, | ||
[newSchema, newSchema] | ||
) | ||
} | ||
|
||
private async turnRelatedQuestionIntoObject( | ||
queryRunner: QueryRunner, | ||
config: "patch" | "full" | ||
): Promise<void> { | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
?? = JSON_REMOVE( | ||
JSON_SET( | ||
??, | ||
'$.relatedQuestion', | ||
??->'$.relatedQuestions[0]' | ||
), | ||
'$.relatedQuestions' | ||
) | ||
WHERE ?? ->> '$.relatedQuestions' is not null | ||
`, | ||
[config, config, config, config] | ||
) | ||
} | ||
|
||
private async turnRelatedQuestionIntoArray( | ||
queryRunner: QueryRunner, | ||
config: "patch" | "full" | ||
): Promise<void> { | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
?? = JSON_REMOVE( | ||
JSON_SET( | ||
??, | ||
'$.relatedQuestions', | ||
JSON_ARRAY( | ||
JSON_OBJECT( | ||
'url', ?? ->> '$.relatedQuestion.url', | ||
'text', ?? ->> '$.relatedQuestion.text' | ||
) | ||
) | ||
), | ||
'$.relatedQuestion' | ||
) | ||
WHERE ?? ->> '$.relatedQuestion' IS NOT NULL | ||
`, | ||
[config, config, config, config, config] | ||
) | ||
} | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await this.turnRelatedQuestionIntoObject(queryRunner, "patch") | ||
await this.turnRelatedQuestionIntoObject(queryRunner, "full") | ||
|
||
await this.updateSchema( | ||
queryRunner, | ||
"https://files.ourworldindata.org/schemas/grapher-schema.006.json" | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await this.turnRelatedQuestionIntoArray(queryRunner, "patch") | ||
await this.turnRelatedQuestionIntoArray(queryRunner, "full") | ||
|
||
await this.updateSchema( | ||
queryRunner, | ||
"https://files.ourworldindata.org/schemas/grapher-schema.005.json" | ||
) | ||
} | ||
} |
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
Oops, something went wrong.