diff --git a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.test.ts b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.test.ts index e7f84ad1fe518..9829498118cc0 100644 --- a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.test.ts +++ b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.test.ts @@ -17,6 +17,7 @@ * under the License. */ +import { SavedObjectUnsanitizedDoc } from 'kibana/server'; import { dashboardSavedObjectTypeMigrations as migrations } from './dashboard_migrations'; describe('dashboard', () => { @@ -24,7 +25,7 @@ describe('dashboard', () => { const migration = migrations['7.0.0']; test('skips error on empty object', () => { - expect(migration({})).toMatchInlineSnapshot(` + expect(migration({} as SavedObjectUnsanitizedDoc)).toMatchInlineSnapshot(` Object { "references": Array [], } @@ -329,7 +330,7 @@ Object { attributes: { panelsJSON: 123, }, - }; + } as SavedObjectUnsanitizedDoc; expect(migration(doc)).toMatchInlineSnapshot(` Object { "attributes": Object { @@ -347,7 +348,7 @@ Object { attributes: { panelsJSON: '{123abc}', }, - }; + } as SavedObjectUnsanitizedDoc; expect(migration(doc)).toMatchInlineSnapshot(` Object { "attributes": Object { @@ -365,7 +366,7 @@ Object { attributes: { panelsJSON: '{}', }, - }; + } as SavedObjectUnsanitizedDoc; expect(migration(doc)).toMatchInlineSnapshot(` Object { "attributes": Object { @@ -383,7 +384,7 @@ Object { attributes: { panelsJSON: '[{"id":"123"}]', }, - }; + } as SavedObjectUnsanitizedDoc; expect(migration(doc)).toMatchInlineSnapshot(` Object { "attributes": Object { @@ -401,7 +402,7 @@ Object { attributes: { panelsJSON: '[{"type":"visualization"}]', }, - }; + } as SavedObjectUnsanitizedDoc; expect(migration(doc)).toMatchInlineSnapshot(` Object { "attributes": Object { @@ -420,7 +421,7 @@ Object { panelsJSON: '[{"id":"1","type":"visualization","foo":true},{"id":"2","type":"visualization","bar":true}]', }, - }; + } as SavedObjectUnsanitizedDoc; const migratedDoc = migration(doc); expect(migratedDoc).toMatchInlineSnapshot(` Object { diff --git a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts index e5288afd81dce..8ebaf7f5bea94 100644 --- a/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts +++ b/src/plugins/dashboard/server/saved_objects/dashboard_migrations.ts @@ -19,25 +19,26 @@ import { get, flow } from 'lodash'; -import { SavedObjectMigrationFn } from 'kibana/server'; +import { + SavedObjectMigrationFn, + SavedObjectUnsanitizedDoc, + SavedObjectMigrationContext, +} from 'kibana/server'; import { migrations730 } from './migrations_730'; import { migrateMatchAllQuery } from './migrate_match_all_query'; +import { DashboardDoc700To720 } from '../../common'; -const migrations700: SavedObjectMigrationFn = doc => { - // Set new "references" attribute - doc.references = doc.references || []; - - // Migrate index pattern +function migrateIndexPattern(doc: DashboardDoc700To720) { const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON'); if (typeof searchSourceJSON !== 'string') { - return doc; + return; } let searchSource; try { searchSource = JSON.parse(searchSourceJSON); } catch (e) { // Let it go, the data is invalid and we'll leave it as is - return doc; + return; } if (searchSource.index) { searchSource.indexRefName = 'kibanaSavedObjectMeta.searchSourceJSON.index'; @@ -63,28 +64,35 @@ const migrations700: SavedObjectMigrationFn = doc => { }); } doc.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(searchSource); +} +const migrations700: SavedObjectMigrationFn = (doc): DashboardDoc700To720 => { + // Set new "references" attribute + doc.references = doc.references || []; + + // Migrate index pattern + migrateIndexPattern(doc as DashboardDoc700To720); // Migrate panels const panelsJSON = get(doc, 'attributes.panelsJSON'); if (typeof panelsJSON !== 'string') { - return doc; + return doc as DashboardDoc700To720; } let panels; try { panels = JSON.parse(panelsJSON); } catch (e) { // Let it go, the data is invalid and we'll leave it as is - return doc; + return doc as DashboardDoc700To720; } if (!Array.isArray(panels)) { - return doc; + return doc as DashboardDoc700To720; } panels.forEach((panel, i) => { if (!panel.type || !panel.id) { return; } panel.panelRefName = `panel_${i}`; - doc.references.push({ + doc.references!.push({ name: `panel_${i}`, type: panel.type, id: panel.id, @@ -93,7 +101,7 @@ const migrations700: SavedObjectMigrationFn = doc => { delete panel.id; }); doc.attributes.panelsJSON = JSON.stringify(panels); - return doc; + return doc as DashboardDoc700To720; }; export const dashboardSavedObjectTypeMigrations = { @@ -108,6 +116,11 @@ export const dashboardSavedObjectTypeMigrations = { * 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.0.0': flow<(doc: SavedObjectUnsanitizedDoc) => DashboardDoc700To720>(migrations700), + '7.3.0': flow< + ( + doc: SavedObjectUnsanitizedDoc, + context: SavedObjectMigrationContext + ) => SavedObjectUnsanitizedDoc + >(migrations730), }; diff --git a/src/plugins/dashboard/server/saved_objects/migrations_730.test.ts b/src/plugins/dashboard/server/saved_objects/migrations_730.test.ts index 885c70e5b4235..aa744324428a4 100644 --- a/src/plugins/dashboard/server/saved_objects/migrations_730.test.ts +++ b/src/plugins/dashboard/server/saved_objects/migrations_730.test.ts @@ -95,8 +95,8 @@ test('dashboard migration 7.3.0 migrates filters to query on search source when }, }; - const doc700: DashboardDoc700To720 = migrations.dashboard['7.0.0'](doc, mockContext); - const newDoc = migrations.dashboard['7.3.0'](doc700, mockContext); + const doc700: DashboardDoc700To720 = migrations['7.0.0'](doc); + const newDoc = migrations['7.3.0'](doc700, mockContext); const parsedSearchSource = JSON.parse(newDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON); expect(parsedSearchSource.filter.length).toBe(0); @@ -127,8 +127,8 @@ test('dashboard migration works when panelsJSON is missing panelIndex', () => { }, }; - const doc700: DashboardDoc700To720 = migrations.dashboard['7.0.0'](doc, mockContext); - const newDoc = migrations.dashboard['7.3.0'](doc700, mockContext); + const doc700: DashboardDoc700To720 = migrations['7.0.0'](doc); + const newDoc = migrations['7.3.0'](doc700, mockContext); const parsedSearchSource = JSON.parse(newDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON); expect(parsedSearchSource.filter.length).toBe(0); diff --git a/src/plugins/dashboard/server/saved_objects/migrations_730.ts b/src/plugins/dashboard/server/saved_objects/migrations_730.ts index 37de816b83291..e9d483f68a5da 100644 --- a/src/plugins/dashboard/server/saved_objects/migrations_730.ts +++ b/src/plugins/dashboard/server/saved_objects/migrations_730.ts @@ -18,13 +18,13 @@ */ import { inspect } from 'util'; -import { SavedObjectMigrationFn } from 'kibana/server'; +import { SavedObjectMigrationContext } from 'kibana/server'; import { DashboardDoc730ToLatest } from '../../common'; import { isDashboardDoc } from './is_dashboard_doc'; import { moveFiltersToQuery } from './move_filters_to_query'; -import { migratePanelsTo730 } from '../../common/migrate_to_730_panels'; +import { migratePanelsTo730, DashboardDoc700To720 } from '../../common'; -export const migrations730: SavedObjectMigrationFn = (doc, { log }) => { +export const migrations730 = (doc: DashboardDoc700To720, { log }: SavedObjectMigrationContext) => { if (!isDashboardDoc(doc)) { // NOTE: we should probably throw an error here... but for now following suit and in the // case of errors, just returning the same document.