|
1
|
import { SearchTypes } from "@medusajs/framework/types" |
|
2
|
import { |
|
3
|
SearchIndexContext, |
|
4
|
SearchIndexRecord, |
|
5
|
SearchIndexRegistry, |
|
6
|
SearchIndexVersionRecord, |
|
7
|
} from "@types" |
|
8
|
import { retrieveIndexDefinition } from "./index" |
|
9
|
|
|
10
|
type DeletionContext = SearchIndexRegistry & |
|
11
|
Pick<SearchIndexContext, "syncService" | "locking" | "activeVersionCache"> |
|
12
|
|
|
13
|
/** |
|
14
|
* Drops every physical index ever built for a logical index and hard deletes the |
|
15
|
* rows behind them, so the next migration plans a `create` and rebuilds from |
|
16
|
* version 1. Soft deleting would leave the numbering to carry on instead. |
|
17
|
*/ |
|
18
|
export async function deleteIndexEntirely( |
|
19
|
context: DeletionContext, |
|
20
|
name: string |
|
21
|
): Promise<SearchTypes.SearchIndexDeleteResult> { |
|
22
|
retrieveIndexDefinition(context.indexes, name) |
|
23
|
|
|
24
|
// The lock a seed and a reindex take, so this cannot land halfway through one. |
|
25
|
if (context.locking) { |
|
26
|
return await context.locking.execute(`search:seed:${name}`, () => |
|
27
|
deleteOne(context, name) |
|
28
|
) |
|
29
|
} |
|
30
|
|
|
31
|
return await deleteOne(context, name) |
|
32
|
} |
|
33
|
|
|
34
|
async function deleteOne( |
|
35
|
context: DeletionContext, |
|
36
|
name: string |
|
37
|
): Promise<SearchTypes.SearchIndexDeleteResult> { |
|
38
|
const [record] = (await context.indexService.list( |
|
39
|
{ name }, |
|
40
|
{ take: 1, withDeleted: true } |
|
41
|
)) as SearchIndexRecord[] |
|
42
|
|
|
43
|
if (!record) { |
|
44
|
return { index: name, deleted_versions: 0 } |
|
45
|
} |
|
46
|
|
|
47
|
// `withDeleted` throughout: a version an earlier cleanup soft deleted still |
|
48
|
// owns a row and a physical index, and leaving either behind is what this is |
|
49
|
// meant to avoid. |
|
50
|
const versions = (await context.versionService.list( |
|
51
|
{ search_index_id: record.id }, |
|
52
|
{ take: null, withDeleted: true } |
|
53
|
)) as SearchIndexVersionRecord[] |
|
54
|
|
|
55
|
for (const version of versions) { |
|
56
|
try { |
|
57
|
const provider = context.providers.retrieve(version.provider) |
|
58
|
|
|
59
|
await provider.deleteIndex({ index: version.physical_name }) |
|
60
|
} catch (error) { |
|
61
|
// The rows go either way: keeping them for an index the engine may no |
|
62
|
// longer have is what stops it from ever being rebuilt. |
|
63
|
context.logger.warn( |
|
64
|
`[Search] Cannot drop physical search index "${version.physical_name}": ${error.message}` |
|
65
|
) |
|
66
|
} |
|
67
|
} |
|
68
|
|
|
69
|
const versionIds = versions.map((version) => version.id) |
|
70
|
|
|
71
|
if (versionIds.length) { |
|
72
|
const syncs = (await context.syncService.list( |
|
73
|
{ search_index_version_id: versionIds }, |
|
74
|
{ take: null, select: ["id"], withDeleted: true } |
|
75
|
)) as { id: string }[] |
|
76
|
|
|
77
|
if (syncs.length) { |
|
78
|
await context.syncService.delete(syncs.map((sync) => sync.id)) |
|
79
|
} |
|
80
|
|
|
81
|
await context.versionService.delete(versionIds) |
|
82
|
} |
|
83
|
|
|
84
|
await context.indexService.delete([record.id]) |
|
85
|
|
|
86
|
await context.activeVersionCache?.invalidate() |
|
87
|
|
|
88
|
return { index: name, deleted_versions: versions.length } |
|
89
|
} |