Skip to content

Commit 2d0bb37

Browse files
Merge branch '7.x' into backport/7.x/pr-64767
2 parents 0916b13 + 7d313c3 commit 2d0bb37

96 files changed

Lines changed: 986 additions & 743 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/development/core/server/kibana-plugin-core-server.savedobjectmigrationfn.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,36 @@ A migration function for a [saved object type](./kibana-plugin-core-server.saved
99
<b>Signature:</b>
1010

1111
```typescript
12-
export declare type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;
12+
export declare type SavedObjectMigrationFn<InputAttributes = unknown, MigratedAttributes = unknown> = (doc: SavedObjectUnsanitizedDoc<InputAttributes>, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc<MigratedAttributes>;
1313
```
1414

1515
## Example
1616

1717

1818
```typescript
19-
const migrateProperty: SavedObjectMigrationFn = (doc, { log }) => {
20-
if(doc.attributes.someProp === null) {
21-
log.warn('Skipping migration');
22-
} else {
23-
doc.attributes.someProp = migrateProperty(doc.attributes.someProp);
24-
}
25-
26-
return doc;
19+
interface TypeV1Attributes {
20+
someKey: string;
21+
obsoleteProperty: number;
2722
}
2823

24+
interface TypeV2Attributes {
25+
someKey: string;
26+
newProperty: string;
27+
}
28+
29+
const migrateToV2: SavedObjectMigrationFn<TypeV1Attributes, TypeV2Attributes> = (doc, { log }) => {
30+
const { obsoleteProperty, ...otherAttributes } = doc.attributes;
31+
// instead of mutating `doc` we make a shallow copy so that we can use separate types for the input
32+
// and output attributes. We don't need to make a deep copy, we just need to ensure that obsolete
33+
// attributes are not present on the returned doc.
34+
return {
35+
...doc,
36+
attributes: {
37+
...otherAttributes,
38+
newProperty: migrate(obsoleteProperty),
39+
},
40+
};
41+
};
42+
2943
```
3044

docs/development/core/server/kibana-plugin-core-server.savedobjectsanitizeddoc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Describes Saved Object documents that have passed through the migration framewor
99
<b>Signature:</b>
1010

1111
```typescript
12-
export declare type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable;
12+
export declare type SavedObjectSanitizedDoc<T = unknown> = SavedObjectDoc<T> & Referencable;
1313
```

docs/development/core/server/kibana-plugin-core-server.savedobjectunsanitizeddoc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Describes Saved Object documents from Kibana &lt; 7.0.0 which don't have a `refe
99
<b>Signature:</b>
1010

1111
```typescript
12-
export declare type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial<Referencable>;
12+
export declare type SavedObjectUnsanitizedDoc<T = unknown> = SavedObjectDoc<T> & Partial<Referencable>;
1313
```

src/core/MIGRATION_EXAMPLES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ const migration = (doc, log) => {...}
957957
Would be converted to:
958958
959959
```typescript
960-
const migration: SavedObjectMigrationFn = (doc, { log }) => {...}
960+
const migration: SavedObjectMigrationFn<OldAttributes, MigratedAttributes> = (doc, { log }) => {...}
961961
```
962962
963963
### Remarks

src/core/server/saved_objects/migrations/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ export { DocumentMigrator } from './document_migrator';
2121
export { IndexMigrator } from './index_migrator';
2222
export { buildActiveMappings } from './build_active_mappings';
2323
export { CallCluster } from './call_cluster';
24-
export { LogFn } from './migration_logger';
24+
export { LogFn, SavedObjectsMigrationLogger } from './migration_logger';
2525
export { MigrationResult, MigrationStatus } from './migration_coordinator';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { SavedObjectMigrationContext } from './types';
21+
import { SavedObjectsMigrationLogger } from './core';
22+
23+
const createLoggerMock = (): jest.Mocked<SavedObjectsMigrationLogger> => {
24+
const mock = {
25+
debug: jest.fn(),
26+
info: jest.fn(),
27+
warning: jest.fn(),
28+
warn: jest.fn(),
29+
};
30+
31+
return mock;
32+
};
33+
34+
const createContextMock = (): jest.Mocked<SavedObjectMigrationContext> => {
35+
const mock = {
36+
log: createLoggerMock(),
37+
};
38+
return mock;
39+
};
40+
41+
export const migrationMocks = {
42+
createContext: createContextMock,
43+
};

src/core/server/saved_objects/migrations/types.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,37 @@ import { SavedObjectsMigrationLogger } from './core/migration_logger';
2626
*
2727
* @example
2828
* ```typescript
29-
* const migrateProperty: SavedObjectMigrationFn = (doc, { log }) => {
30-
* if(doc.attributes.someProp === null) {
31-
* log.warn('Skipping migration');
32-
* } else {
33-
* doc.attributes.someProp = migrateProperty(doc.attributes.someProp);
34-
* }
29+
* interface TypeV1Attributes {
30+
* someKey: string;
31+
* obsoleteProperty: number;
32+
* }
3533
*
36-
* return doc;
34+
* interface TypeV2Attributes {
35+
* someKey: string;
36+
* newProperty: string;
3737
* }
38+
*
39+
* const migrateToV2: SavedObjectMigrationFn<TypeV1Attributes, TypeV2Attributes> = (doc, { log }) => {
40+
* const { obsoleteProperty, ...otherAttributes } = doc.attributes;
41+
* // instead of mutating `doc` we make a shallow copy so that we can use separate types for the input
42+
* // and output attributes. We don't need to make a deep copy, we just need to ensure that obsolete
43+
* // attributes are not present on the returned doc.
44+
* return {
45+
* ...doc,
46+
* attributes: {
47+
* ...otherAttributes,
48+
* newProperty: migrate(obsoleteProperty),
49+
* },
50+
* };
51+
* };
3852
* ```
3953
*
4054
* @public
4155
*/
42-
export type SavedObjectMigrationFn = (
43-
doc: SavedObjectUnsanitizedDoc,
56+
export type SavedObjectMigrationFn<InputAttributes = unknown, MigratedAttributes = unknown> = (
57+
doc: SavedObjectUnsanitizedDoc<InputAttributes>,
4458
context: SavedObjectMigrationContext
45-
) => SavedObjectUnsanitizedDoc;
59+
) => SavedObjectUnsanitizedDoc<MigratedAttributes>;
4660

4761
/**
4862
* Migration context provided when invoking a {@link SavedObjectMigrationFn | migration handler}

src/core/server/saved_objects/saved_objects_service.mock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { savedObjectsClientProviderMock } from './service/lib/scoped_client_prov
3131
import { savedObjectsRepositoryMock } from './service/lib/repository.mock';
3232
import { savedObjectsClientMock } from './service/saved_objects_client.mock';
3333
import { typeRegistryMock } from './saved_objects_type_registry.mock';
34+
import { migrationMocks } from './migrations/mocks';
3435
import { ServiceStatusLevels } from '../status';
3536

3637
type SavedObjectsServiceContract = PublicMethodsOf<SavedObjectsService>;
@@ -105,4 +106,5 @@ export const savedObjectsServiceMock = {
105106
createSetupContract: createSetupContractMock,
106107
createInternalStartContract: createInternalStartContractMock,
107108
createStartContract: createStartContractMock,
109+
createMigrationContext: migrationMocks.createContext,
108110
};

src/core/server/saved_objects/serialization/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export interface SavedObjectsRawDocSource {
4747
/**
4848
* Saved Object base document
4949
*/
50-
interface SavedObjectDoc {
51-
attributes: any;
50+
interface SavedObjectDoc<T = unknown> {
51+
attributes: T;
5252
id?: string; // NOTE: SavedObjectDoc is used for uncreated objects where `id` is optional
5353
type: string;
5454
namespace?: string;
@@ -69,12 +69,12 @@ interface Referencable {
6969
*
7070
* @public
7171
*/
72-
export type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial<Referencable>;
72+
export type SavedObjectUnsanitizedDoc<T = unknown> = SavedObjectDoc<T> & Partial<Referencable>;
7373

7474
/**
7575
* Describes Saved Object documents that have passed through the migration
7676
* framework and are guaranteed to have a `references` root property.
7777
*
7878
* @public
7979
*/
80-
export type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable;
80+
export type SavedObjectSanitizedDoc<T = unknown> = SavedObjectDoc<T> & Referencable;

src/core/server/server.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ export interface SavedObjectMigrationContext {
16801680
}
16811681

16821682
// @public
1683-
export type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc;
1683+
export type SavedObjectMigrationFn<InputAttributes = unknown, MigratedAttributes = unknown> = (doc: SavedObjectUnsanitizedDoc<InputAttributes>, context: SavedObjectMigrationContext) => SavedObjectUnsanitizedDoc<MigratedAttributes>;
16841684

16851685
// @public
16861686
export interface SavedObjectMigrationMap {
@@ -1708,7 +1708,7 @@ export interface SavedObjectsAddToNamespacesOptions extends SavedObjectsBaseOpti
17081708
// Warning: (ae-forgotten-export) The symbol "Referencable" needs to be exported by the entry point index.d.ts
17091709
//
17101710
// @public
1711-
export type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable;
1711+
export type SavedObjectSanitizedDoc<T = unknown> = SavedObjectDoc<T> & Referencable;
17121712

17131713
// @public (undocumented)
17141714
export interface SavedObjectsBaseOptions {
@@ -2311,7 +2311,7 @@ export class SavedObjectTypeRegistry {
23112311
}
23122312

23132313
// @public
2314-
export type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial<Referencable>;
2314+
export type SavedObjectUnsanitizedDoc<T = unknown> = SavedObjectDoc<T> & Partial<Referencable>;
23152315

23162316
// @public
23172317
export type ScopeableRequest = KibanaRequest | LegacyRequest | FakeRequest;

0 commit comments

Comments
 (0)