-
-
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.
✨ automatically migrate outdated configs / TAS-623 (#3967)
* ✨ automatically migrate outdated configs * ✅ add db tests * 📜 (grapher schema) update documentation * 🔨 move config migration into saveGrapher * 🔨 use migrate function in db migration
- Loading branch information
1 parent
f7503b9
commit 8c3b4a4
Showing
12 changed files
with
443 additions
and
49 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
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
52 changes: 52 additions & 0 deletions
52
db/migration/1726588731621-MigrateOutdatedConfigsToLatestVersion.ts
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,52 @@ | ||
import { migrateGrapherConfigToLatestVersion } from "@ourworldindata/grapher" | ||
import { GrapherInterface } from "@ourworldindata/types" | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
|
||
export class MigrateOutdatedConfigsToLatestVersion1726588731621 | ||
implements MigrationInterface | ||
{ | ||
private migrateConfig(config: Record<string, any>): GrapherInterface { | ||
try { | ||
return migrateGrapherConfigToLatestVersion(config) | ||
} catch { | ||
// if the migration function throws, then the $schema field | ||
// is either missing or invalid. when that happens, we assume | ||
// a schema v1, and try again | ||
config.$schema = | ||
"https://files.ourworldindata.org/schemas/grapher-schema.001.json" | ||
return migrateGrapherConfigToLatestVersion(config) | ||
} | ||
} | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const outdatedConfigs = await queryRunner.query( | ||
`-- sql | ||
SELECT id, patch, full | ||
FROM chart_configs | ||
WHERE | ||
patch ->> '$.$schema' != 'https://files.ourworldindata.org/schemas/grapher-schema.005.json' | ||
OR full ->> '$.$schema' != 'https://files.ourworldindata.org/schemas/grapher-schema.005.json' | ||
` | ||
) | ||
|
||
for (const { id, patch, full } of outdatedConfigs) { | ||
const updatedPatch = this.migrateConfig(JSON.parse(patch)) | ||
const updatedFull = this.migrateConfig(JSON.parse(full)) | ||
|
||
await queryRunner.query( | ||
`-- sql | ||
UPDATE chart_configs | ||
SET patch = ?, full = ? | ||
WHERE id = ? | ||
`, | ||
[JSON.stringify(updatedPatch), JSON.stringify(updatedFull), id] | ||
) | ||
} | ||
} | ||
|
||
public async down(): Promise<void> { | ||
throw new Error( | ||
"Can't revert migration MigrateOutdatedConfigsToLatestVersion1726588731621" | ||
) | ||
} | ||
} |
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.