Skip to content

Commit c58f7f5

Browse files
Corey Robertsonelasticmachine
andauthored
[7.x] [Canvas] Migrate saved object mappings and migrations to Kibana Platform (#58891) (#63704)
* [Canvas] Migrate saved object mappings and migrations to Kibana Platform (#58891) * Move saved object mappings and migratins to kibana platform * Remove ts-ignore Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> * Fix types Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent f8563f8 commit c58f7f5

File tree

8 files changed

+101
-71
lines changed

8 files changed

+101
-71
lines changed

x-pack/legacy/plugins/canvas/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import { resolve } from 'path';
88
import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/utils';
99
import { init } from './init';
10-
import { mappings } from './server/mappings';
1110
import { CANVAS_APP, CANVAS_TYPE, CUSTOM_ELEMENT_TYPE } from './common/lib';
12-
import { migrations } from './migrations';
1311

1412
export function canvas(kibana) {
1513
return new kibana.Plugin({
@@ -33,8 +31,6 @@ export function canvas(kibana) {
3331
'plugins/canvas/lib/window_error_handler.js',
3432
],
3533
home: ['plugins/canvas/legacy_register_feature'],
36-
mappings,
37-
migrations,
3834
savedObjectsManagement: {
3935
[CANVAS_TYPE]: {
4036
icon: 'canvasApp',

x-pack/legacy/plugins/canvas/migrations.test.js

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

x-pack/plugins/canvas/server/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { initRoutes } from './routes';
1414
import { registerCanvasUsageCollector } from './collectors';
1515
import { loadSampleData } from './sample_data';
1616
import { setupInterpreter } from './setup_interpreter';
17+
import { customElementType, workpadType } from './saved_objects';
1718

1819
interface PluginsSetup {
1920
expressions: ExpressionsServerSetup;
@@ -29,6 +30,9 @@ export class CanvasPlugin implements Plugin {
2930
}
3031

3132
public async setup(coreSetup: CoreSetup, plugins: PluginsSetup) {
33+
coreSetup.savedObjects.registerType(customElementType);
34+
coreSetup.savedObjects.registerType(workpadType);
35+
3236
plugins.features.registerFeature({
3337
id: 'canvas',
3438
name: 'Canvas',

x-pack/legacy/plugins/canvas/server/mappings.ts renamed to x-pack/plugins/canvas/server/saved_objects/custom_element.ts

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

7-
// @ts-ignore converting /libs/constants to TS breaks CI
8-
import { CANVAS_TYPE, CUSTOM_ELEMENT_TYPE } from '../common/lib/constants';
7+
import { SavedObjectsType } from 'src/core/server';
8+
import { CUSTOM_ELEMENT_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants';
99

10-
export const mappings = {
11-
[CANVAS_TYPE]: {
12-
dynamic: false,
13-
properties: {
14-
name: {
15-
type: 'text',
16-
fields: {
17-
keyword: {
18-
type: 'keyword',
19-
},
20-
},
21-
},
22-
'@timestamp': { type: 'date' },
23-
'@created': { type: 'date' },
24-
},
25-
},
26-
[CUSTOM_ELEMENT_TYPE]: {
10+
export const customElementType: SavedObjectsType = {
11+
name: CUSTOM_ELEMENT_TYPE,
12+
hidden: false,
13+
namespaceType: 'single',
14+
mappings: {
2715
dynamic: false,
2816
properties: {
2917
name: {
@@ -41,4 +29,5 @@ export const mappings = {
4129
'@created': { type: 'date' },
4230
},
4331
},
32+
migrations: {},
4433
};

x-pack/legacy/plugins/canvas/migrations.js renamed to x-pack/plugins/canvas/server/saved_objects/index.ts

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

7-
import { CANVAS_TYPE } from './common/lib';
7+
import { workpadType } from './workpad';
8+
import { customElementType } from './custom_element';
89

9-
export const migrations = {
10-
[CANVAS_TYPE]: {
11-
'7.0.0': doc => {
12-
if (doc.attributes) {
13-
delete doc.attributes.id;
14-
}
15-
return doc;
16-
},
17-
},
18-
};
10+
export { customElementType, workpadType };
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 { removeAttributesId } from './remove_attributes_id';
8+
9+
const context: any = {
10+
log: jest.fn(),
11+
};
12+
13+
describe(`removeAttributesId`, () => {
14+
it('does not throw error on empty object', () => {
15+
const migratedDoc = removeAttributesId({} as any, context);
16+
expect(migratedDoc).toMatchInlineSnapshot(`Object {}`);
17+
});
18+
19+
it('removes id from "attributes"', () => {
20+
const migratedDoc = removeAttributesId(
21+
{
22+
foo: true,
23+
attributes: {
24+
id: '123',
25+
bar: true,
26+
},
27+
} as any,
28+
context
29+
);
30+
expect(migratedDoc).toMatchInlineSnapshot(`
31+
Object {
32+
"attributes": Object {
33+
"bar": true,
34+
},
35+
"foo": true,
36+
}
37+
`);
38+
});
39+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 { SavedObjectMigrationFn } from 'src/core/server';
8+
9+
export const removeAttributesId: SavedObjectMigrationFn = doc => {
10+
if (typeof doc.attributes === 'object' && doc.attributes !== null) {
11+
delete (doc.attributes as any).id;
12+
}
13+
return doc;
14+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 { SavedObjectsType } from 'src/core/server';
8+
import { CANVAS_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants';
9+
import { removeAttributesId } from './migrations/remove_attributes_id';
10+
11+
export const workpadType: SavedObjectsType = {
12+
name: CANVAS_TYPE,
13+
hidden: false,
14+
namespaceType: 'single',
15+
mappings: {
16+
dynamic: false,
17+
properties: {
18+
name: {
19+
type: 'text',
20+
fields: {
21+
keyword: {
22+
type: 'keyword',
23+
},
24+
},
25+
},
26+
'@timestamp': { type: 'date' },
27+
'@created': { type: 'date' },
28+
},
29+
},
30+
migrations: {
31+
'7.0.0': removeAttributesId,
32+
},
33+
};

0 commit comments

Comments
 (0)