-
-
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 e3c1c28
Showing
8 changed files
with
132 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) |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
import { GrapherInterface } from "@ourworldindata/types" | ||
import { | ||
getSchemaVersion, | ||
isLatestVersion, | ||
isOutdatedVersionThatCanBeMigrated, | ||
type OutdatedConfig, | ||
type OutdatedSchemaVersionWithMigration, | ||
} from "./helpers" | ||
import { migrations } from "./migrations" | ||
|
||
const migrate = (config: OutdatedConfig): OutdatedConfig => { | ||
const version = getSchemaVersion( | ||
config | ||
) as OutdatedSchemaVersionWithMigration | ||
return migrations[version](config) | ||
Check failure Code scanning / CodeQL Unvalidated dynamic method call High
Invocation of method with
user-controlled Error loading related location Loading Invocation of method with user-controlled Error loading related location Loading |
||
} | ||
|
||
const recursivelyUpgradeConfigToNextSchemaVersion = ( | ||
config: OutdatedConfig | ||
): OutdatedConfig => { | ||
const version = getSchemaVersion(config) | ||
if (isLatestVersion(version)) { | ||
return config | ||
} else if (isOutdatedVersionThatCanBeMigrated(version)) { | ||
return recursivelyUpgradeConfigToNextSchemaVersion(migrate(config)) | ||
} else { | ||
throw new Error(`Missing migration: ${version}`) | ||
} | ||
} | ||
|
||
export const migrateToLatestSchemaVersion = ( | ||
config: OutdatedConfig | ||
): GrapherInterface => | ||
recursivelyUpgradeConfigToNextSchemaVersion(config) as GrapherInterface |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
import { | ||
type OutdatedConfig, | ||
type OutdatedSchemaVersionWithMigration, | ||
createSchemaForVersion, | ||
} 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 migrations: Record< | ||
OutdatedSchemaVersionWithMigration, | ||
(config: OutdatedConfig) => OutdatedConfig | ||
> = { | ||
"003": migrateFrom003To004, | ||
"004": migrateFrom004To005, | ||
} as const |