-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preliminary DocumentMigrator cleanup (#150529)
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
1 parent
1bd6b37
commit 91ff710
Showing
16 changed files
with
502 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...ved-objects/core-saved-objects-migration-server-internal/src/core/regenerate_object_id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
72 changes: 72 additions & 0 deletions
72
...-saved-objects-migration-server-internal/src/document_migrator/build_active_migrations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.