Skip to content

Commit c93edf6

Browse files
committed
Fix graph migrations
1 parent 3056b12 commit c93edf6

File tree

4 files changed

+50
-47
lines changed

4 files changed

+50
-47
lines changed

x-pack/plugins/graph/server/saved_objects/graph_workspace.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66
import { SavedObjectsType } from 'kibana/server';
7-
// @ts-ignore
8-
import { migrations } from './migrations';
7+
import { graphMigrations } from './migrations';
98

109
export const graphWorkspace: SavedObjectsType = {
1110
name: 'graph-workspace',
1211
namespaceType: 'single',
1312
hidden: false,
14-
migrations,
13+
migrations: graphMigrations,
1514
mappings: {
1615
properties: {
1716
description: {

x-pack/plugins/graph/server/saved_objects/migrations.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

x-pack/plugins/graph/server/saved_objects/migrations.test.js renamed to x-pack/plugins/graph/server/saved_objects/migrations.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import migrations from './migrations';
7+
import { graphMigrations } from './migrations';
8+
import { SavedObjectUnsanitizedDoc } from 'kibana/server';
89

910
describe('graph-workspace', () => {
1011
describe('7.0.0', () => {
11-
const migration = migrations['graph-workspace']['7.0.0'];
12+
const migration = graphMigrations['7.0.0'];
1213

1314
test('returns doc on empty object', () => {
14-
expect(migration({})).toMatchInlineSnapshot(`
15+
expect(migration({} as SavedObjectUnsanitizedDoc)).toMatchInlineSnapshot(`
1516
Object {
1617
"references": Array [],
1718
}
@@ -21,6 +22,7 @@ Object {
2122
test('returns doc when wsState is not a string', () => {
2223
const doc = {
2324
id: '1',
25+
type: 'graph-workspace',
2426
attributes: {
2527
wsState: true,
2628
},
@@ -39,6 +41,7 @@ Object {
3941
test('returns doc when wsState is not valid JSON', () => {
4042
const doc = {
4143
id: '1',
44+
type: 'graph-workspace',
4245
attributes: {
4346
wsState: '123abc',
4447
},
@@ -57,6 +60,7 @@ Object {
5760
test('returns doc when "indexPattern" is missing from wsState', () => {
5861
const doc = {
5962
id: '1',
63+
type: 'graph-workspace',
6064
attributes: {
6165
wsState: JSON.stringify(JSON.stringify({ foo: true })),
6266
},
@@ -75,6 +79,7 @@ Object {
7579
test('extract "indexPattern" attribute from doc', () => {
7680
const doc = {
7781
id: '1',
82+
type: 'graph-workspace',
7883
attributes: {
7984
wsState: JSON.stringify(JSON.stringify({ foo: true, indexPattern: 'pattern*' })),
8085
bar: true,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { get } from 'lodash';
8+
import { SavedObjectUnsanitizedDoc } from 'kibana/server';
9+
10+
export const graphMigrations = {
11+
'7.0.0': (doc: SavedObjectUnsanitizedDoc) => {
12+
// Set new "references" attribute
13+
doc.references = doc.references || [];
14+
// Migrate index pattern
15+
const wsState = get(doc, 'attributes.wsState');
16+
if (typeof wsState !== 'string') {
17+
return doc;
18+
}
19+
let state;
20+
try {
21+
state = JSON.parse(JSON.parse(wsState));
22+
} catch (e) {
23+
// Let it go, the data is invalid and we'll leave it as is
24+
return doc;
25+
}
26+
const { indexPattern } = state;
27+
if (!indexPattern) {
28+
return doc;
29+
}
30+
state.indexPatternRefName = 'indexPattern_0';
31+
delete state.indexPattern;
32+
doc.attributes.wsState = JSON.stringify(JSON.stringify(state));
33+
doc.references.push({
34+
name: 'indexPattern_0',
35+
type: 'index-pattern',
36+
id: indexPattern,
37+
});
38+
return doc;
39+
},
40+
};

0 commit comments

Comments
 (0)