Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary DocumentMigrator cleanup #150529

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export {
isWriteBlockException,
isIndexNotFoundException,
} from './src/actions/es_errors';
export { deterministicallyRegenerateObjectId } from './src/core/document_migrator';
export {
REMOVED_TYPES,
deterministicallyRegenerateObjectId,
type DocumentsTransformFailed,
type DocumentsTransformSuccess,
} from './src/core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Side Public License, v 1.
*/

export { DocumentMigrator } from './document_migrator';
export { buildActiveMappings } from './build_active_mappings';
export type { LogFn } from './migration_logger';
export { excludeUnusedTypesQuery, REMOVED_TYPES } from './unused_types';
export { TransformSavedObjectDocumentError } from './transform_saved_object_document_error';
export { deterministicallyRegenerateObjectId } from './regenerate_object_id';
export type {
DocumentsTransformFailed,
DocumentsTransformSuccess,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
SavedObjectUnsanitizedDoc,
} from '@kbn/core-saved-objects-server';
import { SavedObjectsSerializer } from '@kbn/core-saved-objects-base-server-internal';
import { MigrateAndConvertFn } from './document_migrator';
import type { MigrateAndConvertFn } from '../document_migrator/document_migrator';
import { TransformSavedObjectDocumentError } from '.';

export interface DocumentsTransformFailed {
Expand Down
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 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 { v5 as uuidv5 } from 'uuid';

/**
* Deterministically regenerates a saved object's ID based upon it's current namespace, type, and ID. This ensures that we can regenerate
* any existing object IDs without worrying about collisions if two objects that exist in different namespaces share an ID. It also ensures
* that we can later regenerate any inbound object references to match.
*
* @note This is only intended to be used when single-namespace object types are converted into multi-namespace object types.
* @internal
*/
export function deterministicallyRegenerateObjectId(namespace: string, type: string, id: string) {
return uuidv5(`${namespace}:${type}:${id}`, uuidv5.DNS); // the uuidv5 namespace constant (uuidv5.DNS) is arbitrary
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 _ from 'lodash';
import type { Logger } from '@kbn/logging';
import type { ISavedObjectTypeRegistry } from '@kbn/core-saved-objects-server';
import type { Transform, ActiveMigrations } from './types';
import { getReferenceTransforms, getConversionTransforms } from './internal_transforms';
import { validateMigrationsMapObject } from './validate_migrations';
import { transformComparator, wrapWithTry } from './utils';

/**
* Converts migrations from a format that is convenient for callers to a format that
* is convenient for our internal usage:
* From: { type: { version: fn } }
* To: { type: { latestMigrationVersion?: string; latestCoreMigrationVersion?: string; transforms: [{ version: string, transform: fn }] } }
*/
export function buildActiveMigrations(
typeRegistry: ISavedObjectTypeRegistry,
kibanaVersion: string,
log: Logger
): ActiveMigrations {
const referenceTransforms = getReferenceTransforms(typeRegistry);

return typeRegistry.getAllTypes().reduce((migrations, type) => {
const migrationsMap =
typeof type.migrations === 'function' ? type.migrations() : type.migrations;
validateMigrationsMapObject(type.name, kibanaVersion, migrationsMap);

const migrationTransforms = Object.entries(migrationsMap ?? {}).map<Transform>(
([version, transform]) => ({
version,
transform: wrapWithTry(version, type, transform, log),
transformType: 'migrate',
})
);
const conversionTransforms = getConversionTransforms(type);
const transforms = [
...referenceTransforms,
...conversionTransforms,
...migrationTransforms,
].sort(transformComparator);

if (!transforms.length) {
return migrations;
}

const migrationVersionTransforms: Transform[] = [];
const coreMigrationVersionTransforms: Transform[] = [];
transforms.forEach((x) => {
if (x.transformType === 'migrate' || x.transformType === 'convert') {
migrationVersionTransforms.push(x);
} else {
coreMigrationVersionTransforms.push(x);
}
});

return {
...migrations,
[type.name]: {
latestMigrationVersion: _.last(migrationVersionTransforms)?.version,
latestCoreMigrationVersion: _.last(coreMigrationVersionTransforms)?.version,
transforms,
},
};
}, {} as ActiveMigrations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
LEGACY_URL_ALIAS_TYPE,
} from '@kbn/core-saved-objects-base-server-internal';
import { DocumentMigrator } from './document_migrator';
import { TransformSavedObjectDocumentError } from './transform_saved_object_document_error';
import { TransformSavedObjectDocumentError } from '../core/transform_saved_object_document_error';
import { loggingSystemMock } from '@kbn/core-logging-server-mocks';

const mockLoggerFactory = loggingSystemMock.create();
Expand Down
Loading