diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/index.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/index.ts index 383ab648b3fd1..73b3bc2777aae 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/index.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/index.ts @@ -7,8 +7,6 @@ */ export { - getRootFields, - type RootField, SavedObjectsRepository, SavedObjectsClient, SavedObjectsClientProvider, diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/index.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/index.ts index bc8caf6189705..38811b1948551 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/index.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/index.ts @@ -7,11 +7,5 @@ */ export { SavedObjectsClient } from './saved_objects_client'; -export { - getRootFields, - type RootField, - SavedObjectsClientProvider, - SavedObjectsRepository, - PointInTimeFinder, -} from './lib'; +export { SavedObjectsClientProvider, SavedObjectsRepository, PointInTimeFinder } from './lib'; export type { ISavedObjectsClientProvider } from './lib'; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/index.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/index.ts index 512f4f72082cb..cfe52298c7386 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/index.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/index.ts @@ -10,4 +10,3 @@ export { SavedObjectsRepository } from './repository'; export { SavedObjectsClientProvider } from './scoped_client_provider'; export { PointInTimeFinder } from './point_in_time_finder'; export type { ISavedObjectsClientProvider } from './scoped_client_provider'; -export { getRootFields, type RootField } from './utils/included_fields'; diff --git a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/utils/included_fields.ts b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/utils/included_fields.ts index 897c82b2ea442..50686def81c88 100644 --- a/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/utils/included_fields.ts +++ b/packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/utils/included_fields.ts @@ -18,11 +18,9 @@ const ROOT_FIELDS = [ 'updated_at', 'created_at', 'originId', -] as const; +]; -export type RootField = typeof ROOT_FIELDS[number]; - -export function getRootFields(): string[] { +export function getRootFields() { return [...ROOT_FIELDS]; } diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/core/build_active_mappings.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/core/build_active_mappings.ts index 3ba5a52a117d9..72b06507ec402 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/core/build_active_mappings.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/core/build_active_mappings.ts @@ -12,15 +12,11 @@ import crypto from 'crypto'; import { cloneDeep, mapValues } from 'lodash'; -import type { - SavedObjectsFieldMapping, - SavedObjectsMappingProperties, -} from '@kbn/core-saved-objects-server'; +import type { SavedObjectsMappingProperties } from '@kbn/core-saved-objects-server'; import type { IndexMapping, SavedObjectsTypeMappingDefinitions, } from '@kbn/core-saved-objects-base-server-internal'; -import type { RootField } from '@kbn/core-saved-objects-api-server-internal'; /** * Creates an index mapping with the core properties required by saved object @@ -140,9 +136,7 @@ function findChangedProp(actual: any, expected: any) { * @returns {IndexMapping} */ export function getBaseMappings(): IndexMapping { - const baseMappings: IndexMapping & { - properties: Partial>; - } = { + return { dynamic: 'strict', properties: { type: { @@ -188,8 +182,6 @@ export function getBaseMappings(): IndexMapping { }, }, }; - - return baseMappings; } function validateAndMerge( diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.test.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.test.ts index edf426d7be973..382eedfe0c2d8 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.test.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.test.ts @@ -7,6 +7,7 @@ */ import { FetchIndexResponse } from '../actions/fetch_indices'; +import { BaseState } from '../state'; import { addExcludedTypesToBoolQuery, addMustClausesToBoolQuery, @@ -20,6 +21,7 @@ import { createBulkIndexOperationTuple, hasLaterVersionAlias, aliasVersion, + getIndexTypes, } from './helpers'; describe('addExcludedTypesToBoolQuery', () => { @@ -444,3 +446,17 @@ describe('getTempIndexName', () => { expect(getTempIndexName('.kibana_cases', '8.8.0')).toEqual('.kibana_cases_8.8.0_reindex_temp'); }); }); + +describe('getIndexTypes', () => { + it("returns the list of types that belong to a migrator's index, based on its state", () => { + const baseState = { + indexPrefix: '.kibana_task_manager', + indexTypesMap: { + '.kibana': ['foo', 'bar'], + '.kibana_task_manager': ['task'], + }, + }; + + expect(getIndexTypes(baseState as unknown as BaseState)).toEqual(['task']); + }); +}); diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.ts index d3e8fcabfc432..4588c6639d883 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/helpers.ts @@ -17,7 +17,7 @@ import type { SavedObjectsRawDoc } from '@kbn/core-saved-objects-server'; import type { IndexMapping } from '@kbn/core-saved-objects-base-server-internal'; import type { AliasAction, FetchIndexResponse } from '../actions'; import type { BulkIndexOperationTuple } from './create_batches'; -import { OutdatedDocumentsSearchRead, ReindexSourceToTempRead } from '../state'; +import type { BaseState, OutdatedDocumentsSearchRead, ReindexSourceToTempRead } from '../state'; /** @internal */ export const REINDEX_TEMP_SUFFIX = '_reindex_temp'; @@ -323,3 +323,7 @@ export const increaseBatchSize = ( const increasedBatchSize = Math.floor(stateP.batchSize * 1.2); return increasedBatchSize > stateP.maxBatchSize ? stateP.maxBatchSize : increasedBatchSize; }; + +export const getIndexTypes = (state: BaseState): string[] => { + return state.indexTypesMap[state.indexPrefix]; +}; diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.test.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.test.ts index 5fbd8a42d2e1a..60a7dbe7f11aa 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.test.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.test.ts @@ -2616,7 +2616,7 @@ describe('migrations v2 model', () => { it('CHECK_TARGET_MAPPINGS -> UPDATE_TARGET_MAPPINGS_PROPERTIES if mappings do not match', () => { const res: ResponseType<'CHECK_TARGET_MAPPINGS'> = Either.right({ match: false, - updatedHashes: ['type1', 'type2'], + updatedHashes: ['dashboard', 'lens'], }); const newState = model( checkTargetMappingsState, @@ -2630,12 +2630,12 @@ describe('migrations v2 model', () => { should: [ { term: { - type: 'type1', + type: 'dashboard', }, }, { term: { - type: 'type2', + type: 'lens', }, }, ], @@ -2646,7 +2646,7 @@ describe('migrations v2 model', () => { it('CHECK_TARGET_MAPPINGS -> UPDATE_TARGET_MAPPINGS_PROPERTIES if core fields have been updated', () => { const res: ResponseType<'CHECK_TARGET_MAPPINGS'> = Either.right({ match: false, - updatedHashes: ['type1', 'type2', 'namespaces'], // 'namespaces' is a root field (core, common to all SOs) + updatedHashes: ['dashboard', 'lens', 'namespaces'], // 'namespaces' is a root field (core, common to all SOs) }); const newState = model( checkTargetMappingsState, diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.ts index e0ab2e22fd30b..9da3ba19fcd4e 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/model/model.ts @@ -9,7 +9,6 @@ import * as Either from 'fp-ts/lib/Either'; import * as Option from 'fp-ts/lib/Option'; import type { IndexMapping } from '@kbn/core-saved-objects-base-server-internal'; -import { getRootFields } from '@kbn/core-saved-objects-api-server-internal'; import { isTypeof } from '../actions'; import type { AliasAction } from '../actions'; @@ -48,6 +47,7 @@ import { hasLaterVersionAlias, aliasVersion, REINDEX_TEMP_SUFFIX, + getIndexTypes, } from './helpers'; import { buildTempIndexMap, createBatches } from './create_batches'; import type { MigrationLog } from '../types'; @@ -1425,10 +1425,12 @@ export const model = (currentState: State, resW: ResponseType): if (Either.isRight(res)) { if (!res.right.match) { if (res.right.updatedHashes?.length) { - const updatedRootFields = res.right.updatedHashes.filter((field) => - getRootFields().includes(field) + const typeFields = getIndexTypes(stateP); + const nonTypeFields = res.right.updatedHashes.filter( + (field) => !typeFields.includes(field) ); - if (updatedRootFields.length) { + console.log('TYPE vs NON_TYPE', typeFields, nonTypeFields); + if (nonTypeFields.length) { return { ...stateP, controlState: 'UPDATE_TARGET_MAPPINGS_PROPERTIES', @@ -1437,7 +1439,7 @@ export const model = (currentState: State, resW: ResponseType): ...stateP.logs, { level: 'info', - message: `Kibana is performing a compatible update and some root fields have been updated. All SO documents must be updated. Updated root fields: ${updatedRootFields}.`, + message: `Kibana is performing a compatible update and some root fields have been updated. All SO documents must be updated. Updated root fields: ${nonTypeFields}.`, }, ], };