Skip to content

Commit

Permalink
[Canvas] Migrate saved object mappings and migrations to Kibana Platf…
Browse files Browse the repository at this point in the history
…orm (elastic#58891)

* Move saved object mappings and migratins to kibana platform

* Remove ts-ignore

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Corey Robertson and elasticmachine committed Apr 16, 2020
1 parent c365b0c commit 6f76eaf
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 71 deletions.
4 changes: 0 additions & 4 deletions x-pack/legacy/plugins/canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import { resolve } from 'path';
import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/utils';
import { init } from './init';
import { mappings } from './server/mappings';
import { CANVAS_APP, CANVAS_TYPE, CUSTOM_ELEMENT_TYPE } from './common/lib';
import { migrations } from './migrations';

export function canvas(kibana) {
return new kibana.Plugin({
Expand All @@ -33,8 +31,6 @@ export function canvas(kibana) {
'plugins/canvas/lib/window_error_handler.js',
],
home: ['plugins/canvas/legacy_register_feature'],
mappings,
migrations,
savedObjectsManagement: {
[CANVAS_TYPE]: {
icon: 'canvasApp',
Expand Down
37 changes: 0 additions & 37 deletions x-pack/legacy/plugins/canvas/migrations.test.js

This file was deleted.

4 changes: 4 additions & 0 deletions x-pack/plugins/canvas/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { initRoutes } from './routes';
import { registerCanvasUsageCollector } from './collectors';
import { loadSampleData } from './sample_data';
import { setupInterpreter } from './setup_interpreter';
import { customElementType, workpadType } from './saved_objects';

interface PluginsSetup {
expressions: ExpressionsServerSetup;
Expand All @@ -29,6 +30,9 @@ export class CanvasPlugin implements Plugin {
}

public async setup(coreSetup: CoreSetup, plugins: PluginsSetup) {
coreSetup.savedObjects.registerType(customElementType);
coreSetup.savedObjects.registerType(workpadType);

plugins.features.registerFeature({
id: 'canvas',
name: 'Canvas',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

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

export const mappings = {
[CANVAS_TYPE]: {
dynamic: false,
properties: {
name: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
},
},
},
'@timestamp': { type: 'date' },
'@created': { type: 'date' },
},
},
[CUSTOM_ELEMENT_TYPE]: {
export const customElementType: SavedObjectsType = {
name: CUSTOM_ELEMENT_TYPE,
hidden: false,
namespaceAgnostic: false,
mappings: {
dynamic: false,
properties: {
name: {
Expand All @@ -41,4 +29,5 @@ export const mappings = {
'@created': { type: 'date' },
},
},
migrations: {},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { CANVAS_TYPE } from './common/lib';
import { workpadType } from './workpad';
import { customElementType } from './custom_element';

export const migrations = {
[CANVAS_TYPE]: {
'7.0.0': doc => {
if (doc.attributes) {
delete doc.attributes.id;
}
return doc;
},
},
};
export { customElementType, workpadType };
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { removeAttributesId } from './remove_attributes_id';

const context: any = {
log: jest.fn(),
};

describe(`removeAttributesId`, () => {
it('does not throw error on empty object', () => {
const migratedDoc = removeAttributesId({} as any, context);
expect(migratedDoc).toMatchInlineSnapshot(`Object {}`);
});

it('removes id from "attributes"', () => {
const migratedDoc = removeAttributesId(
{
foo: true,
attributes: {
id: '123',
bar: true,
},
} as any,
context
);
expect(migratedDoc).toMatchInlineSnapshot(`
Object {
"attributes": Object {
"bar": true,
},
"foo": true,
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectMigrationFn } from 'src/core/server';

export const removeAttributesId: SavedObjectMigrationFn = doc => {
if (typeof doc.attributes === 'object' && doc.attributes !== null) {
delete (doc.attributes as any).id;
}
return doc;
};
33 changes: 33 additions & 0 deletions x-pack/plugins/canvas/server/saved_objects/workpad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsType } from 'src/core/server';
import { CANVAS_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants';
import { removeAttributesId } from './migrations/remove_attributes_id';

export const workpadType: SavedObjectsType = {
name: CANVAS_TYPE,
hidden: false,
namespaceAgnostic: false,
mappings: {
dynamic: false,
properties: {
name: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
},
},
},
'@timestamp': { type: 'date' },
'@created': { type: 'date' },
},
},
migrations: {
'7.0.0': removeAttributesId,
},
};

0 comments on commit 6f76eaf

Please sign in to comment.