();
- for (const baseMigrationVersion of Object.keys(baseEmbeddableMigrations)) {
- uniqueVersions.add(baseMigrationVersion);
- }
- const factories = this.embeddableFactories.values();
- for (const factory of factories) {
- Object.keys(factory.migrations).forEach((version) => uniqueVersions.add(version));
- }
- const enhancements = this.enhancements.values();
- for (const enhancement of enhancements) {
- Object.keys(enhancement.migrations).forEach((version) => uniqueVersions.add(version));
- }
- return Array.from(uniqueVersions);
- };
}
diff --git a/src/plugins/embeddable/server/server.api.md b/src/plugins/embeddable/server/server.api.md
index d5c7ce29bab9ec..f8f3dcb0aa0ba4 100644
--- a/src/plugins/embeddable/server/server.api.md
+++ b/src/plugins/embeddable/server/server.api.md
@@ -23,8 +23,10 @@ export interface EmbeddableRegistryDefinition {
+ // Warning: (ae-forgotten-export) The symbol "MigrateFunctionsObject" needs to be exported by the entry point index.d.ts
+ //
// (undocumented)
- getMigrationVersions: () => string[];
+ getAllMigrations: () => MigrateFunctionsObject;
// (undocumented)
registerEmbeddableFactory: (factory: EmbeddableRegistryDefinition) => void;
// (undocumented)
diff --git a/src/plugins/kibana_utils/common/persistable_state/index.ts b/src/plugins/kibana_utils/common/persistable_state/index.ts
index 18f59186f61831..1f417002f27651 100644
--- a/src/plugins/kibana_utils/common/persistable_state/index.ts
+++ b/src/plugins/kibana_utils/common/persistable_state/index.ts
@@ -8,3 +8,4 @@
export * from './types';
export { migrateToLatest } from './migrate_to_latest';
+export { mergeMigrationFunctionMaps } from './merge_migration_function_map';
diff --git a/src/plugins/kibana_utils/common/persistable_state/merge_migration_function_map.test.ts b/src/plugins/kibana_utils/common/persistable_state/merge_migration_function_map.test.ts
new file mode 100644
index 00000000000000..9a6d774d70475f
--- /dev/null
+++ b/src/plugins/kibana_utils/common/persistable_state/merge_migration_function_map.test.ts
@@ -0,0 +1,28 @@
+/*
+ * 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 { mergeMigrationFunctionMaps } from './merge_migration_function_map';
+
+describe('mergeSavedObjectMigrationMaps', () => {
+ const obj1 = {
+ '7.12.1': (state: number) => state + 1,
+ '7.12.2': (state: number) => state + 2,
+ };
+
+ const obj2 = {
+ '7.12.0': (state: number) => state - 2,
+ '7.12.2': (state: number) => state + 2,
+ };
+
+ test('correctly merges two saved object migration maps', () => {
+ const result = mergeMigrationFunctionMaps(obj1, obj2);
+ expect(result['7.12.0'](5)).toEqual(3);
+ expect(result['7.12.1'](5)).toEqual(6);
+ expect(result['7.12.2'](5)).toEqual(9);
+ });
+});
diff --git a/src/plugins/kibana_utils/common/persistable_state/merge_migration_function_map.ts b/src/plugins/kibana_utils/common/persistable_state/merge_migration_function_map.ts
new file mode 100644
index 00000000000000..fc48ab119b02c6
--- /dev/null
+++ b/src/plugins/kibana_utils/common/persistable_state/merge_migration_function_map.ts
@@ -0,0 +1,24 @@
+/*
+ * 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 { mergeWith } from 'lodash';
+import { MigrateFunctionsObject, MigrateFunction, SerializableState } from './types';
+
+export const mergeMigrationFunctionMaps = (
+ obj1: MigrateFunctionsObject,
+ obj2: MigrateFunctionsObject
+) => {
+ const customizer = (objValue: MigrateFunction, srcValue: MigrateFunction) => {
+ if (!srcValue || !objValue) {
+ return srcValue || objValue;
+ }
+ return (state: SerializableState) => objValue(srcValue(state));
+ };
+
+ return mergeWith({ ...obj1 }, obj2, customizer);
+};
diff --git a/src/plugins/kibana_utils/common/persistable_state/types.ts b/src/plugins/kibana_utils/common/persistable_state/types.ts
index f7168b46e7fca6..ba2b923e3e475f 100644
--- a/src/plugins/kibana_utils/common/persistable_state/types.ts
+++ b/src/plugins/kibana_utils/common/persistable_state/types.ts
@@ -99,12 +99,22 @@ export interface PersistableState
};
export type MigrateFunction<
FromVersion extends SerializableState = SerializableState,
ToVersion extends SerializableState = SerializableState
> = (state: FromVersion) => ToVersion;
+/**
+ * migrate function runs the specified migration
+ * @param state
+ * @param version
+ */
+export type PersistableStateMigrateFn = (
+ state: SerializableState,
+ version: string
+) => SerializableState;
+
/**
* @todo Shall we remove this?
*/
@@ -150,23 +160,6 @@ export interface PersistableStateService
VersionedState
;
+
+ /**
+ * returns all registered migrations
+ */
+ getAllMigrations?: () => MigrateFunctionsObject;
}