Skip to content

Commit

Permalink
update migration
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Oct 31, 2024
1 parent 5609edd commit f107534
Showing 1 changed file with 25 additions and 98 deletions.
123 changes: 25 additions & 98 deletions db/migration/1729763649580-SetYAxisMinDefaultToZero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class SetYAxisMinDefaultToZero1729763649580
{
public async up(queryRunner: QueryRunner): Promise<void> {
// 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(`
Expand All @@ -19,106 +18,36 @@ export class SetYAxisMinDefaultToZero1729763649580
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
AND cc.patch ->> '$.yAxis.min' IS NULL
`)

// 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:
// CHARTS THAT DO INHERIT FROM AN INDICATOR
// set yAxis.min explicitly to "auto" for line charts
// that used to rely on "auto" being the default
// that used to rely on "auto" being the default.
// this is the case for charts where neither the patch config
// of the chart itself nor the indicator config have a yAxis.min set
await queryRunner.query(`
-- sql
UPDATE chart_configs cc
JOIN variables v ON cc.id = v.grapherConfigIdETL
SET
cc.patch = JSON_MERGE_PATCH(cc.patch, '{"yAxis":{"min":"auto"}}'),
cc.full = JSON_MERGE_PATCH(cc.full, '{"yAxis":{"min":"auto"}}')
WHERE
COALESCE(cc.patch ->> '$.type', 'LineChart') = 'LineChart'
-- sql
UPDATE chart_configs cc
JOIN charts c ON cc.id = c.configId
-- keep charts, even if they don't have a parent variable
LEFT JOIN charts_x_parents cxp ON cxp.chartId = c.id
LEFT JOIN variables v ON v.id = cxp.variableId
LEFT JOIN chart_configs cc_admin ON cc_admin.id = v.grapherConfigIdAdmin
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"}}')
WHERE
cc.full ->> '$.type' = 'LineChart'
AND c.isInheritanceEnabled IS TRUE
AND cc.patch ->> '$.yAxis.min' IS NULL
`)

// update admin-authored configs (we don't currently have any in use
// but included for completeness)
const indicatorConfigs = await queryRunner.query(`
-- sql
SELECT
v.id AS variableId,
cc_admin.id AS adminConfigId,
cc_admin.patch AS adminConfig,
cc_etl.patch AS etlConfig
FROM variables v
JOIN chart_configs cc_admin ON cc_admin.id = v.grapherConfigIdAdmin
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 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(
`
-- sql
UPDATE chart_configs cc
SET cc.full = ?
WHERE cc.id = ?
`,
[JSON.stringify(fullConfig), indicator.adminConfigId]
)
}
AND cc_admin.full ->> '$.yAxis.min' IS NULL
`)

// Update the full configs of all charts that inherit from an indicator
charts = await queryRunner.query(`
// RECOMPUTE ALL FULL CONFIGS
// (necessary since we changed the defaultGrapherConfig)
const charts = await queryRunner.query(`
-- sql
SELECT
cc.id AS configId,
Expand All @@ -127,13 +56,11 @@ export class SetYAxisMinDefaultToZero1729763649580
cc_admin.patch AS adminConfig
FROM charts c
JOIN chart_configs cc ON cc.id = c.configId
JOIN charts_x_parents p ON p.chartId = c.id
JOIN variables v ON v.id = p.variableId
LEFT JOIN charts_x_parents p ON p.chartId = c.id
LEFT JOIN variables v ON v.id = p.variableId
LEFT JOIN chart_configs cc_etl ON cc_etl.id = v.grapherConfigIdETL
LEFT JOIN chart_configs cc_admin ON cc_admin.id = v.grapherConfigIdAdmin
WHERE c.isInheritanceEnabled IS TRUE
`)

for (const chart of charts) {
const patchConfig = JSON.parse(chart.patchConfig)
const etlConfig = chart.etlConfig ? JSON.parse(chart.etlConfig) : {}
Expand Down

0 comments on commit f107534

Please sign in to comment.