Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.9] Fix more broken usages of bulkCreate (#76005) #76131

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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