2020import { Transform , Readable } from 'stream' ;
2121import { inspect } from 'util' ;
2222
23- import { get , once } from 'lodash' ;
2423import { Client } from 'elasticsearch' ;
2524import { ToolingLog } from '@kbn/dev-utils' ;
2625
@@ -54,7 +53,7 @@ export function createCreateIndexStream({
5453 // If we're trying to import Kibana index docs, we need to ensure that
5554 // previous indices are removed so we're starting w/ a clean slate for
5655 // migrations. This only needs to be done once per archive load operation.
57- const deleteKibanaIndicesOnce = once ( deleteKibanaIndices ) ;
56+ let kibanaIndexAlreadyDeleted = false ;
5857
5958 async function handleDoc ( stream : Readable , record : DocRecord ) {
6059 if ( skipDocsFromIndices . has ( record . value . index ) ) {
@@ -73,8 +72,9 @@ export function createCreateIndexStream({
7372
7473 async function attemptToCreate ( attemptNumber = 1 ) {
7574 try {
76- if ( isKibana ) {
77- await deleteKibanaIndicesOnce ( { client, stats, log } ) ;
75+ if ( isKibana && ! kibanaIndexAlreadyDeleted ) {
76+ await deleteKibanaIndices ( { client, stats, log } ) ;
77+ kibanaIndexAlreadyDeleted = true ;
7878 }
7979
8080 await client . indices . create ( {
@@ -94,6 +94,7 @@ export function createCreateIndexStream({
9494 err ?. body ?. error ?. reason ?. includes ( 'index exists with the same name as the alias' ) &&
9595 attemptNumber < 3
9696 ) {
97+ kibanaIndexAlreadyDeleted = false ;
9798 const aliasStr = inspect ( aliases ) ;
9899 log . info (
99100 `failed to create aliases [${ aliasStr } ] because ES indicated an index/alias already exists, trying again`
@@ -102,10 +103,7 @@ export function createCreateIndexStream({
102103 return ;
103104 }
104105
105- if (
106- get ( err , 'body.error.type' ) !== 'resource_already_exists_exception' ||
107- attemptNumber >= 3
108- ) {
106+ if ( err ?. body ?. error ?. type !== 'resource_already_exists_exception' || attemptNumber >= 3 ) {
109107 throw err ;
110108 }
111109
0 commit comments