Skip to content

Commit

Permalink
[Upgrade Assistant] Use skipFetchFields when creating the indexPatter…
Browse files Browse the repository at this point in the history
…n in order to avoid errors if index doesn't exist (#113821)

* Use skipFetchFields when creating the indexPatter in order to avoid errors when index doesnt exist

* Address CR feedback

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
sabarasaba and kibanamachine authored Oct 5, 2021
1 parent 8236569 commit 6674293
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getDeprecationIndexPatternId } from './external_links';

import { DEPRECATION_LOGS_INDEX_PATTERN } from '../../../../../common/constants';
import { dataPluginMock, Start } from '../../../../../../../../src/plugins/data/public/mocks';

describe('External Links', () => {
let dataService: Start;

beforeEach(() => {
dataService = dataPluginMock.createStartContract();
});

describe('getDeprecationIndexPatternId', () => {
it('creates new index pattern if doesnt exist', async () => {
dataService.dataViews.find = jest.fn().mockResolvedValue([]);
dataService.dataViews.createAndSave = jest.fn().mockResolvedValue({ id: '123-456' });

const indexPatternId = await getDeprecationIndexPatternId(dataService);

expect(indexPatternId).toBe('123-456');
// prettier-ignore
expect(dataService.dataViews.createAndSave).toHaveBeenCalledWith({
title: DEPRECATION_LOGS_INDEX_PATTERN,
allowNoIndex: true,
}, false, true);
});

it('uses existing index pattern if it already exists', async () => {
dataService.dataViews.find = jest.fn().mockResolvedValue([
{
id: '123-456',
title: DEPRECATION_LOGS_INDEX_PATTERN,
},
]);

const indexPatternId = await getDeprecationIndexPatternId(dataService);

expect(indexPatternId).toBe('123-456');
expect(dataService.dataViews.find).toHaveBeenCalledWith(DEPRECATION_LOGS_INDEX_PATTERN);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props {
checkpoint: string;
}

const getDeprecationIndexPatternId = async (dataService: DataPublicPluginStart) => {
export const getDeprecationIndexPatternId = async (dataService: DataPublicPluginStart) => {
const results = await dataService.dataViews.find(DEPRECATION_LOGS_INDEX_PATTERN);
// Since the find might return also results with wildcard matchers we need to find the
// index pattern that has an exact match with our title.
Expand All @@ -33,10 +33,20 @@ const getDeprecationIndexPatternId = async (dataService: DataPublicPluginStart)
if (deprecationIndexPattern) {
return deprecationIndexPattern.id;
} else {
// When creating the index pattern, we need to be careful when creating an indexPattern
// for an index that doesnt exist. Since the deprecation logs data stream is only created
// when a deprecation log is indexed it could be possible that it might not exist at the
// time we need to render the DiscoveryAppLink.
// So in order to avoid those errors we need to make sure that the indexPattern is created
// with allowNoIndex and that we skip fetching fields to from the source index.
const override = false;
const skipFetchFields = true;
// prettier-ignore
const newIndexPattern = await dataService.dataViews.createAndSave({
title: DEPRECATION_LOGS_INDEX_PATTERN,
allowNoIndex: true,
});
}, override, skipFetchFields);

return newIndexPattern.id;
}
};
Expand Down

0 comments on commit 6674293

Please sign in to comment.