-
-
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
- Loading branch information
1 parent
3ab39cb
commit edc4470
Showing
8 changed files
with
128 additions
and
12 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
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
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
36 changes: 36 additions & 0 deletions
36
packages/@ourworldindata/grapher/src/schema/migrations/helpers.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,36 @@ | ||
import { | ||
currentSchemaVersion, | ||
outdatedSchemaVersions, | ||
} from "../defaultGrapherConfig" | ||
|
||
type CurrentSchemaVersion = typeof currentSchemaVersion | ||
type OutdatedSchemaVersion = (typeof outdatedSchemaVersions)[number] | ||
type SchemaVersion = OutdatedSchemaVersion | CurrentSchemaVersion | ||
|
||
// we are missing migrations for the first two schema versions for now. | ||
// if we encounter them at some point, we should add migrations for them as well. | ||
const outdatedSchemaVersionsWithMigration = outdatedSchemaVersions.filter( | ||
(v) => v !== "001" && v !== "002" | ||
) | ||
export type OutdatedSchemaVersionWithMigration = Exclude< | ||
OutdatedSchemaVersion, | ||
"001" | "002" | ||
> | ||
|
||
// we can't type outdated configs as we don't know what they look like | ||
export type OutdatedConfig = Record<string, any> | ||
|
||
const schemaVersionRegex = | ||
/https:\/\/files\.ourworldindata\.org\/schemas\/grapher-schema\.(?<version>\d{3})\.json/m | ||
|
||
export const getSchemaVersion = (config: OutdatedConfig): SchemaVersion => | ||
config.$schema?.match(schemaVersionRegex)?.groups?.version as SchemaVersion | ||
|
||
export const createSchemaForVersion = (version: SchemaVersion): string => | ||
`https://files.ourworldindata.org/schemas/grapher-schema.${version}.json` | ||
|
||
export const isLatestVersion = (version: SchemaVersion) => | ||
version === currentSchemaVersion | ||
|
||
export const isOutdatedVersionThatCanBeMigrated = (version: SchemaVersion) => | ||
outdatedSchemaVersionsWithMigration.includes(version as any) |
26 changes: 26 additions & 0 deletions
26
packages/@ourworldindata/grapher/src/schema/migrations/migrate.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,26 @@ | ||
import { GrapherInterface } from "@ourworldindata/types" | ||
import { | ||
getSchemaVersion, | ||
isLatestVersion, | ||
isOutdatedVersionThatCanBeMigrated, | ||
type OutdatedConfig, | ||
} from "./helpers" | ||
import { runMigration } from "./migrations" | ||
|
||
const recursivelyUpgradeConfigToNextSchemaVersion = ( | ||
config: OutdatedConfig | ||
): OutdatedConfig => { | ||
const version = getSchemaVersion(config) | ||
if (isLatestVersion(version)) { | ||
return config | ||
} else if (isOutdatedVersionThatCanBeMigrated(version)) { | ||
return recursivelyUpgradeConfigToNextSchemaVersion(runMigration(config)) | ||
} else { | ||
throw new Error(`Missing migration: ${version}`) | ||
} | ||
} | ||
|
||
export const migrateToLatestSchemaVersion = ( | ||
config: OutdatedConfig | ||
): GrapherInterface => | ||
recursivelyUpgradeConfigToNextSchemaVersion(config) as GrapherInterface |
38 changes: 38 additions & 0 deletions
38
packages/@ourworldindata/grapher/src/schema/migrations/migrations.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,38 @@ | ||
import { match } from "ts-pattern" | ||
import { | ||
type OutdatedConfig, | ||
type OutdatedSchemaVersionWithMigration, | ||
createSchemaForVersion, | ||
getSchemaVersion, | ||
} from "./helpers" | ||
|
||
// | ||
// schema migrations | ||
// | ||
// add a migration if introducing a breaking change. | ||
// make sure to update the $schema field to the next version! | ||
// | ||
|
||
// TODO: write migrations for the first two schema versions | ||
|
||
const migrateFrom003To004 = (config: OutdatedConfig): OutdatedConfig => { | ||
delete config.data | ||
config.$schema = createSchemaForVersion("004") | ||
return config | ||
} | ||
|
||
const migrateFrom004To005 = (config: OutdatedConfig): OutdatedConfig => { | ||
delete config.hideLinesOutsideTolerance | ||
config.$schema = createSchemaForVersion("005") | ||
return config | ||
} | ||
|
||
export const runMigration = (config: OutdatedConfig): OutdatedConfig => { | ||
const version = getSchemaVersion( | ||
config | ||
) as OutdatedSchemaVersionWithMigration | ||
return match(version) | ||
.with("003", () => migrateFrom003To004(config)) | ||
.with("004", () => migrateFrom004To005(config)) | ||
.exhaustive() | ||
} |