forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ZDT] DocumentMigrator: support higher version documents (elastic#157895
) ## Summary Part of elastic#150312 (next steps depend on elastic#153117) **This PR does two things:** - introduce the concept of version persistence schema - adapt the document migrator to support downward migrations for documents of an higher version. In the follow-up, we will then update the calls from the SOR to the document migrator to allow downward conversions when we're using the ZDT migration algorithm (which requires elastic#153117 to be merged) ### Model version persistence schema. *(This is what has also been named 'eviction schema' or 'known fields schema'.)* A new `SavedObjectsModelVersion.schemas.backwardConversion` property was added to the model version definition. This 'schema' can either be an arbitrary function, or a `schema.object` from `@kbn/config-schema` ```ts type SavedObjectModelVersionBackwardConversionSchema< InAttrs = unknown, OutAttrs = unknown > = ObjectType | SavedObjectModelVersionBackwardConversionFn<InAttrs, OutAttrs>; ``` When specified for a version, the document's attributes will go thought this schema during down conversions by the document migrator. ### Adapt the document migrator to support downward migrations for documents of an higher version. Add an `allowDowngrade` option to `DocumentMigrator.migrate` and `KibanaMigrator.migrateDocument`. When this option is set to `true`, the document migration will accept to 'downgrade' the document if necessary, instead of throwing an error as done when the option is `false` or unspecified (which was the only behavior prior to this PR's changes) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
Showing
30 changed files
with
990 additions
and
83 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
131 changes: 131 additions & 0 deletions
131
...e-saved-objects-base-server-internal/src/model_version/backward_conversion_schema.test.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,131 @@ | ||
/* | ||
* 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 { schema } from '@kbn/config-schema'; | ||
import { convertModelVersionBackwardConversionSchema } from './backward_conversion_schema'; | ||
import type { | ||
SavedObjectUnsanitizedDoc, | ||
SavedObjectModelVersionForwardCompatibilityFn, | ||
} from '@kbn/core-saved-objects-server'; | ||
|
||
describe('convertModelVersionBackwardConversionSchema', () => { | ||
const createDoc = ( | ||
parts: Partial<SavedObjectUnsanitizedDoc<any>> | ||
): SavedObjectUnsanitizedDoc<any> => ({ | ||
id: 'id', | ||
type: 'type', | ||
attributes: {}, | ||
...parts, | ||
}); | ||
|
||
describe('using functions', () => { | ||
it('converts the schema', () => { | ||
const conversionSchema: jest.MockedFunction<SavedObjectModelVersionForwardCompatibilityFn> = | ||
jest.fn(); | ||
conversionSchema.mockImplementation((attrs) => attrs); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(conversionSchema).toHaveBeenCalledTimes(1); | ||
expect(conversionSchema).toHaveBeenCalledWith({ foo: 'bar' }); | ||
expect(output).toEqual(doc); | ||
}); | ||
|
||
it('returns the document with the updated properties', () => { | ||
const conversionSchema: jest.MockedFunction< | ||
SavedObjectModelVersionForwardCompatibilityFn<any, any> | ||
> = jest.fn(); | ||
conversionSchema.mockImplementation((attrs) => ({ foo: attrs.foo })); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar', hello: 'dolly' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(output).toEqual({ | ||
...doc, | ||
attributes: { foo: 'bar' }, | ||
}); | ||
}); | ||
|
||
it('throws if the function throws', () => { | ||
const conversionSchema: jest.MockedFunction< | ||
SavedObjectModelVersionForwardCompatibilityFn<any, any> | ||
> = jest.fn(); | ||
conversionSchema.mockImplementation(() => { | ||
throw new Error('dang'); | ||
}); | ||
|
||
const doc = createDoc({}); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
expect(() => converted(doc)).toThrowErrorMatchingInlineSnapshot(`"dang"`); | ||
}); | ||
}); | ||
|
||
describe('using config-schemas', () => { | ||
it('converts the schema', () => { | ||
const conversionSchema = schema.object( | ||
{ | ||
foo: schema.maybe(schema.string()), | ||
}, | ||
{ unknowns: 'ignore' } | ||
); | ||
const validateSpy = jest.spyOn(conversionSchema, 'validate'); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(validateSpy).toHaveBeenCalledTimes(1); | ||
expect(validateSpy).toHaveBeenCalledWith({ foo: 'bar' }, {}); | ||
expect(output).toEqual(doc); | ||
}); | ||
|
||
it('returns the document with the updated properties', () => { | ||
const conversionSchema = schema.object( | ||
{ | ||
foo: schema.maybe(schema.string()), | ||
}, | ||
{ unknowns: 'ignore' } | ||
); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar', hello: 'dolly' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
const output = converted(doc); | ||
|
||
expect(output).toEqual({ | ||
...doc, | ||
attributes: { | ||
foo: 'bar', | ||
}, | ||
}); | ||
}); | ||
|
||
it('throws if the validation throws', () => { | ||
const conversionSchema = schema.object( | ||
{ | ||
foo: schema.maybe(schema.string()), | ||
}, | ||
{ unknowns: 'forbid' } | ||
); | ||
|
||
const doc = createDoc({ attributes: { foo: 'bar', hello: 'dolly' } }); | ||
const converted = convertModelVersionBackwardConversionSchema(conversionSchema); | ||
|
||
expect(() => converted(doc)).toThrowErrorMatchingInlineSnapshot( | ||
`"[hello]: definition for this key is missing"` | ||
); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
...s/core-saved-objects-base-server-internal/src/model_version/backward_conversion_schema.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,43 @@ | ||
/* | ||
* 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 { isConfigSchema, type ObjectType } from '@kbn/config-schema'; | ||
import type { | ||
SavedObjectUnsanitizedDoc, | ||
SavedObjectModelVersionForwardCompatibilitySchema, | ||
} from '@kbn/core-saved-objects-server'; | ||
|
||
function isObjectType( | ||
schema: SavedObjectModelVersionForwardCompatibilitySchema | ||
): schema is ObjectType { | ||
return isConfigSchema(schema); | ||
} | ||
|
||
export type ConvertedSchema = (doc: SavedObjectUnsanitizedDoc) => SavedObjectUnsanitizedDoc; | ||
|
||
export const convertModelVersionBackwardConversionSchema = ( | ||
schema: SavedObjectModelVersionForwardCompatibilitySchema | ||
): ConvertedSchema => { | ||
if (isObjectType(schema)) { | ||
return (doc) => { | ||
const attrs = schema.validate(doc.attributes, {}); | ||
return { | ||
...doc, | ||
attributes: attrs, | ||
}; | ||
}; | ||
} else { | ||
return (doc) => { | ||
const attrs = schema(doc.attributes); | ||
return { | ||
...doc, | ||
attributes: attrs, | ||
}; | ||
}; | ||
} | ||
}; |
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
Oops, something went wrong.