From 6e6475c2cfd9121db8c0e493b09829291f8a4a3e Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Fri, 25 Sep 2020 16:06:07 +0300 Subject: [PATCH 1/3] Invalid `searchSourceJSON` causes saved object migration to fail Closes: #78530 --- .../saved_objects/dashboard_migrations.ts | 12 +-- .../migrate_match_all_query.test.ts | 17 ++++ .../saved_objects/migrate_match_all_query.ts | 7 ++ .../saved_objects/search_migrations.test.ts | 83 ++++++++++++------- .../server/saved_objects/search_migrations.ts | 9 +- .../visualization_migrations.test.ts | 77 +++++++++++------ .../saved_objects/visualization_migrations.ts | 10 ++- 7 files changed, 144 insertions(+), 71 deletions(-) diff --git a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts index 1e8356a1ef100..a137b274185c3 100644 --- a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts +++ b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts @@ -101,17 +101,7 @@ const migrations700: SavedObjectMigrationFn = (doc): DashboardDoc700To }; export const dashboardSavedObjectTypeMigrations = { - /** - * We need to have this migration twice, once with a version prior to 7.0.0 once with a version - * after it. The reason for that is, that this migration has been introduced once 7.0.0 was already - * released. Thus a user who already had 7.0.0 installed already got the 7.0.0 migrations below running, - * so we need a version higher than that. But this fix was backported to the 6.7 release, meaning if we - * would only have the 7.0.1 migration in here a user on the 6.7 release will migrate their saved objects - * to the 7.0.1 state, and thus when updating their Kibana to 7.0, will never run the 7.0.0 migrations introduced - * in that version. So we apply this twice, once with 6.7.2 and once with 7.0.1 while the backport to 6.7 - * only contained the 6.7.2 migration and not the 7.0.1 migration. - */ - '6.7.2': flow(migrateMatchAllQuery), '7.0.0': flow(migrations700), '7.3.0': flow(migrations730), + '7.8.2': flow(migrateMatchAllQuery), }; diff --git a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.test.ts b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.test.ts index 8a91c422eed3d..ce7a5ffcd9fe1 100644 --- a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.test.ts +++ b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.test.ts @@ -49,4 +49,21 @@ describe('migrate match_all query', () => { }, }); }); + + it('should return original doc if searchSourceJSON cannot be parsed', () => { + const migratedDoc = migrateMatchAllQuery( + { + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + } as Parameters[0], + savedObjectMigrationContext + ); + + expect(migratedDoc).toEqual({ + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }); + }); }); diff --git a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts index 452d68aa92394..4069266697fc6 100644 --- a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts +++ b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts @@ -21,6 +21,12 @@ import { SavedObjectMigrationFn } from 'kibana/server'; import { get } from 'lodash'; import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common'; +/** + * This migration script is related to: + * @link https://github.com/elastic/kibana/pull/62194 + * @link https://github.com/elastic/kibana/pull/14644 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.8.2 + */ export const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); @@ -31,6 +37,7 @@ export const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { searchSource = JSON.parse(searchSourceJSON); } catch (e) { // Let it go, the data is invalid and we'll leave it as is + return doc; } if (searchSource.query?.match_all) { diff --git a/src/plugins/discover/server/saved_objects/search_migrations.test.ts b/src/plugins/discover/server/saved_objects/search_migrations.test.ts index babd25c03dbb2..7119a9c5ec6ab 100644 --- a/src/plugins/discover/server/saved_objects/search_migrations.test.ts +++ b/src/plugins/discover/server/saved_objects/search_migrations.test.ts @@ -23,38 +23,6 @@ import { searchMigrations } from './search_migrations'; const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext; describe('migration search', () => { - describe('6.7.2', () => { - const migrationFn = searchMigrations['6.7.2']; - - it('should migrate obsolete match_all query', () => { - const migratedDoc = migrationFn( - { - type: 'search', - attributes: { - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - query: { - match_all: {}, - }, - }), - }, - }, - }, - savedObjectMigrationContext - ); - const migratedSearchSource = JSON.parse( - migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON - ); - - expect(migratedSearchSource).toEqual({ - query: { - query: '', - language: 'kuery', - }, - }); - }); - }); - describe('7.0.0', () => { const migrationFn = searchMigrations['7.0.0']; @@ -328,4 +296,55 @@ Object { expect(migratedDoc).toEqual(doc); }); }); + + describe('7.8.2', () => { + const migrationFn = searchMigrations['7.8.2']; + + it('should migrate obsolete match_all query', () => { + const migratedDoc = migrationFn( + { + type: 'search', + attributes: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + query: { + match_all: {}, + }, + }), + }, + }, + }, + savedObjectMigrationContext + ); + const migratedSearchSource = JSON.parse( + migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON + ); + + expect(migratedSearchSource).toEqual({ + query: { + query: '', + language: 'kuery', + }, + }); + }); + + it('should return original doc if searchSourceJSON cannot be parsed', () => { + const migratedDoc = migrationFn( + { + type: 'search', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }, + savedObjectMigrationContext + ); + + expect(migratedDoc).toEqual({ + type: 'search', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }); + }); + }); }); diff --git a/src/plugins/discover/server/saved_objects/search_migrations.ts b/src/plugins/discover/server/saved_objects/search_migrations.ts index 0302159c43c56..6d1b9b54c7cac 100644 --- a/src/plugins/discover/server/saved_objects/search_migrations.ts +++ b/src/plugins/discover/server/saved_objects/search_migrations.ts @@ -21,6 +21,12 @@ import { flow, get } from 'lodash'; import { SavedObjectMigrationFn } from 'kibana/server'; import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common'; +/** + * This migration script is related to: + * @link https://github.com/elastic/kibana/pull/62194 + * @link https://github.com/elastic/kibana/pull/14644 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.8.2 + */ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); @@ -31,6 +37,7 @@ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { searchSource = JSON.parse(searchSourceJSON); } catch (e) { // Let it go, the data is invalid and we'll leave it as is + return doc; } if (searchSource.query?.match_all) { @@ -122,7 +129,7 @@ const migrateSearchSortToNestedArray: SavedObjectMigrationFn = (doc) = }; export const searchMigrations = { - '6.7.2': flow(migrateMatchAllQuery), '7.0.0': flow(setNewReferences), '7.4.0': flow(migrateSearchSortToNestedArray), + '7.8.2': flow(migrateMatchAllQuery), }; diff --git a/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts b/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts index dc8bae69ca377..501e4c259fc6d 100644 --- a/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts +++ b/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts @@ -150,32 +150,6 @@ describe('migration visualization', () => { expect(aggs[3]).not.toHaveProperty('params.customBucket.params.time_zone'); expect(aggs[2]).not.toHaveProperty('params.time_zone'); }); - - it('should migrate obsolete match_all query', () => { - const migratedDoc = migrate({ - ...doc, - attributes: { - ...doc.attributes, - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - query: { - match_all: {}, - }, - }), - }, - }, - }); - const migratedSearchSource = JSON.parse( - migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON - ); - - expect(migratedSearchSource).toEqual({ - query: { - query: '', - language: 'kuery', - }, - }); - }); }); }); @@ -1487,6 +1461,57 @@ describe('migration visualization', () => { }); }); + describe('7.8.2', () => { + const migrationFn = visualizationSavedObjectTypeMigrations['7.8.2']; + + it('should migrate obsolete match_all query', () => { + const migratedDoc = migrationFn( + { + type: 'area', + attributes: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + query: { + match_all: {}, + }, + }), + }, + }, + }, + savedObjectMigrationContext + ); + const migratedSearchSource = JSON.parse( + migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON + ); + + expect(migratedSearchSource).toEqual({ + query: { + query: '', + language: 'kuery', + }, + }); + }); + + it('should return original doc if searchSourceJSON cannot be parsed', () => { + const migratedDoc = migrationFn( + { + type: 'area', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }, + savedObjectMigrationContext + ); + + expect(migratedDoc).toEqual({ + type: 'area', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }); + }); + }); + describe('7.8.0 tsvb split_color_mode', () => { const migrate = (doc: any) => visualizationSavedObjectTypeMigrations['7.8.0']( diff --git a/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts b/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts index 170d7c460b06a..54215a26b4162 100644 --- a/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts +++ b/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts @@ -655,6 +655,12 @@ const migrateTableSplits: SavedObjectMigrationFn = (doc) => { } }; +/** + * This migration script is related to: + * @link https://github.com/elastic/kibana/pull/62194 + * @link https://github.com/elastic/kibana/pull/14644 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.8.2 + */ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); @@ -665,6 +671,7 @@ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { searchSource = JSON.parse(searchSourceJSON); } catch (e) { // Let it go, the data is invalid and we'll leave it as is + return doc; } if (searchSource.query?.match_all) { @@ -761,7 +768,7 @@ export const visualizationSavedObjectTypeMigrations = { * in that version. So we apply this twice, once with 6.7.2 and once with 7.0.1 while the backport to 6.7 * only contained the 6.7.2 migration and not the 7.0.1 migration. */ - '6.7.2': flow(migrateMatchAllQuery, removeDateHistogramTimeZones), + '6.7.2': flow(removeDateHistogramTimeZones), '7.0.0': flow( addDocReferences, migrateIndexPattern, @@ -781,5 +788,6 @@ export const visualizationSavedObjectTypeMigrations = { '7.4.2': flow(transformSplitFiltersStringToQueryObject), '7.7.0': flow(migrateOperatorKeyTypo, migrateSplitByChartRow), '7.8.0': flow(migrateTsvbDefaultColorPalettes), + '7.8.2': flow(migrateMatchAllQuery), '7.10.0': flow(migrateFilterRatioQuery, removeTSVBSearchSource), }; From b0e1ac79ef5de8c52dd938392c8ddb27124387b9 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Mon, 28 Sep 2020 13:50:44 +0300 Subject: [PATCH 2/3] 7.8.2 -> 7.9.3 --- .../dashboard/server/saved_objects/dashboard_migrations.ts | 2 +- .../server/saved_objects/migrate_match_all_query.ts | 3 ++- .../discover/server/saved_objects/search_migrations.test.ts | 4 ++-- .../discover/server/saved_objects/search_migrations.ts | 5 +++-- .../server/saved_objects/visualization_migrations.test.ts | 4 ++-- .../server/saved_objects/visualization_migrations.ts | 5 +++-- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts index a137b274185c3..b6e6d2c24b7fc 100644 --- a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts +++ b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts @@ -103,5 +103,5 @@ const migrations700: SavedObjectMigrationFn = (doc): DashboardDoc700To export const dashboardSavedObjectTypeMigrations = { '7.0.0': flow(migrations700), '7.3.0': flow(migrations730), - '7.8.2': flow(migrateMatchAllQuery), + '7.9.3': flow(migrateMatchAllQuery), }; diff --git a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts index 4069266697fc6..d517af0dd03c3 100644 --- a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts +++ b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts @@ -25,7 +25,8 @@ import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common'; * This migration script is related to: * @link https://github.com/elastic/kibana/pull/62194 * @link https://github.com/elastic/kibana/pull/14644 - * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.8.2 + * Important: migration scripts was moved from 6.7.2 -> 7.9.3 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.9.3 */ export const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); diff --git a/src/plugins/discover/server/saved_objects/search_migrations.test.ts b/src/plugins/discover/server/saved_objects/search_migrations.test.ts index 7119a9c5ec6ab..089d201afe94d 100644 --- a/src/plugins/discover/server/saved_objects/search_migrations.test.ts +++ b/src/plugins/discover/server/saved_objects/search_migrations.test.ts @@ -297,8 +297,8 @@ Object { }); }); - describe('7.8.2', () => { - const migrationFn = searchMigrations['7.8.2']; + describe('7.9.3', () => { + const migrationFn = searchMigrations['7.9.3']; it('should migrate obsolete match_all query', () => { const migratedDoc = migrationFn( diff --git a/src/plugins/discover/server/saved_objects/search_migrations.ts b/src/plugins/discover/server/saved_objects/search_migrations.ts index 6d1b9b54c7cac..3851571dbdff5 100644 --- a/src/plugins/discover/server/saved_objects/search_migrations.ts +++ b/src/plugins/discover/server/saved_objects/search_migrations.ts @@ -25,7 +25,8 @@ import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common'; * This migration script is related to: * @link https://github.com/elastic/kibana/pull/62194 * @link https://github.com/elastic/kibana/pull/14644 - * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.8.2 + * Important: migration scripts was moved from 6.7.2 -> 7.9.3 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.9.3 */ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); @@ -131,5 +132,5 @@ const migrateSearchSortToNestedArray: SavedObjectMigrationFn = (doc) = export const searchMigrations = { '7.0.0': flow(setNewReferences), '7.4.0': flow(migrateSearchSortToNestedArray), - '7.8.2': flow(migrateMatchAllQuery), + '7.9.3': flow(migrateMatchAllQuery), }; diff --git a/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts b/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts index 501e4c259fc6d..508c54a6249e2 100644 --- a/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts +++ b/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts @@ -1461,8 +1461,8 @@ describe('migration visualization', () => { }); }); - describe('7.8.2', () => { - const migrationFn = visualizationSavedObjectTypeMigrations['7.8.2']; + describe('7.9.3', () => { + const migrationFn = visualizationSavedObjectTypeMigrations['7.9.3']; it('should migrate obsolete match_all query', () => { const migratedDoc = migrationFn( diff --git a/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts b/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts index 54215a26b4162..287c863220435 100644 --- a/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts +++ b/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts @@ -659,7 +659,8 @@ const migrateTableSplits: SavedObjectMigrationFn = (doc) => { * This migration script is related to: * @link https://github.com/elastic/kibana/pull/62194 * @link https://github.com/elastic/kibana/pull/14644 - * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.8.2 + * Important: migration scripts was moved from 6.7.2 -> 7.9.3 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.9.3 */ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); @@ -788,6 +789,6 @@ export const visualizationSavedObjectTypeMigrations = { '7.4.2': flow(transformSplitFiltersStringToQueryObject), '7.7.0': flow(migrateOperatorKeyTypo, migrateSplitByChartRow), '7.8.0': flow(migrateTsvbDefaultColorPalettes), - '7.8.2': flow(migrateMatchAllQuery), + '7.9.3': flow(migrateMatchAllQuery), '7.10.0': flow(migrateFilterRatioQuery, removeTSVBSearchSource), }; From e4da267a1c9b3f3647d04bdea29b6e6722214c26 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Wed, 30 Sep 2020 13:03:31 +0300 Subject: [PATCH 3/3] return migration into 6.7.2 --- .../saved_objects/dashboard_migrations.ts | 11 ++ .../saved_objects/migrate_match_all_query.ts | 3 +- .../saved_objects/search_migrations.test.ts | 104 ++++++++++-------- .../server/saved_objects/search_migrations.ts | 4 +- .../visualization_migrations.test.ts | 97 ++++++++-------- .../saved_objects/visualization_migrations.ts | 5 +- 6 files changed, 128 insertions(+), 96 deletions(-) diff --git a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts index b6e6d2c24b7fc..ac91c5a92048a 100644 --- a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts +++ b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts @@ -101,6 +101,17 @@ const migrations700: SavedObjectMigrationFn = (doc): DashboardDoc700To }; export const dashboardSavedObjectTypeMigrations = { + /** + * We need to have this migration twice, once with a version prior to 7.0.0 once with a version + * after it. The reason for that is, that this migration has been introduced once 7.0.0 was already + * released. Thus a user who already had 7.0.0 installed already got the 7.0.0 migrations below running, + * so we need a version higher than that. But this fix was backported to the 6.7 release, meaning if we + * would only have the 7.0.1 migration in here a user on the 6.7 release will migrate their saved objects + * to the 7.0.1 state, and thus when updating their Kibana to 7.0, will never run the 7.0.0 migrations introduced + * in that version. So we apply this twice, once with 6.7.2 and once with 7.0.1 while the backport to 6.7 + * only contained the 6.7.2 migration and not the 7.0.1 migration. + */ + '6.7.2': flow(migrateMatchAllQuery), '7.0.0': flow(migrations700), '7.3.0': flow(migrations730), '7.9.3': flow(migrateMatchAllQuery), diff --git a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts index d517af0dd03c3..3d7aadab5602c 100644 --- a/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts +++ b/src/plugins/dashboard/server/saved_objects/migrate_match_all_query.ts @@ -25,8 +25,7 @@ import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common'; * This migration script is related to: * @link https://github.com/elastic/kibana/pull/62194 * @link https://github.com/elastic/kibana/pull/14644 - * Important: migration scripts was moved from 6.7.2 -> 7.9.3 - * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.9.3 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated we should execute it twice in 6.7.2 and 7.9.3 */ export const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); diff --git a/src/plugins/discover/server/saved_objects/search_migrations.test.ts b/src/plugins/discover/server/saved_objects/search_migrations.test.ts index 089d201afe94d..3324a2d93f5ad 100644 --- a/src/plugins/discover/server/saved_objects/search_migrations.test.ts +++ b/src/plugins/discover/server/saved_objects/search_migrations.test.ts @@ -22,7 +22,64 @@ import { searchMigrations } from './search_migrations'; const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext; +const testMigrateMatchAllQuery = (migrationFn: Function) => { + it('should migrate obsolete match_all query', () => { + const migratedDoc = migrationFn( + { + type: 'search', + attributes: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + query: { + match_all: {}, + }, + }), + }, + }, + }, + savedObjectMigrationContext + ); + const migratedSearchSource = JSON.parse( + migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON + ); + + expect(migratedSearchSource).toEqual({ + query: { + query: '', + language: 'kuery', + }, + }); + }); + + it('should return original doc if searchSourceJSON cannot be parsed', () => { + const migratedDoc = migrationFn( + { + type: 'search', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }, + savedObjectMigrationContext + ); + + expect(migratedDoc).toEqual({ + type: 'search', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }); + }); +}; + describe('migration search', () => { + describe('6.7.2', () => { + const migrationFn = searchMigrations['6.7.2']; + + describe('migrateMatchAllQuery', () => { + testMigrateMatchAllQuery(migrationFn); + }); + }); + describe('7.0.0', () => { const migrationFn = searchMigrations['7.0.0']; @@ -300,51 +357,8 @@ Object { describe('7.9.3', () => { const migrationFn = searchMigrations['7.9.3']; - it('should migrate obsolete match_all query', () => { - const migratedDoc = migrationFn( - { - type: 'search', - attributes: { - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - query: { - match_all: {}, - }, - }), - }, - }, - }, - savedObjectMigrationContext - ); - const migratedSearchSource = JSON.parse( - migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON - ); - - expect(migratedSearchSource).toEqual({ - query: { - query: '', - language: 'kuery', - }, - }); - }); - - it('should return original doc if searchSourceJSON cannot be parsed', () => { - const migratedDoc = migrationFn( - { - type: 'search', - attributes: { - kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', - }, - }, - savedObjectMigrationContext - ); - - expect(migratedDoc).toEqual({ - type: 'search', - attributes: { - kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', - }, - }); + describe('migrateMatchAllQuery', () => { + testMigrateMatchAllQuery(migrationFn); }); }); }); diff --git a/src/plugins/discover/server/saved_objects/search_migrations.ts b/src/plugins/discover/server/saved_objects/search_migrations.ts index 3851571dbdff5..fdb086bd17a2d 100644 --- a/src/plugins/discover/server/saved_objects/search_migrations.ts +++ b/src/plugins/discover/server/saved_objects/search_migrations.ts @@ -25,8 +25,7 @@ import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common'; * This migration script is related to: * @link https://github.com/elastic/kibana/pull/62194 * @link https://github.com/elastic/kibana/pull/14644 - * Important: migration scripts was moved from 6.7.2 -> 7.9.3 - * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.9.3 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated we should execute it twice in 6.7.2 and 7.9.3 */ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); @@ -130,6 +129,7 @@ const migrateSearchSortToNestedArray: SavedObjectMigrationFn = (doc) = }; export const searchMigrations = { + '6.7.2': flow(migrateMatchAllQuery), '7.0.0': flow(setNewReferences), '7.4.0': flow(migrateSearchSortToNestedArray), '7.9.3': flow(migrateMatchAllQuery), diff --git a/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts b/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts index 508c54a6249e2..c4ee92194ec36 100644 --- a/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts +++ b/src/plugins/visualizations/server/saved_objects/visualization_migrations.test.ts @@ -22,6 +22,50 @@ import { SavedObjectMigrationContext, SavedObjectMigrationFn } from 'kibana/serv const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext; +const testMigrateMatchAllQuery = (migrate: Function) => { + it('should migrate obsolete match_all query', () => { + const migratedDoc = migrate({ + type: 'area', + attributes: { + kibanaSavedObjectMeta: { + searchSourceJSON: JSON.stringify({ + query: { + match_all: {}, + }, + }), + }, + }, + }); + + const migratedSearchSource = JSON.parse( + migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON + ); + + expect(migratedSearchSource).toEqual({ + query: { + query: '', + language: 'kuery', + }, + }); + }); + + it('should return original doc if searchSourceJSON cannot be parsed', () => { + const migratedDoc = migrate({ + type: 'area', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }); + + expect(migratedDoc).toEqual({ + type: 'area', + attributes: { + kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', + }, + }); + }); +}; + describe('migration visualization', () => { describe('6.7.2', () => { const migrate = (doc: any) => @@ -31,6 +75,10 @@ describe('migration visualization', () => { ); let doc: any; + describe('migrateMatchAllQuery', () => { + testMigrateMatchAllQuery(migrate); + }); + describe('date histogram time zone removal', () => { beforeEach(() => { doc = { @@ -1462,53 +1510,14 @@ describe('migration visualization', () => { }); describe('7.9.3', () => { - const migrationFn = visualizationSavedObjectTypeMigrations['7.9.3']; - - it('should migrate obsolete match_all query', () => { - const migratedDoc = migrationFn( - { - type: 'area', - attributes: { - kibanaSavedObjectMeta: { - searchSourceJSON: JSON.stringify({ - query: { - match_all: {}, - }, - }), - }, - }, - }, - savedObjectMigrationContext - ); - const migratedSearchSource = JSON.parse( - migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON - ); - - expect(migratedSearchSource).toEqual({ - query: { - query: '', - language: 'kuery', - }, - }); - }); - - it('should return original doc if searchSourceJSON cannot be parsed', () => { - const migratedDoc = migrationFn( - { - type: 'area', - attributes: { - kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', - }, - }, + const migrate = (doc: any) => + visualizationSavedObjectTypeMigrations['7.9.3']( + doc as Parameters[0], savedObjectMigrationContext ); - expect(migratedDoc).toEqual({ - type: 'area', - attributes: { - kibanaSavedObjectMeta: 'kibanaSavedObjectMeta', - }, - }); + describe('migrateMatchAllQuery', () => { + testMigrateMatchAllQuery(migrate); }); }); diff --git a/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts b/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts index 287c863220435..fbeefacf6035f 100644 --- a/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts +++ b/src/plugins/visualizations/server/saved_objects/visualization_migrations.ts @@ -659,8 +659,7 @@ const migrateTableSplits: SavedObjectMigrationFn = (doc) => { * This migration script is related to: * @link https://github.com/elastic/kibana/pull/62194 * @link https://github.com/elastic/kibana/pull/14644 - * Important: migration scripts was moved from 6.7.2 -> 7.9.3 - * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated that script was added into 7.9.3 + * This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated we should execute it twice in 6.7.2 and 7.9.3 */ const migrateMatchAllQuery: SavedObjectMigrationFn = (doc) => { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); @@ -769,7 +768,7 @@ export const visualizationSavedObjectTypeMigrations = { * in that version. So we apply this twice, once with 6.7.2 and once with 7.0.1 while the backport to 6.7 * only contained the 6.7.2 migration and not the 7.0.1 migration. */ - '6.7.2': flow(removeDateHistogramTimeZones), + '6.7.2': flow(migrateMatchAllQuery, removeDateHistogramTimeZones), '7.0.0': flow( addDocReferences, migrateIndexPattern,