@@ -85,7 +85,6 @@ export async function installIndexPatterns(
8585 savedObjectsClient ,
8686 InstallationStatus . installed
8787 ) ;
88-
8988 // TODO: move to install package
9089 // cache all installed packages if they don't exist
9190 const packagePromises = installedPackages . map ( ( pkg ) =>
@@ -95,26 +94,32 @@ export async function installIndexPatterns(
9594 ) ;
9695 await Promise . all ( packagePromises ) ;
9796
97+ const packageVersionsToFetch = [ ...installedPackages ] ;
9898 if ( pkgName && pkgVersion ) {
99- // add this package to the array if it doesn't already exist
100- const foundPkg = installedPackages . find ( ( pkg ) => pkg . pkgName === pkgName ) ;
101- // this may be removed if we add the packged to saved objects before installing index patterns
102- // otherwise this is a first time install
103- // TODO: handle update case when versions are different
104- if ( ! foundPkg ) {
105- installedPackages . push ( { pkgName, pkgVersion } ) ;
99+ const packageToInstall = packageVersionsToFetch . find ( ( pkg ) => pkg . pkgName === pkgName ) ;
100+
101+ if ( packageToInstall ) {
102+ // set the version to the one we want to install
103+ // if we're installing for the first time the number will be the same
104+ // if this is an upgrade then we'll be modifying the version number to the upgrade version
105+ packageToInstall . pkgVersion = pkgVersion ;
106+ } else {
107+ // this will likely not happen because the saved objects should already have the package we're trying
108+ // install which means that it should have been found in the case above
109+ packageVersionsToFetch . push ( { pkgName, pkgVersion } ) ;
106110 }
107111 }
108112 // get each package's registry info
109- const installedPackagesFetchInfoPromise = installedPackages . map ( ( pkg ) =>
113+ const packageVersionsFetchInfoPromise = packageVersionsToFetch . map ( ( pkg ) =>
110114 Registry . fetchInfo ( pkg . pkgName , pkg . pkgVersion )
111115 ) ;
112- const installedPackagesInfo = await Promise . all ( installedPackagesFetchInfoPromise ) ;
116+
117+ const packageVersionsInfo = await Promise . all ( packageVersionsFetchInfoPromise ) ;
113118
114119 // for each index pattern type, create an index pattern
115120 const indexPatternTypes = [ IndexPatternType . logs , IndexPatternType . metrics ] ;
116121 indexPatternTypes . forEach ( async ( indexPatternType ) => {
117- // if this is an update because a package is being unisntalled (no pkgkey argument passed) and no other packages are installed, remove the index pattern
122+ // if this is an update because a package is being uninstalled (no pkgkey argument passed) and no other packages are installed, remove the index pattern
118123 if ( ! pkgName && installedPackages . length === 0 ) {
119124 try {
120125 await savedObjectsClient . delete ( INDEX_PATTERN_SAVED_OBJECT_TYPE , `${ indexPatternType } -*` ) ;
@@ -125,8 +130,7 @@ export async function installIndexPatterns(
125130 }
126131
127132 // get all data stream fields from all installed packages
128- const fields = await getAllDataStreamFieldsByType ( installedPackagesInfo , indexPatternType ) ;
129-
133+ const fields = await getAllDataStreamFieldsByType ( packageVersionsInfo , indexPatternType ) ;
130134 const kibanaIndexPattern = createIndexPattern ( indexPatternType , fields ) ;
131135 // create or overwrite the index pattern
132136 await savedObjectsClient . create ( INDEX_PATTERN_SAVED_OBJECT_TYPE , kibanaIndexPattern , {
0 commit comments