Skip to content

Commit fe22a38

Browse files
committed
chore(swift): add cleanup method to replace all objects helper
1 parent 0179bdb commit fe22a38

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -543,51 +543,57 @@ public extension SearchClient {
543543
) async throws -> ReplaceAllObjectsResponse {
544544
let tmpIndexName = "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"
545545

546-
var copyOperationResponse = try await operationIndex(
547-
indexName: indexName,
548-
operationIndexParams: OperationIndexParams(
549-
operation: .copy,
550-
destination: tmpIndexName,
551-
scope: [.settings, .rules, .synonyms]
552-
),
553-
requestOptions: requestOptions
554-
)
546+
do {
547+
var copyOperationResponse = try await operationIndex(
548+
indexName: indexName,
549+
operationIndexParams: OperationIndexParams(
550+
operation: .copy,
551+
destination: tmpIndexName,
552+
scope: [.settings, .rules, .synonyms]
553+
),
554+
requestOptions: requestOptions
555+
)
555556

556-
let batchResponses = try await self.chunkedBatch(
557-
indexName: tmpIndexName,
558-
objects: objects,
559-
waitForTasks: true,
560-
batchSize: batchSize,
561-
requestOptions: requestOptions
562-
)
563-
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
557+
let batchResponses = try await self.chunkedBatch(
558+
indexName: tmpIndexName,
559+
objects: objects,
560+
waitForTasks: true,
561+
batchSize: batchSize,
562+
requestOptions: requestOptions
563+
)
564+
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
564565

565-
copyOperationResponse = try await operationIndex(
566-
indexName: indexName,
567-
operationIndexParams: OperationIndexParams(
568-
operation: .copy,
569-
destination: tmpIndexName,
570-
scope: [.settings, .rules, .synonyms]
571-
),
572-
requestOptions: requestOptions
573-
)
574-
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
575-
576-
let moveOperationResponse = try await self.operationIndex(
577-
indexName: tmpIndexName,
578-
operationIndexParams: OperationIndexParams(
579-
operation: .move,
580-
destination: indexName
581-
),
582-
requestOptions: requestOptions
583-
)
584-
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)
566+
copyOperationResponse = try await operationIndex(
567+
indexName: indexName,
568+
operationIndexParams: OperationIndexParams(
569+
operation: .copy,
570+
destination: tmpIndexName,
571+
scope: [.settings, .rules, .synonyms]
572+
),
573+
requestOptions: requestOptions
574+
)
575+
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
585576

586-
return ReplaceAllObjectsResponse(
587-
copyOperationResponse: copyOperationResponse,
588-
batchResponses: batchResponses,
589-
moveOperationResponse: moveOperationResponse
590-
)
577+
let moveOperationResponse = try await self.operationIndex(
578+
indexName: tmpIndexName,
579+
operationIndexParams: OperationIndexParams(
580+
operation: .move,
581+
destination: indexName
582+
),
583+
requestOptions: requestOptions
584+
)
585+
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)
586+
587+
return ReplaceAllObjectsResponse(
588+
copyOperationResponse: copyOperationResponse,
589+
batchResponses: batchResponses,
590+
moveOperationResponse: moveOperationResponse
591+
)
592+
} catch {
593+
_ = try? await deleteIndex(indexName: tmpIndexName)
594+
595+
throw error
596+
}
591597
}
592598

593599
/// Generate a secured API key

scripts/cts/testServer/replaceAllObjectsFailed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import type { Express } from 'express';
55
import express from 'express';
66

7-
import { setupServer } from '.';
7+
import { setupServer } from './index.js';
88

99
const raoState: Record<
1010
string,

0 commit comments

Comments
 (0)