-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (grapher) support multiple chart types
- Loading branch information
1 parent
1e07f96
commit c56c672
Showing
36 changed files
with
794 additions
and
235 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
|
||
export class AddTypesFieldToConfigs1731431457062 implements MigrationInterface { | ||
private async updateSchema( | ||
queryRunner: QueryRunner, | ||
newVersion: `${number}${number}${number}` | ||
): Promise<void> { | ||
const schema = `https://files.ourworldindata.org/schemas/grapher-schema.${newVersion}.json` | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
patch = JSON_SET(patch, '$.$schema', ?), | ||
full = JSON_SET(full, '$.$schema', ?) | ||
`, | ||
[schema, schema] | ||
) | ||
} | ||
|
||
private async addTypesFieldToConfigs( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
for (const configType of ["patch", "full"]) { | ||
// if hasChartTab is true, set the types field to the current type | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET ?? = JSON_SET( | ||
??, | ||
'$.chartTypes', | ||
JSON_ARRAY(?? ->> '$.type') | ||
) | ||
WHERE | ||
COALESCE(?? ->> '$.hasChartTab', 'true') = 'true' | ||
AND ?? ->> '$.type' IS NOT NULL | ||
`, | ||
[configType, configType, configType, configType, configType] | ||
) | ||
|
||
// if hasChartTab is false, set the types field to an empty array | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET ?? = JSON_SET( | ||
??, | ||
'$.chartTypes', | ||
JSON_ARRAY() | ||
) | ||
WHERE ?? ->> '$.hasChartTab' = 'false' | ||
`, | ||
[configType, configType, configType] | ||
) | ||
} | ||
} | ||
|
||
private async addDerivedChartTypeColumn( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
await queryRunner.query( | ||
`-- sql | ||
ALTER TABLE chart_configs | ||
ADD COLUMN type VARCHAR(255) GENERATED ALWAYS AS | ||
( | ||
CASE | ||
-- if types is unset, the type defaults to line chart | ||
WHEN full ->> '$.chartTypes' IS NULL THEN 'LineChart' | ||
-- else, the chart type listed first is considered the "main" type | ||
-- (might be null for Graphers without a chart tab) | ||
ELSE full ->> '$.chartTypes[0]' | ||
END | ||
) | ||
VIRTUAL AFTER slug; | ||
` | ||
) | ||
} | ||
|
||
private async removeTypeAndHasChartTabFields( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
await queryRunner.query(` | ||
-- sql | ||
UPDATE chart_configs | ||
SET patch = JSON_REMOVE(patch, '$.type', '$.hasChartTab') | ||
`) | ||
} | ||
|
||
public async removeDerivedTypeColumn( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
await queryRunner.query( | ||
`-- sql | ||
ALTER TABLE chart_configs | ||
DROP COLUMN type; | ||
` | ||
) | ||
} | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await this.addTypesFieldToConfigs(queryRunner) | ||
await this.removeTypeAndHasChartTabFields(queryRunner) | ||
await this.addDerivedChartTypeColumn(queryRunner) | ||
await this.updateSchema(queryRunner, "006") | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
Check warning on line 108 in db/migration/1731431457062-AddTypesFieldToConfigs.ts GitHub Actions / eslint
|
||
// TODO: implement down migration | ||
} | ||
} |
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
Oops, something went wrong.