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.
register model version migration to the document migrator (elastic#15…
…0842) ## Summary Part of elastic#150301 - Add logic to converts model version transformations to the format used by the document migrator - Use in when preparing migration data for the document migrator - Improve the migration validation logic to take model versions into account (and clean it) --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
5554fe8
commit dc8ab4d
Showing
19 changed files
with
1,417 additions
and
268 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
12 changes: 12 additions & 0 deletions
12
...core/saved-objects/core-saved-objects-base-server-internal/src/model_version/constants.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,12 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* The major version that is used to represent model versions. | ||
*/ | ||
export const modelVersionVirtualMajor = 10; |
105 changes: 105 additions & 0 deletions
105
...aved-objects/core-saved-objects-base-server-internal/src/model_version/conversion.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,105 @@ | ||
/* | ||
* 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 { | ||
isVirtualModelVersion, | ||
virtualVersionToModelVersion, | ||
modelVersionToVirtualVersion, | ||
assertValidModelVersion, | ||
} from './conversion'; | ||
|
||
describe('isVirtualModelVersion', () => { | ||
it('returns true when the version is a virtual model version', () => { | ||
expect(isVirtualModelVersion('10.0.0')).toEqual(true); | ||
expect(isVirtualModelVersion('10.7.0')).toEqual(true); | ||
expect(isVirtualModelVersion('10.12.0')).toEqual(true); | ||
}); | ||
|
||
it('returns false when the version is not a virtual model version', () => { | ||
expect(isVirtualModelVersion('9.2.0')).toEqual(false); | ||
expect(isVirtualModelVersion('10.7.1')).toEqual(false); | ||
expect(isVirtualModelVersion('11.2.0')).toEqual(false); | ||
}); | ||
|
||
it('throws when the version is not a valid semver', () => { | ||
expect(() => isVirtualModelVersion('9.-2.0')).toThrowErrorMatchingInlineSnapshot( | ||
`"Invalid semver: 9.-2.0"` | ||
); | ||
expect(() => isVirtualModelVersion('12.3.5.6.7')).toThrowErrorMatchingInlineSnapshot( | ||
`"Invalid semver: 12.3.5.6.7"` | ||
); | ||
expect(() => isVirtualModelVersion('dolly')).toThrowErrorMatchingInlineSnapshot( | ||
`"Invalid semver: dolly"` | ||
); | ||
}); | ||
}); | ||
|
||
describe('virtualVersionToModelVersion', () => { | ||
it('converts the given virtual version to its model version', () => { | ||
expect(virtualVersionToModelVersion('10.0.0')).toEqual(0); | ||
expect(virtualVersionToModelVersion('10.7.0')).toEqual(7); | ||
expect(virtualVersionToModelVersion('10.12.0')).toEqual(12); | ||
}); | ||
|
||
it('throws when the version is not a virtual model version', () => { | ||
expect(() => virtualVersionToModelVersion('9.2.0')).toThrowErrorMatchingInlineSnapshot( | ||
`"Version is not a virtual model version"` | ||
); | ||
expect(() => virtualVersionToModelVersion('11.3.0')).toThrowErrorMatchingInlineSnapshot( | ||
`"Version is not a virtual model version"` | ||
); | ||
expect(() => virtualVersionToModelVersion('10.3.42')).toThrowErrorMatchingInlineSnapshot( | ||
`"Version is not a virtual model version"` | ||
); | ||
}); | ||
|
||
it('throws when the version is not a valid semver', () => { | ||
expect(() => virtualVersionToModelVersion('9.-2.0')).toThrowErrorMatchingInlineSnapshot( | ||
`"Invalid semver: 9.-2.0"` | ||
); | ||
expect(() => virtualVersionToModelVersion('12.3.5.6.7')).toThrowErrorMatchingInlineSnapshot( | ||
`"Invalid semver: 12.3.5.6.7"` | ||
); | ||
expect(() => virtualVersionToModelVersion('dolly')).toThrowErrorMatchingInlineSnapshot( | ||
`"Invalid semver: dolly"` | ||
); | ||
}); | ||
}); | ||
|
||
describe('modelVersionToVirtualVersion', () => { | ||
it('converts the given model version to its virtual version', () => { | ||
expect(modelVersionToVirtualVersion(0)).toEqual('10.0.0'); | ||
expect(modelVersionToVirtualVersion(7)).toEqual('10.7.0'); | ||
expect(modelVersionToVirtualVersion(12)).toEqual('10.12.0'); | ||
}); | ||
}); | ||
|
||
describe('assertValidModelVersion', () => { | ||
it('throws if the provided value is not an integer', () => { | ||
expect(() => assertValidModelVersion(9.4)).toThrowErrorMatchingInlineSnapshot( | ||
`"Model version must be an integer"` | ||
); | ||
expect(() => assertValidModelVersion('7.6')).toThrowErrorMatchingInlineSnapshot( | ||
`"Model version must be an integer"` | ||
); | ||
}); | ||
|
||
it('throws if the provided value is a negative integer', () => { | ||
expect(() => assertValidModelVersion(-4)).toThrowErrorMatchingInlineSnapshot( | ||
`"Model version cannot be negative"` | ||
); | ||
expect(() => assertValidModelVersion('-3')).toThrowErrorMatchingInlineSnapshot( | ||
`"Model version cannot be negative"` | ||
); | ||
}); | ||
|
||
it('returns the model version as a number', () => { | ||
expect(assertValidModelVersion(4)).toEqual(4); | ||
expect(assertValidModelVersion('3')).toEqual(3); | ||
}); | ||
}); |
94 changes: 94 additions & 0 deletions
94
...ore/saved-objects/core-saved-objects-base-server-internal/src/model_version/conversion.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,94 @@ | ||
/* | ||
* 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 Semver from 'semver'; | ||
import { modelVersionVirtualMajor } from './constants'; | ||
|
||
/** | ||
* Returns the virtual version associated with the given model version | ||
* | ||
* @example | ||
* ``` | ||
* modelVersionToVirtualVersion(5); // "10.5.0"; | ||
* modelVersionToVirtualVersion("3"); // "10.3.0"; | ||
* ``` | ||
*/ | ||
export const modelVersionToVirtualVersion = (modelVersion: number | string) => { | ||
const validatedModelVersion = assertValidModelVersion(modelVersion); | ||
return `${modelVersionVirtualMajor}.${validatedModelVersion}.0`; | ||
}; | ||
|
||
/** | ||
* Return true if the given semver version is a virtual model version. | ||
* Virtual model versions are version which major is the {@link modelVersionVirtualMajor} | ||
* | ||
* @example | ||
* ``` | ||
* isVirtualModelVersion("10.3.0"); // true | ||
* isVirtualModelVersion("9.7.0); // false | ||
* isVirtualModelVersion("10.3.1); // false | ||
* ``` | ||
*/ | ||
export const isVirtualModelVersion = (version: string): boolean => { | ||
const semver = Semver.parse(version); | ||
if (!semver) { | ||
throw new Error(`Invalid semver: ${version}`); | ||
} | ||
return _isVirtualModelVersion(semver); | ||
}; | ||
|
||
/** | ||
* Converts a virtual model version to its model version. | ||
* | ||
* @example | ||
* ``` | ||
* virtualVersionToModelVersion('10.3.0'); // 3 | ||
* virtualVersionToModelVersion('9.3.0'); // throw | ||
* ``` | ||
*/ | ||
export const virtualVersionToModelVersion = (virtualVersion: string): number => { | ||
const semver = Semver.parse(virtualVersion); | ||
if (!semver) { | ||
throw new Error(`Invalid semver: ${virtualVersion}`); | ||
} | ||
if (!_isVirtualModelVersion(semver)) { | ||
throw new Error(`Version is not a virtual model version`); | ||
} | ||
return semver.minor; | ||
}; | ||
|
||
/** | ||
* Asserts the provided number or string is a valid model version, and returns it. | ||
* | ||
* A valid model version is a positive integer. | ||
* | ||
* @example | ||
* ``` | ||
* assertValidModelVersion("7"); // 7 | ||
* assertValidModelVersion(4); // 4 | ||
* assertValidModelVersion("foo"); // throw | ||
* assertValidModelVersion("9.7"); // throw | ||
* assertValidModelVersion("-3"); // throw | ||
* ``` | ||
*/ | ||
export const assertValidModelVersion = (modelVersion: string | number): number => { | ||
if (typeof modelVersion === 'string') { | ||
modelVersion = parseFloat(modelVersion); | ||
} | ||
if (!Number.isInteger(modelVersion)) { | ||
throw new Error('Model version must be an integer'); | ||
} | ||
if (modelVersion < 0) { | ||
throw new Error('Model version cannot be negative'); | ||
} | ||
return modelVersion; | ||
}; | ||
|
||
const _isVirtualModelVersion = (semver: Semver.SemVer): boolean => { | ||
return semver.major === modelVersionVirtualMajor && semver.patch === 0; | ||
}; |
15 changes: 15 additions & 0 deletions
15
...ges/core/saved-objects/core-saved-objects-base-server-internal/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,15 @@ | ||
/* | ||
* 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 { modelVersionVirtualMajor } from './constants'; | ||
export { | ||
assertValidModelVersion, | ||
isVirtualModelVersion, | ||
modelVersionToVirtualVersion, | ||
virtualVersionToModelVersion, | ||
} from './conversion'; |
34 changes: 34 additions & 0 deletions
34
...cts-migration-server-internal/src/document_migrator/build_active_migrations.test.mocks.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 @@ | ||
/* | ||
* 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 const getReferenceTransformsMock = jest.fn(); | ||
export const getConversionTransformsMock = jest.fn(); | ||
|
||
jest.doMock('./internal_transforms', () => ({ | ||
getReferenceTransforms: getReferenceTransformsMock, | ||
getConversionTransforms: getConversionTransformsMock, | ||
})); | ||
|
||
export const getModelVersionTransformsMock = jest.fn(); | ||
|
||
jest.doMock('./model_version', () => ({ | ||
getModelVersionTransforms: getModelVersionTransformsMock, | ||
})); | ||
|
||
export const validateTypeMigrationsMock = jest.fn(); | ||
|
||
jest.doMock('./validate_migrations', () => ({ | ||
validateTypeMigrations: validateTypeMigrationsMock, | ||
})); | ||
|
||
export const resetAllMocks = () => { | ||
getReferenceTransformsMock.mockReset().mockReturnValue([]); | ||
getConversionTransformsMock.mockReset().mockReturnValue([]); | ||
getModelVersionTransformsMock.mockReset().mockReturnValue([]); | ||
validateTypeMigrationsMock.mockReset(); | ||
}; |
Oops, something went wrong.