Skip to content

Commit

Permalink
[8.10] [Saved Objects] Fix document migrator to prevent losing namesp…
Browse files Browse the repository at this point in the history
…aces on import (#164848) (#167372)

# Backport

This will backport the following commits from `main` to `8.10`:
- [[Saved Objects] Fix document migrator to prevent losing namespaces on
import (#164848)](#164848)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Michael
Dokolin","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-09-19T09:18:45Z","message":"[Saved
Objects] Fix document migrator to prevent losing namespaces on import
(#164848)","sha":"ac73d1f6b23eca7cad2e92e1a2cd1aae872ad9c4","branchLabelMapping":{"^v8.11.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["review","Team:Core","Feature:Saved
Objects","release_note:skip","Feature:Migrations","backport:prev-minor","v8.11.0"],"number":164848,"url":"https://github.com/elastic/kibana/pull/164848","mergeCommit":{"message":"[Saved
Objects] Fix document migrator to prevent losing namespaces on import
(#164848)","sha":"ac73d1f6b23eca7cad2e92e1a2cd1aae872ad9c4"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.11.0","labelRegex":"^v8.11.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/164848","number":164848,"mergeCommit":{"message":"[Saved
Objects] Fix document migrator to prevent losing namespaces on import
(#164848)","sha":"ac73d1f6b23eca7cad2e92e1a2cd1aae872ad9c4"}}]}]
BACKPORT-->

Co-authored-by: Michael Dokolin <[email protected]>
  • Loading branch information
kibanamachine and dokmic authored Sep 27, 2023
1 parent 5e0d427 commit 7742bf2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,36 @@ describe('DocumentMigrator', () => {
]);
});

it('does not lose namespaces in documents with undefined namespace and defined namespaces property', () => {
const migrator = new DocumentMigrator({
...testOpts(),
typeRegistry: createRegistry(
{ name: 'dog', namespaceType: 'multiple', convertToMultiNamespaceTypeVersion: '1.0.0' }
// no migration transforms are defined, the typeMigrationVersion will be derived from 'convertToMultiNamespaceTypeVersion'
),
});
migrator.prepareMigrations();
const obj = {
id: 'mischievous',
type: 'dog',
attributes: { name: 'Ann' },
coreMigrationVersion: kibanaVersion,
typeMigrationVersion: '0.1.0',
namespaces: ['something'],
} as SavedObjectUnsanitizedDoc;
const actual = migrator.migrateAndConvert(obj);
expect(actual).toEqual([
{
id: 'mischievous',
type: 'dog',
attributes: { name: 'Ann' },
coreMigrationVersion: kibanaVersion,
typeMigrationVersion: '1.0.0',
namespaces: ['something'],
},
]);
});

it('does not fail when encountering documents with coreMigrationVersion higher than the latest known', () => {
const migrator = new DocumentMigrator({
...testOpts(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ function convertNamespaceType(doc: SavedObjectUnsanitizedDoc) {
const { namespace, ...otherAttrs } = doc;
const additionalDocs: SavedObjectUnsanitizedDoc[] = [];

if (namespace == null && otherAttrs.namespaces) {
return {
additionalDocs,
transformedDoc: otherAttrs,
};
}

// If this object exists in the default namespace, return it with the appropriate `namespaces` field without changing its ID.
if (namespace === undefined) {
return {
Expand Down

0 comments on commit 7742bf2

Please sign in to comment.