Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into kbn-xxx-SOR-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed May 10, 2023
2 parents 48a1be0 + ea23bbb commit 5eac343
Show file tree
Hide file tree
Showing 208 changed files with 1,041 additions and 6,107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ export {
getVirtualVersionsFromMappings,
getVirtualVersionsFromMappingMeta,
getModelVersionDelta,
buildModelVersionTransformFn,
aggregateMappingAdditions,
} from './src/model_version';
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 { SavedObjectsModelChange } from '@kbn/core-saved-objects-server';
import { aggregateMappingAdditions } from './aggregate_model_changes';

describe('aggregateMappingAdditions', () => {
it('merges top level properties', () => {
const changes: SavedObjectsModelChange[] = [
{ type: 'mappings_addition', addedMappings: { foo: { type: 'text' } } },
{ type: 'mappings_addition', addedMappings: { bar: { type: 'keyword' } } },
];
const output = aggregateMappingAdditions(changes);
expect(output).toEqual({
foo: { type: 'text' },
bar: { type: 'keyword' },
});
});

it('merges nested properties', () => {
const changes: SavedObjectsModelChange[] = [
{
type: 'mappings_addition',
addedMappings: { nested: { properties: { foo: { type: 'text' } } } },
},
{
type: 'mappings_addition',
addedMappings: { nested: { properties: { bar: { type: 'keyword' } } } },
},
];
const output = aggregateMappingAdditions(changes);
expect(output).toEqual({
nested: {
properties: {
foo: { type: 'text' },
bar: { type: 'keyword' },
},
},
});
});

it('accepts other types of changes', () => {
const changes: SavedObjectsModelChange[] = [
{ type: 'mappings_addition', addedMappings: { foo: { type: 'text' } } },
{ type: 'mappings_deprecation', deprecatedMappings: [] },
{ type: 'data_backfill', transform: jest.fn() },
];
const output = aggregateMappingAdditions(changes);
expect(output).toEqual({
foo: { type: 'text' },
});
});

it('override in order in case of definition conflict', () => {
const changes: SavedObjectsModelChange[] = [
{ type: 'mappings_addition', addedMappings: { foo: { type: 'text' } } },
{ type: 'mappings_addition', addedMappings: { foo: { type: 'keyword' } } },
];
const output = aggregateMappingAdditions(changes);
expect(output).toEqual({
foo: { type: 'keyword' },
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 { merge } from 'lodash';
import type {
SavedObjectsModelChange,
SavedObjectsMappingProperties,
} from '@kbn/core-saved-objects-server';

/**
* Merge the added mappings from the given list of model changes.
* Note: only changes of the `mappings_addition` type have mapping addition.
*/
export const aggregateMappingAdditions = (
changes: SavedObjectsModelChange[]
): SavedObjectsMappingProperties => {
let mappings: SavedObjectsMappingProperties = {};
changes.forEach((change) => {
if (change.type === 'mappings_addition') {
mappings = merge(mappings, change.addedMappings);
}
});
return mappings;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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 { loggerMock } from '@kbn/logging-mocks';
import type {
SavedObjectsModelChange,
SavedObjectModelTransformationFn,
SavedObjectModelTransformationDoc,
SavedObjectModelTransformationContext,
} from '@kbn/core-saved-objects-server';
import { buildModelVersionTransformFn } from './build_transform_fn';

describe('buildModelVersionTransformFn', () => {
const stubTransform = (): jest.MockedFn<SavedObjectModelTransformationFn> =>
jest.fn().mockImplementation((doc: SavedObjectModelTransformationDoc) => ({ document: doc }));

const createContext = (): SavedObjectModelTransformationContext => ({
log: loggerMock.create(),
modelVersion: 42,
});

const createDoc = (): SavedObjectModelTransformationDoc => ({
id: 'foo',
type: 'bar',
attributes: {},
});

it('builds the transform from a single function', () => {
const transform1 = stubTransform();
const changes: SavedObjectsModelChange[] = [{ type: 'data_backfill', transform: transform1 }];

const mergedTransform = buildModelVersionTransformFn(changes);

const context = createContext();
const document = createDoc();

mergedTransform(document, context);

expect(transform1).toHaveBeenCalledTimes(1);
expect(transform1).toHaveBeenCalledWith(document, context);
});

it('builds the transform from 2 functions', () => {
const transform1 = stubTransform();
const transform2 = stubTransform();
const changes: SavedObjectsModelChange[] = [
{ type: 'data_backfill', transform: transform1 },
{ type: 'data_backfill', transform: transform2 },
];

const mergedTransform = buildModelVersionTransformFn(changes);

const context = createContext();
const document = createDoc();

mergedTransform(document, context);

expect(transform1).toHaveBeenCalledTimes(1);
expect(transform1).toHaveBeenCalledWith(document, context);
expect(transform2).toHaveBeenCalledTimes(1);
expect(transform2).toHaveBeenCalledWith(document, context);
expect(transform1.mock.invocationCallOrder[0]).toBeLessThan(
transform2.mock.invocationCallOrder[0]
);
});

it('builds the transform from more functions', () => {
const transform1 = stubTransform();
const transform2 = stubTransform();
const transform3 = stubTransform();
const transform4 = stubTransform();
const changes: SavedObjectsModelChange[] = [
{ type: 'data_backfill', transform: transform1 },
{ type: 'data_backfill', transform: transform2 },
{ type: 'data_backfill', transform: transform3 },
{ type: 'data_backfill', transform: transform4 },
];

const mergedTransform = buildModelVersionTransformFn(changes);

const context = createContext();
const document = createDoc();

mergedTransform(document, context);

expect(transform1).toHaveBeenCalledTimes(1);
expect(transform2).toHaveBeenCalledTimes(1);
expect(transform3).toHaveBeenCalledTimes(1);
expect(transform4).toHaveBeenCalledTimes(1);

expect(transform1).toHaveBeenCalledWith(document, context);
expect(transform2).toHaveBeenCalledWith(document, context);
expect(transform3).toHaveBeenCalledWith(document, context);
expect(transform4).toHaveBeenCalledWith(document, context);

expect(transform1.mock.invocationCallOrder[0]).toBeLessThan(
transform2.mock.invocationCallOrder[0]
);
expect(transform2.mock.invocationCallOrder[0]).toBeLessThan(
transform3.mock.invocationCallOrder[0]
);
expect(transform3.mock.invocationCallOrder[0]).toBeLessThan(
transform4.mock.invocationCallOrder[0]
);
});

it('returns a no-op function when no transform functions are present', () => {
const changes: SavedObjectsModelChange[] = [
{ type: 'mappings_addition', addedMappings: {} },
{ type: 'mappings_deprecation', deprecatedMappings: [] },
];

const mergedTransform = buildModelVersionTransformFn(changes);

const context = createContext();
const document = createDoc();

const output = mergedTransform(document, context);
// test that it returns the source doc unmodified
expect(output.document).toEqual(createDoc());
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 {
SavedObjectsModelChange,
SavedObjectModelTransformationFn,
} from '@kbn/core-saved-objects-server';

/**
* Build the transform function for given model version, by chaining the transformations from its model changes.
*/
export const buildModelVersionTransformFn = (
modelChanges: SavedObjectsModelChange[]
): SavedObjectModelTransformationFn => {
const transformFns: SavedObjectModelTransformationFn[] = [];
modelChanges.forEach((change) => {
if (change.type === 'data_backfill') {
transformFns.push(change.transform);
}
});
return mergeTransformFunctions(transformFns);
};

const mergeTransformFunctions = (
transformFns: SavedObjectModelTransformationFn[]
): SavedObjectModelTransformationFn => {
if (transformFns.length === 0) {
return noopTransform;
}
if (transformFns.length === 1) {
return transformFns[0];
}
let mergedFn = transformFns[0];
for (let i = 1; i < transformFns.length; i++) {
mergedFn = merge(transformFns[i], mergedFn);
}
return mergedFn;
};

const noopTransform: SavedObjectModelTransformationFn = (doc) => ({ document: doc });

const merge = (
outer: SavedObjectModelTransformationFn,
inner: SavedObjectModelTransformationFn
): SavedObjectModelTransformationFn => {
return (document, context) => {
return outer(inner(document, context).document, context);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ export {
getVirtualVersionsFromMappingMeta,
} from './model_version_from_mappings';
export { getModelVersionDelta } from './get_version_delta';
export { buildModelVersionTransformFn } from './build_transform_fn';
export { aggregateMappingAdditions } from './aggregate_model_changes';
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ describe('ModelVersion map utilities', () => {
});

const dummyModelVersion = (): SavedObjectsModelVersion => ({
modelChange: {
type: 'expansion',
},
changes: [],
});

const dummyMigration = jest.fn();
Expand Down
Loading

0 comments on commit 5eac343

Please sign in to comment.