Skip to content

Commit

Permalink
[Discover] Add migrations for visContextJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
jughosta committed Jan 15, 2024
1 parent 772a701 commit 4529711
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/plugins/saved_search/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"server": true,
"browser": true,
"requiredPlugins": ["data", "contentManagement", "embeddable", "expressions"],
"optionalPlugins": ["spaces", "savedObjectsTaggingOss"],
"optionalPlugins": ["spaces", "savedObjectsTaggingOss", "lens"],
"requiredBundles": [],
"extraPublicDirs": ["common"]
}
Expand Down
11 changes: 9 additions & 2 deletions src/plugins/saved_search/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
PluginSetup as DataPluginSetup,
PluginStart as DataPluginStart,
} from '@kbn/data-plugin/server';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import type { ContentManagementServerSetup } from '@kbn/content-management-plugin/server';
import { ExpressionsServerSetup } from '@kbn/expressions-plugin/server';
import { getSavedSearchObjectType } from './saved_objects';
Expand All @@ -28,6 +29,7 @@ export interface SavedSearchPublicSetupDependencies {
data: DataPluginSetup;
contentManagement: ContentManagementServerSetup;
expressions: ExpressionsServerSetup;
lens: LensServerPluginSetup;
}

export interface SavedSearchServerStartDeps {
Expand All @@ -41,7 +43,7 @@ export class SavedSearchServerPlugin

public setup(
core: CoreSetup,
{ data, contentManagement, expressions }: SavedSearchPublicSetupDependencies
{ data, contentManagement, expressions, lens }: SavedSearchPublicSetupDependencies
) {
contentManagement.register({
id: SavedSearchType,
Expand All @@ -57,7 +59,12 @@ export class SavedSearchServerPlugin
const searchSource = data.search.searchSource;

const getSearchSourceMigrations = searchSource.getAllMigrations.bind(searchSource);
core.savedObjects.registerType(getSavedSearchObjectType(getSearchSourceMigrations));
core.savedObjects.registerType(
getSavedSearchObjectType({
getSearchSourceMigrations,
lensEmbeddableFactory: lens.lensEmbeddableFactory,
})
);

expressions.registerType(kibanaContext);
expressions.registerFunction(
Expand Down
17 changes: 13 additions & 4 deletions src/plugins/saved_search/server/saved_objects/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
import { ANALYTICS_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import { SavedObjectsType } from '@kbn/core/server';
import { MigrateFunctionsObject } from '@kbn/kibana-utils-plugin/common';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import { getAllMigrations } from './search_migrations';
import { SCHEMA_SEARCH_V8_8_0, SCHEMA_SEARCH_V8_12_0, SCHEMA_SEARCH_V8_13_0 } from './schema';

export function getSavedSearchObjectType(
getSearchSourceMigrations: () => MigrateFunctionsObject
): SavedObjectsType {
export function getSavedSearchObjectType({
getSearchSourceMigrations,
lensEmbeddableFactory,
}: {
getSearchSourceMigrations: () => MigrateFunctionsObject;
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory'];
}): SavedObjectsType {
return {
name: 'search',
indexPattern: ANALYTICS_SAVED_OBJECT_INDEX,
Expand Down Expand Up @@ -47,6 +52,10 @@ export function getSavedSearchObjectType(
'8.12.0': SCHEMA_SEARCH_V8_12_0,
'8.13.0': SCHEMA_SEARCH_V8_13_0,
},
migrations: () => getAllMigrations(getSearchSourceMigrations()), // TODO: add lens embeddable migrations for `visContextJSON`
migrations: () =>
getAllMigrations({
searchSourceMigrations: getSearchSourceMigrations(),
lensEmbeddableFactory,
}),
};
}
19 changes: 14 additions & 5 deletions src/plugins/saved_search/server/saved_objects/search_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { mergeSavedObjectMigrationMaps } from '@kbn/core/server';
import { DEFAULT_QUERY_LANGUAGE } from '@kbn/data-plugin/server';
import { MigrateFunctionsObject, MigrateFunction } from '@kbn/kibana-utils-plugin/common';
import { isSerializedSearchSource, SerializedSearchSourceFields } from '@kbn/data-plugin/common';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import { getLensVisContextMigrations } from './search_vis_migrations';

export interface SavedSearchMigrationAttributes extends SavedObjectAttributes {
kibanaSavedObjectMeta: {
Expand Down Expand Up @@ -170,11 +172,18 @@ export const searchMigrations = {
'7.9.3': flow(migrateMatchAllQuery),
};

export const getAllMigrations = (
searchSourceMigrations: MigrateFunctionsObject
): SavedObjectMigrationMap => {
export const getAllMigrations = ({
searchSourceMigrations,
lensEmbeddableFactory,
}: {
searchSourceMigrations: MigrateFunctionsObject;
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory'];
}): SavedObjectMigrationMap => {
return mergeSavedObjectMigrationMaps(
searchMigrations,
getSearchSourceMigrations(searchSourceMigrations) as SavedObjectMigrationMap
mergeSavedObjectMigrationMaps(
searchMigrations,
getSearchSourceMigrations(searchSourceMigrations) as SavedObjectMigrationMap
),
getLensVisContextMigrations(lensEmbeddableFactory)
);
};
116 changes: 116 additions & 0 deletions src/plugins/saved_search/server/saved_objects/search_vis_migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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 { isFunction, mapValues } from 'lodash';
import type { SavedObjectMigrationMap } from '@kbn/core-saved-objects-server';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import { LogMeta } from '@kbn/logging';
import { MigrateFunction, MigrateFunctionsObject } from '@kbn/kibana-utils-plugin/common';
import {
SavedObjectMigrationContext,
SavedObjectMigrationParams,
SavedObjectSanitizedDoc,
SavedObjectUnsanitizedDoc,
} from '@kbn/core-saved-objects-server';
import type { SavedSearchAttributes } from '../../common/types';

export const getLensVisContextMigrations = (
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory']
): SavedObjectMigrationMap => {
return getLensMigrations({
lensEmbeddableFactory,
migratorFactory: migrateByValueLensVisualizations,
});
};

function migrateByValueLensVisualizations(
migrate: MigrateFunction
): SavedObjectMigrationParams<SavedSearchAttributes, SavedSearchAttributes> {
return {
deferred: false,
transform: (
doc: SavedObjectUnsanitizedDoc<SavedSearchAttributes>,
context: SavedObjectMigrationContext
): SavedObjectSanitizedDoc<SavedSearchAttributes> => {
if (doc.attributes.visContextJSON) {
return migrateLensVisContext({ migrate, doc, context });
}

return Object.assign(doc, { references: doc.references ?? [] });
},
};
}

interface MigrationLogMeta extends LogMeta {
migrations: {
[x: string]: {
id: string;
};
};
}

function migrateLensVisContext({
migrate,
doc,
context,
}: {
migrate: MigrateFunction;
doc: SavedObjectUnsanitizedDoc<SavedSearchAttributes>;
context: SavedObjectMigrationContext;
}): SavedObjectSanitizedDoc<SavedSearchAttributes> {
try {
const { visContextJSON } = doc.attributes;

return {
...doc,
attributes: {
...doc.attributes,
visContextJSON: visContextJSON
? JSON.stringify(migrate(JSON.parse(visContextJSON)))
: undefined,
},
references: doc.references ?? [],
};
} catch (error) {
context.log.error<MigrationLogMeta>(
`Failed to migrate "search" with doc id: ${doc.id} version: ${context.migrationVersion} error: ${error.message}`,
{
migrations: {
visContextJSON: {
id: doc.id,
},
},
}
);

return Object.assign(doc, { references: doc.references ?? [] });
}
}

interface GetLensMigrationsArgs<T> {
lensEmbeddableFactory: LensServerPluginSetup['lensEmbeddableFactory'];
migratorFactory: (
migrate: MigrateFunction,
migrationVersion: string
) => SavedObjectMigrationParams<T, T>;
}

function getLensMigrations<T>({
lensEmbeddableFactory,
migratorFactory,
}: GetLensMigrationsArgs<T>) {
const lensMigrations = lensEmbeddableFactory().migrations;
const lensMigrationObject = isFunction(lensMigrations) ? lensMigrations() : lensMigrations || {};

const embeddableMigrations = mapValues<MigrateFunctionsObject, SavedObjectMigrationParams<T, T>>(
lensMigrationObject,
migratorFactory
);

return embeddableMigrations;
}

0 comments on commit 4529711

Please sign in to comment.