Migrations
Provides functionality for managing and applying data migrations.
Concept: As data schemas evolve over time, older data stored
in DataStores needs to be updated to match the new structure. This module
allows defining a series of migration steps (Types.MigrationStep) that
transform data from one version of the schema to the next.
Workflow:
-
Define migration steps using helpers like
makeAddFieldsStepormakeTransformStep. Each step must have a uniquename. - Provide the list of steps to the
StoreviaStoreContext. - When data is loaded (
Session:load), theapplyfunction in this module is called. -
applycompares the list of all defined steps against theappliedMigrationslist stored within theDataStoreRecord. -
It executes the
applyfunction of any step that hasn't been applied yet, in the order they are defined. - The
applyfunction of each step receives the current data and returns the transformed data. -
The names of successfully applied steps are added to the
appliedMigrationslist, which is then saved back to theDataStoreRecord.
Idempotency: The system ensures migrations are idempotent (applying them multiple
times has the same effect as applying them once) by checking the appliedMigrations list
before running a step.
Error Handling: Each step's apply function is executed within a pcall to catch
errors. If a step fails, the migration process stops, and the error is propagated.
Types
ApplyParams
interface ApplyParams {logger: Log.Logger--
Logger instance for logging migration progress and errors.
steps: {Types.MigrationStep}--
The list of all defined migration steps for the store.
appliedMigrations: {string}--
The list of names of migration steps already applied to this specific data record, loaded from the DataStoreRecord.
}Parameters for the apply function.
ApplyResult
interface ApplyResult {appliedMigrations: {string}--
The updated list of applied migration names, including any newly applied steps. This should be saved back to the DataStoreRecord.
}Result returned by the apply function upon successful completion.
Functions
validate
Migrations.validate(steps: {Types.MigrationStep}--
The list of migration steps to validate.
) → ()Validates that the provided migration steps adhere to the expected structure.
Errors
| Type | Description |
|---|---|
| string | Throws an error if any step is malformed. |
makeAddFieldsStep
Migrations.makeAddFieldsStep(name: string,--
The unique name for this migration step.
) → Types.MigrationStep--
A migration step object.
Helper function to create a common type of migration step: adding new fields with default values to existing data. Uses a deep merge strategy.
makeTransformStep
Migrations.makeTransformStep(name: string,--
The unique name for this migration step.
) → Types.MigrationStep--
A migration step object.
Helper function to create a migration step that applies a custom transformation function to the data.
apply
Migrations.apply() → Promise<ApplyResult>--
A Promise that resolves with the updated data and the new list of applied migration names.
Applies pending migration steps to the data.
Iterates through the defined steps and applies any step whose name is not
present in the appliedMigrations list. Ensures idempotency and uses pcall
for safe execution of each step's apply function.
Errors
| Type | Description |
|---|---|
| string | Rejects if any migration step fails during `pcall`. |
getStepNames
Migrations.getStepNames(migrations: {Types.MigrationStep}--
The list of migration steps.
) → {string}--
A list containing only the names of the migration steps.
Utility function to extract just the names from a list of migration steps.