Skip to content

Commit

Permalink
Fix more broken usages of bulkCreate (#76005)
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Aug 27, 2020
1 parent 4e8dd3e commit 5449aa7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ class SavedObjectsInstallerUi extends React.Component {

let resp;
try {
resp = await this.props.bulkCreate(this.props.savedObjects, {
overwrite: this.state.overwrite,
});
// Filter out the saved object version field, if present, to avoid inadvertently triggering optimistic concurrency control.
const objectsToCreate = this.props.savedObjects.map(
// eslint-disable-next-line no-unused-vars
({ version, ...savedObject }) => savedObject
);
resp = await this.props.bulkCreate(objectsToCreate, { overwrite: this.state.overwrite });
} catch (error) {
if (!this._isMounted) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,25 @@ describe('bulkCreate', () => {

expect(component).toMatchSnapshot();
});

test('should filter out saved object version before calling bulkCreate', async () => {
const bulkCreateMock = jest.fn().mockResolvedValue({
savedObjects: [savedObject],
});
const component = mountWithIntl(
<SavedObjectsInstaller.WrappedComponent
bulkCreate={bulkCreateMock}
savedObjects={[{ ...savedObject, version: 'foo' }]}
/>
);

findTestSubject(component, 'loadSavedObjects').simulate('click');

// Ensure all promises resolve
await new Promise((resolve) => process.nextTick(resolve));
// Ensure the state changes are reflected
component.update();

expect(bulkCreateMock).toHaveBeenCalledWith([savedObject], expect.any(Object));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getAuthorizationHeader } from '../../lib/request_authorization';
import { MlInfoResponse } from '../../../common/types/ml_server_info';
import {
KibanaObjects,
KibanaObjectConfig,
ModuleDataFeed,
ModuleJob,
Module,
Expand Down Expand Up @@ -100,7 +101,7 @@ interface ObjectExistResponse {
id: string;
type: string;
exists: boolean;
savedObject?: any;
savedObject?: { id: string; type: string; attributes: KibanaObjectConfig };
}

interface SaveResults {
Expand Down Expand Up @@ -678,14 +679,14 @@ export class DataRecognizer {
let results = { saved_objects: [] as any[] };
const filteredSavedObjects = objectExistResults
.filter((o) => o.exists === false)
.map((o) => o.savedObject);
.map((o) => o.savedObject!);
if (filteredSavedObjects.length) {
results = await this.savedObjectsClient.bulkCreate(
// Add an empty migrationVersion attribute to each saved object to ensure
// it is automatically migrated to the 7.0+ format with a references attribute.
filteredSavedObjects.map((doc) => ({
...doc,
migrationVersion: doc.migrationVersion || {},
migrationVersion: {},
}))
);
}
Expand Down

0 comments on commit 5449aa7

Please sign in to comment.