Skip to content

Commit

Permalink
trigger ci
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Oct 24, 2024
1 parent 31f0776 commit 283367a
Showing 1 changed file with 59 additions and 22 deletions.
81 changes: 59 additions & 22 deletions db/migration/1729763649580-SetYAxisMinDefaultToZero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,56 @@ export class SetYAxisMinDefaultToZero1729763649580
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
// when inheritance is disabled, set yAxis.min explicitly to "auto"
// for charts that used to rely on "auto" being the default
// CHARTS THAT DON'T INHERIT FROM AN INDICATOR

// set yAxis.min explicitly to "auto" for line charts
// that used to rely on "auto" being the default
await queryRunner.query(`
-- sql
UPDATE chart_configs cc
JOIN charts c ON cc.id = c.configId
SET
-- using JSON_MERGE_PATCH instead of JSON_SET in case yAxis doesn't exist
cc.patch = JSON_MERGE_PATCH(cc.patch, '{"yAxis":{"min":"auto"}}'),
cc.full = JSON_MERGE_PATCH(cc.full, '{"yAxis":{"min":"auto"}}')
cc.patch = JSON_MERGE_PATCH(cc.patch, '{"yAxis":{"min":"auto"}}')
WHERE
cc.full ->> '$.type' = 'LineChart'
AND cc.patch ->> '$.yAxis.min' IS NULL
AND c.isInheritanceEnabled IS FALSE
`)

// set yAxis.min to "auto" for etl-authored configs for configs
// recompute the full configs of all charts that don't inherit
// (we do this for all charts, not only line charts, because
// the default grapher config has been changed)
let charts = await queryRunner.query(`
-- sql
SELECT
cc.id AS configId,
cc.patch AS patchConfig
FROM charts c
JOIN chart_configs cc ON cc.id = c.configId
WHERE c.isInheritanceEnabled IS FALSE
`)
for (const chart of charts) {
const fullConfig = mergeGrapherConfigs(
defaultGrapherConfig,
JSON.parse(chart.patchConfig)
)

await queryRunner.query(
`
-- sql
UPDATE chart_configs cc
SET cc.full = ?
WHERE cc.id = ?
`,
[JSON.stringify(fullConfig), chart.configId]
)
}

// INDICATOR CHARTS

// ETL-authored configs:
// set yAxis.min explicitly to "auto" for line charts
// that used to rely on "auto" being the default
await queryRunner.query(`
-- sql
Expand All @@ -47,17 +80,31 @@ export class SetYAxisMinDefaultToZero1729763649580
cc_etl.patch AS etlConfig
FROM variables v
JOIN chart_configs cc_admin ON cc_admin.id = v.grapherConfigIdAdmin
JOIN chart_configs cc_etl ON cc_etl.id = v.grapherConfigIdETL
LEFT JOIN chart_configs cc_etl ON cc_etl.id = v.grapherConfigIdETL
WHERE
COALESCE(cc_etl.patch ->> '$.type', 'LineChart') = 'LineChart'
AND cc_etl.patch ->> '$.yAxis.min' = 'auto'
`)

for (const indicator of indicatorConfigs) {
const fullConfig = mergeGrapherConfigs(
JSON.parse(indicator.etlConfig),
JSON.parse(indicator.adminConfig)
)
const adminConfig = JSON.parse(indicator.adminConfig)
const etlConfig = indicator.etlConfig
? JSON.parse(indicator.etlConfig)
: {}

// if both configs didn't specify yAxis.min, then the config
// used to rely on the default value being "auto"
const isLineChart =
!adminConfig.type || adminConfig.type === "LineChart"
if (
isLineChart &&
adminConfig.yAxis?.min === undefined &&
etlConfig.yAxis?.min === undefined
) {
adminConfig.yAxis = adminConfig.yAxis ?? {}
adminConfig.yAxis.min = "auto"
}

const fullConfig = mergeGrapherConfigs(etlConfig, adminConfig)

await queryRunner.query(
`
Expand All @@ -71,7 +118,7 @@ export class SetYAxisMinDefaultToZero1729763649580
}

// Update the full configs of all charts that inherit from an indicator
const charts = await queryRunner.query(`
charts = await queryRunner.query(`
-- sql
SELECT
cc.id AS configId,
Expand All @@ -94,16 +141,6 @@ export class SetYAxisMinDefaultToZero1729763649580
? JSON.parse(chart.adminConfig)
: {}

// if neither the indicator chart nor the patch config specified a yAxis
// min value, then the chart used to rely on the default "auto" value
if (
etlConfig?.yAxis?.min === undefined &&
patchConfig.yAxis?.min === undefined
) {
patchConfig.yAxis = patchConfig.yAxis ?? {}
patchConfig.yAxis.min = "auto"
}

const fullConfig = mergeGrapherConfigs(
defaultGrapherConfig,
etlConfig,
Expand Down

0 comments on commit 283367a

Please sign in to comment.