-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the SO versionModel API (#150149)
## Summary Fix #150297 First step toward managed upgrades This PR adds the types that will be used for the new SO model version API, and adds the new properties to the `SavedObjectsType` type. The implementation is outside of scope of the PR and will be implemented in a future PR. The PR also adapt the `check_registered_types` test to trigger a review when the attributes of `SavedObjectsType` introduced in this PR are changed.
- Loading branch information
1 parent
82c0dfe
commit 7136039
Showing
10 changed files
with
779 additions
and
98 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
23 changes: 23 additions & 0 deletions
23
packages/core/saved-objects/core-saved-objects-server/src/model_version/index.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,23 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export type { | ||
SavedObjectModelTransformationDoc, | ||
SavedObjectModelTransformationContext, | ||
SavedObjectModelTransformationFn, | ||
SavedObjectModelBidirectionalTransformation, | ||
SavedObjectModelTransformationResult, | ||
} from './transformations'; | ||
|
||
export type { | ||
SavedObjectsModelChange, | ||
SavedObjectsModelExpansionChange, | ||
SavedObjectsModelVersion, | ||
SavedObjectsModelVersionMap, | ||
SavedObjectsModelVersionMapProvider, | ||
} from './model_version'; |
100 changes: 100 additions & 0 deletions
100
packages/core/saved-objects/core-saved-objects-server/src/model_version/model_version.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,100 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { SavedObjectsMappingProperties } from '../mapping_definition'; | ||
import type { SavedObjectModelBidirectionalTransformation } from './transformations'; | ||
|
||
/** | ||
* Represents a model version of a given savedObjects type. | ||
* | ||
* Model versions supersede the {@link SavedObjectsType.migrations | migrations} (and {@link SavedObjectsType.schemas | schemas}) APIs | ||
* by exposing an unified way of describing the changes of shape or data of a type. | ||
* | ||
* @public | ||
*/ | ||
export interface SavedObjectsModelVersion { | ||
/** | ||
* The {@link SavedObjectsModelChange | changes} associated with this version. | ||
*/ | ||
modelChange: SavedObjectsModelChange; | ||
} | ||
|
||
/** | ||
* {@link SavedObjectsModelChange | model change} representing an expansion. | ||
* | ||
* A model expansion can do either, or both, or those: | ||
* - add new mappings | ||
* - migrate data in a backward-compatible way | ||
* | ||
* @remark when adding mappings, {@link SavedObjectsType.mappings | the type mappings} must also be updated accordingly. | ||
* Overall, the type's mapping represents the latests version of the mappings, where the model changes | ||
* represent the changes of mappings between two versions. | ||
* | ||
* @public | ||
*/ | ||
export interface SavedObjectsModelExpansionChange< | ||
PreviousAttributes = unknown, | ||
NewAttributes = unknown | ||
> { | ||
/** | ||
* The type of {@link SavedObjectsModelChange | change}, used to identify them internally. | ||
*/ | ||
type: 'expansion'; | ||
/** | ||
* (optional) A bidirectional transformation to migrate the document from and/or to the previous model version. | ||
*/ | ||
transformation?: SavedObjectModelBidirectionalTransformation<PreviousAttributes, NewAttributes>; | ||
/** | ||
* (optional) The new mappings introduced in this version. | ||
*/ | ||
addedMappings?: SavedObjectsMappingProperties; | ||
/** | ||
* (optional) A list of paths to mappings to flag as deprecated. Deprecated mappings should no longer be used and will | ||
* eventually be deleted later. | ||
*/ | ||
deprecatedMappings?: string[]; | ||
} | ||
|
||
/** | ||
* Identify the model change associated with a given {@link SavedObjectsModelVersion}. | ||
* | ||
* At the moment, Only one type of change is supported: {@link SavedObjectsModelExpansionChange | expansions}. | ||
* | ||
* @public | ||
*/ | ||
export type SavedObjectsModelChange = SavedObjectsModelExpansionChange; | ||
|
||
/** | ||
* A record of {@link SavedObjectsModelVersion | model versions} for a given savedObjects type. | ||
* The record's keys must be integers, starting with 1 for the first entry, and there shouldn't be gaps. | ||
* | ||
* @example | ||
* ```typescript | ||
* const modelVersionMap: SavedObjectsModelVersionMap = { | ||
* '1': modelVersion1, | ||
* '2': modelVersion2, | ||
* '3': modelVersion3, | ||
* } | ||
* ``` | ||
* | ||
* @public | ||
*/ | ||
export interface SavedObjectsModelVersionMap { | ||
[modelVersion: string]: SavedObjectsModelVersion; | ||
} | ||
|
||
/** | ||
* A function returning a {@link SavedObjectsModelVersionMap | model version map} | ||
* | ||
* Ensured to be called after all plugins executed their `setup` phase. | ||
* Similar to what was done with migrations, can be used to defer resolving the model versions | ||
* associated to a type to after all plugins have been set up. | ||
* | ||
* @public | ||
*/ | ||
export type SavedObjectsModelVersionMapProvider = () => SavedObjectsModelVersionMap; |
81 changes: 81 additions & 0 deletions
81
packages/core/saved-objects/core-saved-objects-server/src/model_version/transformations.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,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { SavedObjectSanitizedDoc } from '../serialization'; | ||
import type { SavedObjectsMigrationLogger } from '../migration'; | ||
|
||
/** | ||
* Document type used during model migration. | ||
* | ||
* @public | ||
*/ | ||
export type SavedObjectModelTransformationDoc<T = unknown> = SavedObjectSanitizedDoc<T>; | ||
|
||
/** | ||
* Context passed down to {@link SavedObjectModelTransformationFn | transformation functions}. | ||
* | ||
* @public | ||
*/ | ||
export interface SavedObjectModelTransformationContext { | ||
/** | ||
* logger instance to be used by the migration handler | ||
*/ | ||
readonly log: SavedObjectsMigrationLogger; | ||
/** | ||
* The model version this migration is registered for | ||
*/ | ||
readonly modelVersion: number; | ||
} | ||
|
||
/** | ||
* Return type for the {@link SavedObjectModelTransformationFn | transformation functions} | ||
* | ||
* @public | ||
*/ | ||
export interface SavedObjectModelTransformationResult<DocAttrs = unknown> { | ||
document: SavedObjectModelTransformationDoc<DocAttrs>; | ||
} | ||
|
||
/** | ||
* Transformation function for the model version API. | ||
* | ||
* Similar to the old migration system, model version transformations take the document to migrate | ||
* and a context object as input and must return the transformed document in its return value. | ||
* | ||
* @public | ||
*/ | ||
export type SavedObjectModelTransformationFn< | ||
InputAttributes = unknown, | ||
OutputAttributes = unknown | ||
> = ( | ||
document: SavedObjectModelTransformationDoc<InputAttributes>, | ||
context: SavedObjectModelTransformationContext | ||
) => SavedObjectModelTransformationResult<OutputAttributes>; | ||
|
||
/** | ||
* A bidirectional transformation. | ||
* | ||
* Bidirectional transformations define migration functions that can be used to | ||
* transform a document from the lower version to the higher one (`up`), and | ||
* the other way around, from the higher version to the lower one (`down`) | ||
* | ||
* @public | ||
*/ | ||
export interface SavedObjectModelBidirectionalTransformation< | ||
PreviousAttributes = unknown, | ||
NewAttributes = unknown | ||
> { | ||
/** | ||
* The upward (previous=>next) transformation. | ||
*/ | ||
up: SavedObjectModelTransformationFn<PreviousAttributes, NewAttributes>; | ||
/** | ||
* The downward (next=>previous) transformation. | ||
*/ | ||
down: SavedObjectModelTransformationFn<NewAttributes, PreviousAttributes>; | ||
} |
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.