Skip to content

Commit

Permalink
add migrations for searh savedobject type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Apr 4, 2020
1 parent 4e2f1f0 commit e76fd18
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/plugins/data/server/saved_objects/search_migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ import { searchSavedObjectTypeMigrations } from './search_migrations';
const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext;

describe('migration search', () => {
describe('6.7.2', () => {
const migrationFn = searchSavedObjectTypeMigrations['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 = searchSavedObjectTypeMigrations['7.0.0'];

Expand Down
36 changes: 36 additions & 0 deletions src/plugins/data/server/saved_objects/search_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@

import { flow, get } from 'lodash';
import { SavedObjectMigrationFn } from 'kibana/server';
import { DEFAULT_QUERY_LANGUAGE } from '../../common';

const migrateMatchAllQuery: SavedObjectMigrationFn = doc => {
const searchSourceJSON = get<string>(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');

if (searchSourceJSON) {
let searchSource: any;

try {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}

if (searchSource.query?.match_all) {
return {
...doc,
attributes: {
...doc.attributes,
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
...searchSource,
query: {
query: '',
language: DEFAULT_QUERY_LANGUAGE,
},
}),
},
},
};
}
}

return doc;
};

const migrateIndexPattern: SavedObjectMigrationFn = doc => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');
Expand Down Expand Up @@ -87,6 +122,7 @@ const migrateSearchSortToNestedArray: SavedObjectMigrationFn = doc => {
};

export const searchSavedObjectTypeMigrations = {
'6.7.2': flow<SavedObjectMigrationFn>(migrateMatchAllQuery),
'7.0.0': flow<SavedObjectMigrationFn>(setNewReferences),
'7.4.0': flow<SavedObjectMigrationFn>(migrateSearchSortToNestedArray),
};

0 comments on commit e76fd18

Please sign in to comment.