Skip to content

Commit b9dbffd

Browse files
committed
chore(swift): add cleanup method to replace all objects helper
1 parent 2f9995f commit b9dbffd

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
@@ -549,51 +549,57 @@ public extension SearchClient {
549549
) async throws -> ReplaceAllObjectsResponse {
550550
let tmpIndexName = "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"
551551

552-
var copyOperationResponse = try await operationIndex(
553-
indexName: indexName,
554-
operationIndexParams: OperationIndexParams(
555-
operation: .copy,
556-
destination: tmpIndexName,
557-
scope: [.settings, .rules, .synonyms]
558-
),
559-
requestOptions: requestOptions
560-
)
552+
do {
553+
var copyOperationResponse = try await operationIndex(
554+
indexName: indexName,
555+
operationIndexParams: OperationIndexParams(
556+
operation: .copy,
557+
destination: tmpIndexName,
558+
scope: [.settings, .rules, .synonyms]
559+
),
560+
requestOptions: requestOptions
561+
)
561562

562-
let batchResponses = try await self.chunkedBatch(
563-
indexName: tmpIndexName,
564-
objects: objects,
565-
waitForTasks: true,
566-
batchSize: batchSize,
567-
requestOptions: requestOptions
568-
)
569-
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
563+
let batchResponses = try await self.chunkedBatch(
564+
indexName: tmpIndexName,
565+
objects: objects,
566+
waitForTasks: true,
567+
batchSize: batchSize,
568+
requestOptions: requestOptions
569+
)
570+
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
570571

571-
copyOperationResponse = try await operationIndex(
572-
indexName: indexName,
573-
operationIndexParams: OperationIndexParams(
574-
operation: .copy,
575-
destination: tmpIndexName,
576-
scope: [.settings, .rules, .synonyms]
577-
),
578-
requestOptions: requestOptions
579-
)
580-
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
581-
582-
let moveOperationResponse = try await self.operationIndex(
583-
indexName: tmpIndexName,
584-
operationIndexParams: OperationIndexParams(
585-
operation: .move,
586-
destination: indexName
587-
),
588-
requestOptions: requestOptions
589-
)
590-
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)
572+
copyOperationResponse = try await operationIndex(
573+
indexName: indexName,
574+
operationIndexParams: OperationIndexParams(
575+
operation: .copy,
576+
destination: tmpIndexName,
577+
scope: [.settings, .rules, .synonyms]
578+
),
579+
requestOptions: requestOptions
580+
)
581+
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
591582

592-
return ReplaceAllObjectsResponse(
593-
copyOperationResponse: copyOperationResponse,
594-
batchResponses: batchResponses,
595-
moveOperationResponse: moveOperationResponse
596-
)
583+
let moveOperationResponse = try await self.operationIndex(
584+
indexName: tmpIndexName,
585+
operationIndexParams: OperationIndexParams(
586+
operation: .move,
587+
destination: indexName
588+
),
589+
requestOptions: requestOptions
590+
)
591+
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)
592+
593+
return ReplaceAllObjectsResponse(
594+
copyOperationResponse: copyOperationResponse,
595+
batchResponses: batchResponses,
596+
moveOperationResponse: moveOperationResponse
597+
)
598+
} catch {
599+
_ = try? await deleteIndex(indexName: tmpIndexName)
600+
601+
throw error
602+
}
597603
}
598604

599605
/// 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)