Skip to content

Commit

Permalink
Use indexTypesMap to determine what fields are SO types
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoldevila committed Jun 20, 2023
1 parent 281f4c9 commit a751935
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/

export {
getRootFields,
type RootField,
SavedObjectsRepository,
SavedObjectsClient,
SavedObjectsClientProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -140,9 +136,7 @@ function findChangedProp(actual: any, expected: any) {
* @returns {IndexMapping}
*/
export function getBaseMappings(): IndexMapping {
const baseMappings: IndexMapping & {
properties: Partial<Record<RootField, SavedObjectsFieldMapping>>;
} = {
return {
dynamic: 'strict',
properties: {
type: {
Expand Down Expand Up @@ -188,8 +182,6 @@ export function getBaseMappings(): IndexMapping {
},
},
};

return baseMappings;
}

function validateAndMerge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { FetchIndexResponse } from '../actions/fetch_indices';
import { BaseState } from '../state';
import {
addExcludedTypesToBoolQuery,
addMustClausesToBoolQuery,
Expand All @@ -20,6 +21,7 @@ import {
createBulkIndexOperationTuple,
hasLaterVersionAlias,
aliasVersion,
getIndexTypes,
} from './helpers';

describe('addExcludedTypesToBoolQuery', () => {
Expand Down Expand Up @@ -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']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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];
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -2630,12 +2630,12 @@ describe('migrations v2 model', () => {
should: [
{
term: {
type: 'type1',
type: 'dashboard',
},
},
{
term: {
type: 'type2',
type: 'lens',
},
},
],
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -48,6 +47,7 @@ import {
hasLaterVersionAlias,
aliasVersion,
REINDEX_TEMP_SUFFIX,
getIndexTypes,
} from './helpers';
import { buildTempIndexMap, createBatches } from './create_batches';
import type { MigrationLog } from '../types';
Expand Down Expand Up @@ -1425,10 +1425,12 @@ export const model = (currentState: State, resW: ResponseType<AllActionStates>):
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',
Expand All @@ -1437,7 +1439,7 @@ export const model = (currentState: State, resW: ResponseType<AllActionStates>):
...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}.`,
},
],
};
Expand Down

0 comments on commit a751935

Please sign in to comment.