-
-
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) support multiple chart types
- Loading branch information
1 parent
a6288be
commit f5db56f
Showing
35 changed files
with
579 additions
and
202 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
198 changes: 198 additions & 0 deletions
198
db/migration/1731316808331-AddAvailableTabsToGrapherConfigs.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,198 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
|
||
export class AddAvailableTabsToGrapherConfigs1731316808331 | ||
implements MigrationInterface | ||
{ | ||
private async updateSchema( | ||
queryRunner: QueryRunner, | ||
newSchema: string | ||
): Promise<void> { | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
patch = JSON_SET(patch, '$.$schema', ?), | ||
full = JSON_SET(full, '$.$schema', ?) | ||
`, | ||
[newSchema, newSchema] | ||
) | ||
} | ||
|
||
private async addAvailableTabsToGrapherConfigs( | ||
queryRunner: QueryRunner, | ||
config: "patch" | "full" | ||
): Promise<void> { | ||
// case 1: | ||
// hasMapTab=false, hasChartTab=false -> availableTabs=[] | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
?? = JSON_SET(??, '$.availableTabs', JSON_ARRAY('Table')) | ||
WHERE | ||
(?? ->> '$.hasMapTab' = 'false' OR ?? ->> '$.hasMapTab' IS NULL) | ||
AND (?? ->> '$.hasChartTab' = 'false') | ||
`, | ||
[config, config, config, config, config] | ||
) | ||
|
||
// case 2: | ||
// hasMapTab=false, hasChartTab=true -> availableTabs=[chartType] | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
?? = JSON_SET(??, '$.availableTabs', JSON_ARRAY('Table', COALESCE(?? ->> '$.type', 'LineChart'))) | ||
WHERE | ||
(?? ->> '$.hasMapTab' = 'false' OR ?? ->> '$.hasMapTab' IS NULL) | ||
AND (?? ->> '$.hasChartTab' = 'true' OR ?? ->> '$.hasChartTab' IS NULL) | ||
`, | ||
[config, config, config, config, config, config, config] | ||
) | ||
|
||
// case 3: | ||
// hasMapTab=true, hasChartTab=false -> availableTabs=["WorldMap"] | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
?? = JSON_SET(??, '$.availableTabs', JSON_ARRAY('Table', 'WorldMap')) | ||
WHERE | ||
(?? ->> '$.hasMapTab' = 'true') | ||
AND (?? ->> '$.hasChartTab' = 'false') | ||
`, | ||
[config, config, config, config] | ||
) | ||
|
||
// case 4: | ||
// hasMapTab=true, hasChartTab=true -> availableTabs=["WorldMap", chartType] | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET | ||
?? = JSON_SET(??, '$.availableTabs', JSON_ARRAY('Table', 'WorldMap', COALESCE(?? ->> '$.type', 'LineChart'))) | ||
WHERE | ||
(?? ->> '$.hasMapTab' = 'true') | ||
AND (?? ->> '$.hasChartTab' = 'true' OR ?? ->> '$.hasChartTab' IS NULL) | ||
`, | ||
[config, config, config, config, config, config] | ||
) | ||
} | ||
|
||
private async removeAvailableTabsFromGrapherConfigs( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
// TODO | ||
} | ||
|
||
private async updateTabField( | ||
queryRunner: QueryRunner, | ||
config: "patch" | "full" | ||
): Promise<void> { | ||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET ?? = JSON_SET(??, '$.tab', 'WorldMap') | ||
WHERE ?? ->> '$.tab' = 'map' | ||
`, | ||
[config, config, config] | ||
) | ||
|
||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET ?? = JSON_SET(??, '$.tab', 'Table') | ||
WHERE ?? ->> '$.tab' = 'table' | ||
`, | ||
[config, config, config] | ||
) | ||
|
||
await queryRunner.query( | ||
` | ||
-- sql | ||
UPDATE chart_configs | ||
SET ?? = JSON_SET(??, '$.tab', COALESCE(?? ->> '$.type', 'LineChart')) | ||
WHERE ?? ->> '$.tab' = 'chart' | ||
`, | ||
[config, config, config, config] | ||
) | ||
} | ||
|
||
private async removeTypeHasMapTabAndHasChartTabFields( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
await queryRunner.query(` | ||
-- sql | ||
UPDATE chart_configs | ||
SET patch = JSON_REMOVE(patch, '$.type', '$.hasChartTab', '$.hasMapTab') | ||
`) | ||
} | ||
|
||
private async addTypeHasMapTabAndHasChartTabFields( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
// TODO | ||
} | ||
|
||
private async addDerivedTypeColumn( | ||
queryRunner: QueryRunner | ||
): Promise<void> { | ||
// TODO: not future-proof | ||
await queryRunner.query( | ||
`-- sql | ||
ALTER TABLE chart_configs | ||
ADD COLUMN type VARCHAR(255) GENERATED ALWAYS AS | ||
( | ||
CASE | ||
WHEN JSON_LENGTH(full, '$.availableTabs') = 0 THEN NULL | ||
WHEN full ->> '$.availableTabs[0]' != 'Map' THEN full ->> '$.availableTabs[0]' | ||
ELSE full ->> '$.availableTabs[1]' | ||
END | ||
) | ||
VIRTUAL AFTER slug; | ||
` | ||
) | ||
} | ||
|
||
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.addAvailableTabsToGrapherConfigs(queryRunner, "patch") | ||
await this.addAvailableTabsToGrapherConfigs(queryRunner, "full") | ||
await this.updateTabField(queryRunner, "patch") | ||
await this.updateTabField(queryRunner, "full") | ||
// await this.removeTypeHasMapTabAndHasChartTabFields(queryRunner) | ||
// await this.updateSchema( | ||
// queryRunner, | ||
// "https://files.ourworldindata.org/schemas/grapher-schema.007.json" | ||
// ) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// TODO: order! | ||
// await this.removeAvailableTabsFromGrapherConfigs(queryRunner) | ||
// await this.revertTabFieldMigration(queryRunner) | ||
// await this.addTypeHasMapTabAndHasChartTabFields(queryRunner) | ||
// await this.updateSchema( | ||
// queryRunner, | ||
// "https://files.ourworldindata.org/schemas/grapher-schema.006.json" | ||
// ) | ||
// await this.removeDerivedTypeColumn(queryRunner) | ||
} | ||
} |
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.