Skip to content

Commit

Permalink
Fix migration issue with old filter queries (#41245) (#41268)
Browse files Browse the repository at this point in the history
* Fix migration issue that assumes old filter queries that don't have meta property have query string

* fix type error

* adjust snapshot so it includes the new title attribute
  • Loading branch information
stacey-gammon authored Jul 16, 2019
1 parent 8b575af commit b77d8db
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
* under the License.
*/

import { migrations } from '../../../migrations/';
import { migrations730 } from './migrations_730';
import {
DashboardDoc700To720,
DashboardDoc730ToLatest,
RawSavedDashboardPanel730ToLatest,
DashboardDocPre700,
} from './types';

const mockLogger = {
Expand All @@ -36,6 +38,7 @@ test('dashboard migration 7.3.0 migrates filters to query on search source', ()
type: 'dashboard',
references: [],
attributes: {
title: 'hi',
useMargins: true,
description: '',
uiStateJSON: '{}',
Expand All @@ -52,22 +55,55 @@ test('dashboard migration 7.3.0 migrates filters to query on search source', ()
const newDoc = migrations730(doc, mockLogger);

expect(newDoc).toMatchInlineSnapshot(`
Object {
"attributes": Object {
"description": "",
"kibanaSavedObjectMeta": Object {
"searchSourceJSON": "{\\"filter\\":[],\\"highlightAll\\":true,\\"version\\":true,\\"query\\":{\\"query\\":\\"n: 6\\",\\"language\\":\\"lucene\\"}}",
Object {
"attributes": Object {
"description": "",
"kibanaSavedObjectMeta": Object {
"searchSourceJSON": "{\\"filter\\":[],\\"highlightAll\\":true,\\"version\\":true,\\"query\\":{\\"query\\":\\"n: 6\\",\\"language\\":\\"lucene\\"}}",
},
"panelsJSON": "[{\\"id\\":\\"1\\",\\"type\\":\\"visualization\\",\\"foo\\":true},{\\"id\\":\\"2\\",\\"type\\":\\"visualization\\",\\"bar\\":true}]",
"timeRestore": false,
"title": "hi",
"useMargins": true,
"version": 1,
},
"id": "1",
"references": Array [],
"type": "dashboard",
}
`);
});

// See https://github.com/elastic/kibana/issues/41240 - this can happen.
test('dashboard migration 7.3.0 does not migrate filters to query on search source when there is no query string property', () => {
const doc: DashboardDocPre700 = {
id: 'AWviOturFv4p9HkVSIgn',
type: 'dashboard',
attributes: {
title: 'all_the_viz',
description: '',
panelsJSON:
'[{"col":1,"columns":["_source"],"id":"AWviL7GTFv4p9HkVSIf8","panelIndex":1,"row":1,"size_x":6,"size_y":3,"sort":["@timestamp","desc"],"type":"search"},{"col":7,"id":"AWviMFurFv4p9HkVSIf9","panelIndex":2,"row":1,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviMOBlFv4p9HkVSIf-","panelIndex":3,"row":4,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviMZ6SFv4p9HkVSIf_","panelIndex":4,"row":4,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviMqdpFv4p9HkVSIgK","panelIndex":5,"row":7,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviM0kxFv4p9HkVSIga","panelIndex":6,"row":7,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviM_UOFv4p9HkVSIgb","panelIndex":7,"row":10,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviNMDaFv4p9HkVSIgc","panelIndex":8,"row":10,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviNR7vFv4p9HkVSIgd","panelIndex":9,"row":13,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviNcPPFv4p9HkVSIge","panelIndex":10,"row":13,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviNu5XFv4p9HkVSIgf","panelIndex":11,"row":16,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviN5pxFv4p9HkVSIgg","panelIndex":12,"row":16,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviN_qeFv4p9HkVSIgh","panelIndex":13,"row":19,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviOI-XFv4p9HkVSIgi","panelIndex":14,"row":19,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviOP2GFv4p9HkVSIgj","panelIndex":15,"row":22,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviOYpkFv4p9HkVSIgk","panelIndex":16,"row":22,"size_x":6,"size_y":3,"type":"visualization"},{"col":1,"id":"AWviOilNFv4p9HkVSIgl","panelIndex":17,"row":25,"size_x":6,"size_y":3,"type":"visualization"},{"col":7,"id":"AWviOpW1Fv4p9HkVSIgm","panelIndex":18,"row":25,"size_x":6,"size_y":3,"type":"visualization"}]',
optionsJSON: '{"darkTheme":false}',
uiStateJSON:
'{"P-9":{"vis":{"params":{"sort":{"columnIndex":null,"direction":null}}}},"P-3":{"vis":{"defaultColors":{"0 - 88":"rgb(247,252,245)","88 - 175":"rgb(199,233,192)","175 - 263":"rgb(116,196,118)","263 - 350":"rgb(35,139,69)"}}},"P-10":{"vis":{"defaultColors":{"0 - 50":"rgb(0,104,55)","50 - 75":"rgb(255,255,190)","75 - 100":"rgb(165,0,38)"}}},"P-11":{"vis":{"defaultColors":{"0 - 100":"rgb(0,104,55)"}}},"P-12":{"vis":{"defaultColors":{"0 - 100":"rgb(0,104,55)"}}}}',
version: 1,
timeRestore: false,
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"filter":[{"query":{"match_all":{}}}],"highlightAll":true,"version":true}',
},
},
"panelsJSON": "[{\\"id\\":\\"1\\",\\"type\\":\\"visualization\\",\\"foo\\":true},{\\"id\\":\\"2\\",\\"type\\":\\"visualization\\",\\"bar\\":true}]",
"timeRestore": false,
"useMargins": true,
"version": 1,
},
"id": "1",
"references": Array [],
"type": "dashboard",
}
`);
};

const doc700: DashboardDoc700To720 = migrations.dashboard['7.0.0'](doc, mockLogger);
const newDoc = migrations.dashboard['7.3.0'](doc700, mockLogger);

const parsedSearchSource = JSON.parse(newDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON);
expect(parsedSearchSource.filter.length).toBe(1);
expect(parsedSearchSource.query.query).toBe('');

expect(newDoc.attributes.uiStateJSON).toBeUndefined();
});

test('dashboard migration 7.3.0 migrates panels', () => {
Expand All @@ -76,6 +112,7 @@ test('dashboard migration 7.3.0 migrates panels', () => {
type: 'dashboard',
references: [],
attributes: {
title: 'hi',
useMargins: true,
description: '',
uiStateJSON: '{}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export function migrations730(
moveFiltersToQuery(searchSource)
);
} catch (e) {
logger.warning(`Exception @ migrations730 while trying to migrate query filters! ${e}`);
logger.warning(
`Exception @ migrations730 while trying to migrate query filters!\nError:${e}\nSearchSource JSON:\n${doc.attributes.kibanaSavedObjectMeta.searchSourceJSON}`
);
return doc;
}

Expand All @@ -55,7 +57,12 @@ export function migrations730(
try {
const panels = JSON.parse(doc.attributes.panelsJSON);
doc.attributes.panelsJSON = JSON.stringify(
migratePanelsTo730(panels, '7.3.0', doc.attributes.useMargins, uiState)
migratePanelsTo730(
panels,
'7.3.0',
doc.attributes.useMargins === undefined ? true : doc.attributes.useMargins,
uiState
)
);

delete doc.attributes.uiStateJSON;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export interface SearchSource730 {
}

function isQueryFilter(filter: Filter | { query: unknown }): filter is Pre600FilterQuery {
return filter.query && !(filter as Filter).meta;
return (
filter.query && !(filter as Filter).meta && (filter as Pre600FilterQuery).query.query_string
);
}

export function moveFiltersToQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ interface DashboardAttributes extends SavedObjectAttributes {
description: string;
version: number;
timeRestore: boolean;
useMargins: boolean;
useMargins?: boolean;
title: string;
optionsJSON?: string;
}

export type DashboardAttributes730ToLatest = DashboardAttributes;
Expand All @@ -42,7 +44,9 @@ interface DashboardAttributesTo720 extends SavedObjectAttributes {
uiStateJSON: string;
version: number;
timeRestore: boolean;
useMargins: boolean;
useMargins?: boolean;
title: string;
optionsJSON?: string;
}

export type DashboardDoc730ToLatest = Doc<DashboardAttributes>;
Expand Down

0 comments on commit b77d8db

Please sign in to comment.