Last active
August 20, 2021 10:57
-
-
Save Offirmo/1dcf80172cf8c00e87c24e4227e407d6 to your computer and use it in GitHub Desktop.
[Offirmo's migrations] #Offirmo #TypeScript
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
import { enforce_immutability, LastMigrationStep, MigrationStep, generic_migrate_to_latest } from '@offirmo-private/state-utils' | |
import { LIB, SCHEMA_VERSION } from './consts' | |
import { UState, TState } from './types' | |
import { OMRSoftExecutionContext } from './sec' | |
// some hints may be needed to migrate to demo state | |
// need to export them for composing tests | |
export const MIGRATION_HINTS_FOR_TESTS: any = enforce_immutability({ | |
}) | |
///////////////////// | |
///////////////////// | |
type StateForMigration = [UState, TState] | |
export function migrate_to_latest(SEC: OMRSoftExecutionContext, legacy_state: Readonly<any>, hints: Readonly<any> = {}): StateForMigration { | |
return generic_migrate_to_latest({ | |
SEC: SEC as any, | |
LIB, | |
SCHEMA_VERSION, | |
legacy_state, | |
hints, | |
sub_states_migrate_to_latest: {}, | |
pipeline: [ | |
migrate_to_4x, | |
migrate_to_3, | |
migrate_to_2, | |
] | |
}) | |
} | |
///////////////////// | |
const migrate_to_4x: LastMigrationStep<StateForMigration, [any, any]> = (SEC, legacy_state, hints, previous, legacy_schema_version) => { | |
//console.log('hello from migrate_to_4x', legacy_state, hints, previous, legacy_schema_version) | |
if (legacy_schema_version < 3) | |
legacy_state = previous(SEC, legacy_state, hints) | |
let [ u_state, t_state ] = legacy_state | |
u_state = { | |
...u_state, | |
schema_version: 4, | |
} | |
t_state = { | |
...t_state, | |
schema_version: 4, | |
// this field was added | |
revision: u_state.revision, | |
} | |
// eventually, update schema version | |
state.schema_version = 14 | |
state.t_state.schema_version = 14 | |
state.u_state.schema_version = 14 | |
state = { | |
...state, | |
u_state: { | |
...state.u_state, | |
schema_version: 15, | |
}, | |
t_state: { | |
...state.t_state, | |
schema_version: 15, | |
}, | |
} | |
return [ u_state, t_state ] | |
} | |
const migrate_to_3: MigrationStep<[any, any], [any, any]> = (SEC, legacy_state, hints, next, legacy_schema_version) => { | |
if (legacy_schema_version < 2) | |
legacy_state = next(SEC, legacy_state, hints) | |
let state: any = { | |
...legacy_state, | |
slot_id: 0, | |
} | |
delete state.persistence_id | |
return state | |
} | |
const migrate_to_2: MigrationStep<[any, any], [any, any]> = () => { | |
throw new Error('Schema is too old (pre-beta), can’t migrate!') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment