import * as Option from 'fp-ts/Option' ; import { IndexMapping } from '../mappings' ; import { SavedObjectsMigrationVersion } from '../../../types' ; import { SavedObjectsMigrationConfigType } from '../saved_objects_config' ; import type { ISavedObjectTypeRegistry } from '../saved_objects_type_registry' ; import { InitState } from './state' ; import { excludeUnusedTypesQuery } from '../migrations/core' ; /**
 * Construct the initial state for the model
 */ export const createInitialState = ( { kibanaVersion , targetMappings , preMigrationScript , migrationVersionPerType , indexPrefix , migrationsConfig , typeRegistry , } ) : InitState => { const outdatedDocumentsQuery = { bool : { should : Object . entries ( migrationVersionPerType ) . map ( ( [ type , latestVersion ] ) => ( { bool : { must : { term : { type } } , must_not : { term : { [ `migrationVersion.${ type }` ] : latestVersion } } , } , } ) ) , } , } ; const reindexTargetMappings = { dynamic : false , properties : { type : { type : 'keyword' } , migrationVersion : { dynamic : 'true' , type : 'object' , } , } , } ; const knownTypes = typeRegistry . getAllTypes ( ) . map ( ( type ) => type . name ) ; const excludeFilterHooks = Object . fromEntries ( typeRegistry . getAllTypes ( ) . filter ( ( type ) => ! ! type . excludeOnUpgrade ) . map ( ( type ) => [ type . name , type . excludeOnUpgrade ! ] ) ) ; return { controlState : 'INIT' , indexPrefix , legacyIndex : indexPrefix , currentAlias : indexPrefix , versionAlias : `${ indexPrefix }_${ kibanaVersion }` , versionIndex : `${ indexPrefix }_${ kibanaVersion }_001` , tempIndex : `${ indexPrefix }_${ kibanaVersion }_reindex_temp` , kibanaVersion , preMigrationScript : Option . fromNullable ( preMigrationScript ) , targetIndexMappings : targetMappings , tempIndexMappings : reindexTargetMappings , outdatedDocumentsQuery , retryCount : 0 , retryDelay : 0 , retryAttempts : migrationsConfig . retryAttempts , batchSize : migrationsConfig . batchSize , maxBatchSizeBytes : migrationsConfig . maxBatchSizeBytes . getValueInBytes ( ) , logs : [ ] , unusedTypesQuery : excludeUnusedTypesQuery , knownTypes , excludeFromUpgradeFilterHooks : excludeFilterHooks , } ; } ;