-
-
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.
- Loading branch information
1 parent
f062e76
commit c5e8003
Showing
5 changed files
with
543 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
import { | ||
diffGrapherConfigs, | ||
GrapherInterface, | ||
mergeGrapherConfigs, | ||
} from "@ourworldindata/utils" | ||
|
||
const uuid = "d4fd6977-3dc1-11ef-8ef2-0242ac120002" | ||
|
||
const defaultConfig = { | ||
$schema: "https://files.ourworldindata.org/schemas/grapher-schema.004.json", | ||
version: 1, | ||
isPublished: false, | ||
type: "LineChart", | ||
logo: "owid", | ||
tab: "chart", | ||
hasChartTab: true, | ||
hasMapTab: false, | ||
minTime: "earliest", | ||
maxTime: "latest", | ||
xAxis: { | ||
scaleType: "linear", | ||
canChangeScaleType: false, | ||
facetDomain: "shared", | ||
removePointsOutsideDomain: false, | ||
}, | ||
yAxis: { | ||
scaleType: "linear", | ||
canChangeScaleType: false, | ||
facetDomain: "shared", | ||
removePointsOutsideDomain: false, | ||
}, | ||
baseColorScheme: "default", | ||
invertColorScheme: false, | ||
colorScale: { | ||
baseColorScheme: "default", | ||
colorSchemeInvert: false, | ||
binningStrategy: "ckmeans", | ||
binningStrategyBinCount: 5, | ||
equalSizeBins: true, | ||
customNumericColorsActive: false, | ||
}, | ||
addCountryMode: "add-country", | ||
entityType: "country or region", | ||
entityTypePlural: "countries", | ||
matchingEntitiesOnly: false, | ||
missingDataStrategy: "auto", | ||
selectedFacetStrategy: "none", | ||
facettingLabelByYVariables: "metric", | ||
stackMode: "absolute", | ||
sortBy: "total", | ||
sortOrder: "desc", | ||
scatterPointLabelStrategy: "year", | ||
compareEndPointsOnly: false, | ||
zoomToSelection: false, | ||
showYearLabels: false, | ||
showNoDataArea: true, | ||
hideLegend: false, | ||
hideLogo: false, | ||
hideTimeline: false, | ||
hideRelativeToggle: true, | ||
hideConnectedScatterLines: false, | ||
hideLinesOutsideTolerance: false, | ||
hideTotalValueLabel: false, | ||
hideScatterLabels: false, | ||
hideFacetControl: true, | ||
hideAnnotationFieldsInTitle: { | ||
entity: false, | ||
time: false, | ||
changeInPrefix: false, | ||
}, | ||
map: { | ||
projection: "World", | ||
time: "latest", | ||
timeTolerance: 0, | ||
toleranceStrategy: "closest", | ||
colorScale: { | ||
baseColorScheme: "default", | ||
colorSchemeInvert: false, | ||
binningStrategy: "ckmeans", | ||
binningStrategyBinCount: 5, | ||
equalSizeBins: true, | ||
customNumericColorsActive: false, | ||
}, | ||
tooltipUseCustomLabels: false, | ||
hideTimeline: false, | ||
}, | ||
} as unknown as GrapherInterface // TODO: type | ||
|
||
export class AddDefaultChartConfig1720449698768 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const defaultConfigJson = JSON.stringify(defaultConfig) | ||
|
||
// add default chart config as a new row in the chart_configs table | ||
await queryRunner.query( | ||
`-- sql | ||
INSERT INTO chart_configs (id, patchConfig, config) VALUES (UUID_TO_BIN(?, 1), ?, ?) | ||
`, | ||
[uuid, defaultConfigJson, defaultConfigJson] | ||
) | ||
|
||
const charts = (await queryRunner.query( | ||
`-- sql | ||
SELECT id, uuid, config FROM chart_configs | ||
WHERE uuid != ? | ||
`, | ||
[uuid] | ||
)) as { id: string; uuid: string; config: string }[] | ||
|
||
for (const chart of charts) { | ||
const originalConfig = JSON.parse(chart.config) | ||
|
||
// if the schema is missing, we assume it's the current one | ||
if (!originalConfig["$schema"]) { | ||
originalConfig["$schema"] = | ||
"https://files.ourworldindata.org/schemas/grapher-schema.004.json" | ||
} | ||
|
||
const patchConfig = diffGrapherConfigs( | ||
originalConfig, | ||
defaultConfig | ||
) | ||
const fullConfig = mergeGrapherConfigs( | ||
defaultConfig, | ||
originalConfig | ||
) | ||
|
||
await queryRunner.query( | ||
`-- sql | ||
UPDATE chart_configs | ||
SET | ||
patchConfig = ?, | ||
config = ? | ||
WHERE id = ? | ||
`, | ||
[ | ||
JSON.stringify(patchConfig), | ||
JSON.stringify(fullConfig), | ||
chart.id, | ||
] | ||
) | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`-- sql | ||
DELETE FROM chart_configs | ||
WHERE uuid = ? | ||
`, | ||
[uuid] | ||
) | ||
|
||
// recover the original configs from the charts table | ||
await queryRunner.query( | ||
`-- sql | ||
UPDATE chart_configs cc | ||
JOIN charts c | ||
SET | ||
cc.patchConfig = c.config | ||
cc.config = c.config | ||
` | ||
) | ||
} | ||
} |
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.