-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
persistable state migrations (#103680)
- Loading branch information
Showing
26 changed files
with
470 additions
and
103 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
...r.embeddablesetup.getmigrationversions.md → ...erver.embeddablesetup.getallmigrations.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-embeddable-server](./kibana-plugin-plugins-embeddable-server.md) > [EmbeddableSetup](./kibana-plugin-plugins-embeddable-server.embeddablesetup.md) > [getMigrationVersions](./kibana-plugin-plugins-embeddable-server.embeddablesetup.getmigrationversions.md) | ||
[Home](./index.md) > [kibana-plugin-plugins-embeddable-server](./kibana-plugin-plugins-embeddable-server.md) > [EmbeddableSetup](./kibana-plugin-plugins-embeddable-server.embeddablesetup.md) > [getAllMigrations](./kibana-plugin-plugins-embeddable-server.embeddablesetup.getallmigrations.md) | ||
|
||
## EmbeddableSetup.getMigrationVersions property | ||
## EmbeddableSetup.getAllMigrations property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
getMigrationVersions: () => string[]; | ||
getAllMigrations: () => MigrateFunctionsObject; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export * from './migrations_embeddable'; | ||
export * from './migrations_embeddable_factory'; |
30 changes: 30 additions & 0 deletions
30
examples/embeddable_examples/public/migrations/migration.7.3.0.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { MigrateFunction } from '../../../../src/plugins/kibana_utils/common/persistable_state'; | ||
import { SimpleEmbeddableInput } from './migrations_embeddable_factory'; | ||
import { EmbeddableInput } from '../../../../src/plugins/embeddable/common'; | ||
|
||
// before 7.3.0 this embeddable received a very simple input with a variable named `number` | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
type SimpleEmbeddableInput_pre7_3_0 = EmbeddableInput & { | ||
number: number; | ||
}; | ||
|
||
type SimpleEmbeddable730MigrateFn = MigrateFunction< | ||
SimpleEmbeddableInput_pre7_3_0, | ||
SimpleEmbeddableInput | ||
>; | ||
|
||
// when migrating old state we'll need to set a default title, or we should make title optional in the new state | ||
const defaultTitle = 'no title'; | ||
|
||
export const migration730: SimpleEmbeddable730MigrateFn = (state) => { | ||
const newState: SimpleEmbeddableInput = { ...state, title: defaultTitle, value: state.number }; | ||
return newState; | ||
}; |
45 changes: 45 additions & 0 deletions
45
examples/embeddable_examples/public/migrations/migrations_embeddable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { SIMPLE_EMBEDDABLE, SimpleEmbeddableInput } from '.'; | ||
import { Embeddable, IContainer } from '../../../../src/plugins/embeddable/public'; | ||
|
||
export class SimpleEmbeddable extends Embeddable<SimpleEmbeddableInput> { | ||
// The type of this embeddable. This will be used to find the appropriate factory | ||
// to instantiate this kind of embeddable. | ||
public readonly type = SIMPLE_EMBEDDABLE; | ||
|
||
constructor(initialInput: SimpleEmbeddableInput, parent?: IContainer) { | ||
super( | ||
// Input state is irrelevant to this embeddable, just pass it along. | ||
initialInput, | ||
// Initial output state - this embeddable does not do anything with output, so just | ||
// pass along an empty object. | ||
{}, | ||
// Optional parent component, this embeddable can optionally be rendered inside a container. | ||
parent | ||
); | ||
} | ||
|
||
/** | ||
* Render yourself at the dom node using whatever framework you like, angular, react, or just plain | ||
* vanilla js. | ||
* @param node | ||
*/ | ||
public render(node: HTMLElement) { | ||
const input = this.getInput(); | ||
// eslint-disable-next-line no-unsanitized/property | ||
node.innerHTML = `<div data-test-subj="simpleEmbeddable">${input.title} ${input.value}</div>`; | ||
} | ||
|
||
/** | ||
* This is mostly relevant for time based embeddables which need to update data | ||
* even if EmbeddableInput has not changed at all. | ||
*/ | ||
public reload() {} | ||
} |
55 changes: 55 additions & 0 deletions
55
examples/embeddable_examples/public/migrations/migrations_embeddable_factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { | ||
IContainer, | ||
EmbeddableInput, | ||
EmbeddableFactoryDefinition, | ||
EmbeddableFactory, | ||
} from '../../../../src/plugins/embeddable/public'; | ||
import { SimpleEmbeddable } from './migrations_embeddable'; | ||
import { migration730 } from './migration.7.3.0'; | ||
|
||
export const SIMPLE_EMBEDDABLE = 'SIMPLE_EMBEDDABLE'; | ||
|
||
// in 7.3.0 we added `title` to the input and renamed the `number` variable to `value` | ||
export type SimpleEmbeddableInput = EmbeddableInput & { | ||
title: string; | ||
value: number; | ||
}; | ||
|
||
export type SimpleEmbeddableFactory = EmbeddableFactory; | ||
export class SimpleEmbeddableFactoryDefinition | ||
implements EmbeddableFactoryDefinition<SimpleEmbeddableInput> { | ||
public readonly type = SIMPLE_EMBEDDABLE; | ||
|
||
// we need to provide migration function every time we change the interface of our state | ||
public readonly migrations = { | ||
'7.3.0': migration730, | ||
}; | ||
|
||
/** | ||
* In our simple example, we let everyone have permissions to edit this. Most | ||
* embeddables should check the UI Capabilities service to be sure of | ||
* the right permissions. | ||
*/ | ||
public async isEditable() { | ||
return true; | ||
} | ||
|
||
public async create(initialInput: SimpleEmbeddableInput, parent?: IContainer) { | ||
return new SimpleEmbeddable(initialInput, parent); | ||
} | ||
|
||
public getDisplayName() { | ||
return i18n.translate('embeddableExamples.migrations.displayName', { | ||
defaultMessage: 'hello world', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
examples/embeddable_examples/server/searchable_list_saved_object.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { mapValues } from 'lodash'; | ||
import { SavedObjectsType, SavedObjectUnsanitizedDoc } from 'kibana/server'; | ||
import { EmbeddableSetup } from '../../../src/plugins/embeddable/server'; | ||
|
||
export const searchableListSavedObject = (embeddable: EmbeddableSetup) => { | ||
return { | ||
name: 'searchableList', | ||
hidden: false, | ||
namespaceType: 'single', | ||
management: { | ||
icon: 'visualizeApp', | ||
defaultSearchField: 'title', | ||
importableAndExportable: true, | ||
getTitle(obj: any) { | ||
return obj.attributes.title; | ||
}, | ||
}, | ||
mappings: { | ||
properties: { | ||
title: { type: 'text' }, | ||
version: { type: 'integer' }, | ||
}, | ||
}, | ||
migrations: () => { | ||
// we assume all the migration will be done by embeddables service and that saved object holds no extra state besides that of searchable list embeddable input\ | ||
// if saved object would hold additional information we would need to merge the response from embeddables.getAllMigrations with our custom migrations. | ||
return mapValues(embeddable.getAllMigrations(), (migrate) => { | ||
return (state: SavedObjectUnsanitizedDoc) => ({ | ||
...state, | ||
attributes: migrate(state.attributes), | ||
}); | ||
}); | ||
}, | ||
} as SavedObjectsType; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.