From cffbb09b3ee9b4f8a42770555626b78067b77d8d Mon Sep 17 00:00:00 2001 From: sophiamersmann Date: Mon, 2 Sep 2024 15:31:47 +0000 Subject: [PATCH] improve readability of migration --- ...veIndicatorChartsToTheChartConfigsTable.ts | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/db/migration/1721296631522-MoveIndicatorChartsToTheChartConfigsTable.ts b/db/migration/1721296631522-MoveIndicatorChartsToTheChartConfigsTable.ts index 3356301490e..ee4b1e99dfa 100644 --- a/db/migration/1721296631522-MoveIndicatorChartsToTheChartConfigsTable.ts +++ b/db/migration/1721296631522-MoveIndicatorChartsToTheChartConfigsTable.ts @@ -69,41 +69,39 @@ export class MoveIndicatorChartsToTheChartConfigsTable1721296631522 // (this can happen if the migration is run for an empty test database) if (variables.length === 0) return - // generate UUIDs for every config - const ids = variables.map((v) => ({ - variableId: v.id, - uuid: uuidv7(), - })) - - // insert a new row for each config with dummy values for the config fields + // add a temporary table that maps variable IDs to config UUIDs + await queryRunner.query(`-- sql + CREATE TABLE tmpUUIDxVariableId ( + uuid char(36) NOT NULL PRIMARY KEY, + variableId INT NOT NULL + ) + `) await queryRunner.query( `-- sql - INSERT INTO chart_configs (id, patch, full) + INSERT INTO tmpUUIDxVariableId (uuid, variableId) VALUES ? `, - [ids.map(({ uuid }) => [uuid, "{}", "{}"])] + [variables.map((v) => [uuidv7(), v.id])] ) + // insert configs into the chart_configs table + await queryRunner.query(`-- sql + INSERT INTO chart_configs (id, patch, full) + SELECT tmp.uuid, v.grapherConfigETL, v.grapherConfigETL + FROM tmpUUIDxVariableId tmp + JOIN variables v ON v.id = tmp.variableId + `) + // add a reference to the chart_configs uuid in the variables table - const variablesUpdateValues = ids - .map( - ({ variableId, uuid }) => - `(${variableId},'${uuid}',unit,coverage,timespan,datasetId,display)` - ) - .join(",") await queryRunner.query(`-- sql - INSERT INTO variables (id, grapherConfigIdETL, unit, coverage, timespan, datasetId, display) - VALUES ${variablesUpdateValues} - ON DUPLICATE KEY UPDATE grapherConfigIdETL=VALUES(grapherConfigIdETL) + UPDATE variables v + JOIN tmpUUIDxVariableId tmp ON tmp.variableId = v.id + SET v.grapherConfigIdETL = tmp.uuid `) - // copy configs from the variables table to the chart_configs table + // drop temp table await queryRunner.query(`-- sql - UPDATE chart_configs - JOIN variables ON variables.grapherConfigIdETL = chart_configs.id - SET - patch = variables.grapherConfigETL, - full = variables.grapherConfigETL + DROP TABLE tmpUUIDxVariableId `) }