Skip to content

Commit

Permalink
[Maps] embeddable migrations (#101070)
Browse files Browse the repository at this point in the history
* [Maps] embeddable migrations

* review feedback

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine authored Jun 4, 2021
1 parent 8f83090 commit 93df9a3
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 141 deletions.
21 changes: 21 additions & 0 deletions x-pack/plugins/maps/server/embeddable_migrations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import semverGte from 'semver/functions/gte';
import { embeddableMigrations } from './embeddable_migrations';
// @ts-ignore
import { savedObjectMigrations } from './saved_objects/saved_object_migrations';

describe('saved object migrations and embeddable migrations', () => {
test('should have same versions registered (>7.12)', () => {
const savedObjectMigrationVersions = Object.keys(savedObjectMigrations).filter((version) => {
return semverGte(version, '7.13.0');
});
const embeddableMigrationVersions = Object.keys(embeddableMigrations);
expect(savedObjectMigrationVersions.sort()).toEqual(embeddableMigrationVersions.sort());
});
});
26 changes: 26 additions & 0 deletions x-pack/plugins/maps/server/embeddable_migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { SerializableState } from '../../../../src/plugins/kibana_utils/common';
import { MapSavedObjectAttributes } from '../common/map_saved_object_type';
import { moveAttribution } from '../common/migrations/move_attribution';

/*
* Embeddables such as Maps, Lens, and Visualize can be embedded by value or by reference on a dashboard.
* To ensure that any migrations (>7.12) are run correctly in both cases,
* the migration function must be registered as both a saved object migration and an embeddable migration
* This is the embeddable migration registry.
*/
export const embeddableMigrations = {
'7.14.0': (state: SerializableState) => {
return {
...state,
attributes: moveAttribution(state as { attributes: MapSavedObjectAttributes }),
} as SerializableState;
},
};
8 changes: 8 additions & 0 deletions x-pack/plugins/maps/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ import { HomeServerPluginSetup } from '../../../../src/plugins/home/server';
import { MapsEmsPluginSetup } from '../../../../src/plugins/maps_ems/server';
import { EMSSettings } from '../common/ems_settings';
import { PluginStart as DataPluginStart } from '../../../../src/plugins/data/server';
import { EmbeddableSetup } from '../../../../src/plugins/embeddable/server';
import { embeddableMigrations } from './embeddable_migrations';

interface SetupDeps {
features: FeaturesPluginSetupContract;
usageCollection: UsageCollectionSetup;
home: HomeServerPluginSetup;
licensing: LicensingPluginSetup;
mapsEms: MapsEmsPluginSetup;
embeddable: EmbeddableSetup;
}

export interface StartDeps {
Expand Down Expand Up @@ -214,6 +217,11 @@ export class MapsPlugin implements Plugin {
core.savedObjects.registerType(mapSavedObjects);
registerMapsUsageCollector(usageCollection, currentConfig);

plugins.embeddable.registerEmbeddableFactory({
id: MAP_SAVED_OBJECT_TYPE,
migrations: embeddableMigrations,
});

return {
config: config$,
};
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/server/saved_objects/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { SavedObjectsType } from 'src/core/server';
import { APP_ICON, getExistingMapPath } from '../../common/constants';
// @ts-ignore
import { migrations } from './migrations';
import { savedObjectMigrations } from './saved_object_migrations';

export const mapSavedObjects: SavedObjectsType = {
name: 'map',
Expand Down Expand Up @@ -39,5 +39,5 @@ export const mapSavedObjects: SavedObjectsType = {
};
},
},
migrations: migrations.map,
migrations: savedObjectMigrations,
};
107 changes: 0 additions & 107 deletions x-pack/plugins/maps/server/saved_objects/migrations.js

This file was deleted.

112 changes: 112 additions & 0 deletions x-pack/plugins/maps/server/saved_objects/saved_object_migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { extractReferences } from '../../common/migrations/references';
import { emsRasterTileToEmsVectorTile } from '../../common/migrations/ems_raster_tile_to_ems_vector_tile';
import { topHitsTimeToSort } from '../../common/migrations/top_hits_time_to_sort';
import { moveApplyGlobalQueryToSources } from '../../common/migrations/move_apply_global_query';
import { addFieldMetaOptions } from '../../common/migrations/add_field_meta_options';
import { migrateSymbolStyleDescriptor } from '../../common/migrations/migrate_symbol_style_descriptor';
import { migrateUseTopHitsToScalingType } from '../../common/migrations/scaling_type';
import { migrateJoinAggKey } from '../../common/migrations/join_agg_key';
import { removeBoundsFromSavedObject } from '../../common/migrations/remove_bounds';
import { setDefaultAutoFitToBounds } from '../../common/migrations/set_default_auto_fit_to_bounds';
import { addTypeToTermJoin } from '../../common/migrations/add_type_to_termjoin';
import { moveAttribution } from '../../common/migrations/move_attribution';

/*
* Embeddables such as Maps, Lens, and Visualize can be embedded by value or by reference on a dashboard.
* To ensure that any migrations (>7.12) are run correctly in both cases,
* the migration function must be registered as both a saved object migration and an embeddable migration
* This is the saved object migration registry.
*/
export const savedObjectMigrations = {
'7.2.0': (doc) => {
const { attributes, references } = extractReferences(doc);

return {
...doc,
attributes,
references,
};
},
'7.4.0': (doc) => {
const attributes = emsRasterTileToEmsVectorTile(doc);

return {
...doc,
attributes,
};
},
'7.5.0': (doc) => {
const attributes = topHitsTimeToSort(doc);

return {
...doc,
attributes,
};
},
'7.6.0': (doc) => {
const attributesPhase1 = moveApplyGlobalQueryToSources(doc);
const attributesPhase2 = addFieldMetaOptions({ attributes: attributesPhase1 });

return {
...doc,
attributes: attributesPhase2,
};
},
'7.7.0': (doc) => {
const attributesPhase1 = migrateSymbolStyleDescriptor(doc);
const attributesPhase2 = migrateUseTopHitsToScalingType({ attributes: attributesPhase1 });

return {
...doc,
attributes: attributesPhase2,
};
},
'7.8.0': (doc) => {
const attributes = migrateJoinAggKey(doc);

return {
...doc,
attributes,
};
},
'7.9.0': (doc) => {
const attributes = removeBoundsFromSavedObject(doc);

return {
...doc,
attributes,
};
},
'7.10.0': (doc) => {
const attributes = setDefaultAutoFitToBounds(doc);

return {
...doc,
attributes,
};
},
'7.12.0': (doc) => {
const attributes = addTypeToTermJoin(doc);

return {
...doc,
attributes,
};
},
'7.14.0': (doc) => {
const attributes = moveAttribution(doc);

return {
...doc,
attributes,
};
},
};
Loading

0 comments on commit 93df9a3

Please sign in to comment.