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 ) ) {
@@ -70,8 +69,9 @@ export function createCreateIndexStream({
7069
7170 async function attemptToCreate ( attemptNumber = 1 ) {
7271 try {
73- if ( isKibana ) {
74- await deleteKibanaIndicesOnce ( { client, stats, log } ) ;
72+ if ( isKibana && ! kibanaIndexAlreadyDeleted ) {
73+ await deleteKibanaIndices ( { client, stats, log } ) ;
74+ kibanaIndexAlreadyDeleted = true ;
7575 }
7676
7777 await client . indices . create ( {
@@ -90,6 +90,7 @@ export function createCreateIndexStream({
9090 err ?. body ?. error ?. reason ?. includes ( 'index exists with the same name as the alias' ) &&
9191 attemptNumber < 3
9292 ) {
93+ kibanaIndexAlreadyDeleted = false ;
9394 const aliasStr = inspect ( aliases ) ;
9495 log . info (
9596 `failed to create aliases [${ aliasStr } ] because ES indicated an index/alias already exists, trying again`
@@ -98,10 +99,7 @@ export function createCreateIndexStream({
9899 return ;
99100 }
100101
101- if (
102- get ( err , 'body.error.type' ) !== 'resource_already_exists_exception' ||
103- attemptNumber >= 3
104- ) {
102+ if ( err ?. body ?. error ?. type !== 'resource_already_exists_exception' || attemptNumber >= 3 ) {
105103 throw err ;
106104 }
107105
0 commit comments