Skip to content

Commit

Permalink
Preliminary DocumentMigrator cleanup (#150529)
Browse files Browse the repository at this point in the history
Related to #150301

This PR tend to do some preliminary cleanup before diving into the
actual implementation. I'm mostly extracting things from the (way too
big and hard to read) document migrator, to increase readability,
maintainability and testability (note: I did not add additional unit
test for the extracted files, as I did not change the impl and as we'll
be rewriting them all soon enough - it would just have been a loss of
time)

- Move the document migrator from
`packages/core/saved-objects/core-saved-objects-migration-server-internal/src/core`
to it's dedicated folder `document_migrator` folder
- Extract all the bits not directly performing the document
transformation (e.g building the migration map, validation, utils) to
their dedicated files
- Only moving things around. No concrete code (apart from types) was
modified, to ease managing conflicts with
#150443
  • Loading branch information
pgayvallet authored Feb 8, 2023
1 parent 1bd6b37 commit 91ff710
Show file tree
Hide file tree
Showing 16 changed files with 502 additions and 398 deletions.
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

0 comments on commit 91ff710

Please sign in to comment.