Skip to content

Commit

Permalink
🔨 adapt this PR to string based UUIDs v7
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 authored and sophiamersmann committed Aug 6, 2024
1 parent 90d50ba commit 0c0cbfb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { defaultGrapherConfig } from "@ourworldindata/grapher"
import { DimensionProperty, GrapherInterface } from "@ourworldindata/types"
import { mergeGrapherConfigs, omit } from "@ourworldindata/utils"
import { MigrationInterface, QueryRunner } from "typeorm"
import { uuidv7 } from "uuidv7"

export class MoveIndicatorChartsToTheChartConfigsTable1721296631522
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE variables
ADD COLUMN grapherConfigIdAdmin binary(16) UNIQUE AFTER sort,
ADD COLUMN grapherConfigIdETL binary(16) UNIQUE AFTER grapherConfigIdAdmin,
ADD COLUMN grapherConfigIdAdmin char(36) UNIQUE AFTER sort,
ADD COLUMN grapherConfigIdETL char(36) UNIQUE AFTER grapherConfigIdAdmin,
ADD CONSTRAINT fk_variables_grapherConfigIdAdmin
FOREIGN KEY (grapherConfigIdAdmin)
REFERENCES chart_configs (id)
Expand Down Expand Up @@ -68,7 +69,7 @@ export class MoveIndicatorChartsToTheChartConfigsTable1721296631522
}

// insert config into the chart_configs table
const configId = await getBinaryUUID(queryRunner)
const configId = uuidv7()
await queryRunner.query(
`-- sql
INSERT INTO chart_configs (id, patch, full)
Expand Down Expand Up @@ -232,8 +233,3 @@ export class MoveIndicatorChartsToTheChartConfigsTable1721296631522
}
}
}

const getBinaryUUID = async (queryRunner: QueryRunner): Promise<Buffer> => {
const rows = await queryRunner.query(`SELECT UUID_TO_BIN(UUID(), 1) AS id`)
return rows[0].id
}
3 changes: 2 additions & 1 deletion db/model/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DEFAULT_GRAPHER_CONFIG_SCHEMA,
} from "@ourworldindata/grapher"
import pl from "nodejs-polars"
import { uuidv7 } from "uuidv7"
import { DATA_API_URL } from "../../settings/serverSettings.js"
import { escape } from "mysql2"
import {
Expand Down Expand Up @@ -135,7 +136,7 @@ export async function insertNewGrapherConfigForVariable(
}
): Promise<void> {
// insert chart config into the database
const configId = await db.getBinaryUUID(knex)
const configId = uuidv7()
await db.knexRaw(
knex,
`-- sql
Expand Down
4 changes: 2 additions & 2 deletions packages/@ourworldindata/types/src/dbTypes/Variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export interface DbInsertVariable {
descriptionShort?: string | null
dimensions?: JsonString | null
display: JsonString
grapherConfigIdAdmin?: Buffer | null
grapherConfigIdETL?: Buffer | null
grapherConfigIdAdmin?: string | null
grapherConfigIdETL?: string | null
id?: number
license?: JsonString | null
licenses?: JsonString | null
Expand Down

0 comments on commit 0c0cbfb

Please sign in to comment.