7
7
getDiffConfig,
8
8
getDiffIndexProfiles,
9
9
isEqualIndex,
10
+ prepareSearchIndexProfile,
10
11
} = require ( './indexService' ) ;
11
12
let _ ;
12
13
@@ -36,23 +37,23 @@ const setNameCollectionsScript = (keyspaceName, name, type) => {
36
37
37
38
const indexSearchProperties = [ 'searchIndexProfiles' , 'searchIndexOptions' ] ;
38
39
39
- const getDataSearchIndex = indexTab => {
40
+ const getDataSearchIndex = ( indexTab , dbVersion ) => {
40
41
return {
41
42
indexType : 'search' ,
42
43
columns : indexTab . searchIndexColumns ,
43
44
config : indexTab . searchIndexConfig ,
44
- profiles : getIndexProfiles ( indexTab . searchIndexProfiles ) ,
45
+ profiles : getIndexProfiles ( indexTab . searchIndexProfiles , dbVersion ) ,
45
46
options : indexTab . searchIndexOptions ,
46
47
ifNotExist : indexTab . searchIndexIfNotExist
47
48
} ;
48
49
}
49
50
50
- const getModifyDataSearchIndex = ( item , type ) => {
51
+ const getModifyDataSearchIndex = ( item , type , dbVersion ) => {
51
52
const itemData = [ ...indexSearchProperties , 'searchIndexIfNotExist' , 'searchIndexColumns' , 'searchIndexConfig' ]
52
53
. map ( property => ( [ property , ( item ?. compMod || { } ) [ property ] ?. [ type ] ] ) )
53
54
. filter ( ( [ __ , value ] ) => ! ! value ) ;
54
55
item = { ...item , ...Object . fromEntries ( itemData ) } ;
55
- return getDataSearchIndex ( item ) ;
56
+ return getDataSearchIndex ( item , dbVersion ) ;
56
57
}
57
58
58
59
const getDataForScript = ( newData , oldData , isEqual = _ . isEqual ) => {
@@ -78,13 +79,26 @@ const getDataForScript = (newData, oldData, isEqual = _.isEqual) => {
78
79
}
79
80
}
80
81
81
- const getDataForSearchIndexScript = role => {
82
+ const getDataForSearchIndexScript = ( role , dataSources , dbVersion ) => {
82
83
const { compMod } = role ;
83
- const dataSearchIndex = getDataSearchIndex ( role ) ;
84
+ const oldIdToNameHashTable = _ . get ( role , 'compMod.oldIdToNameHashTable' , { } ) ;
85
+ const columns = _ . get ( role , 'compMod.searchIndexColumns' , { } ) ;
86
+ const filterColumn = column => column ?. name && ! column ?. compositePartitionKey ;
87
+ const oldColumns = ( columns . old || [ ] )
88
+ . map ( column => getDataColumnIndex ( {
89
+ dataSources,
90
+ column,
91
+ allAttributes : true ,
92
+ idToNameHashTable : oldIdToNameHashTable ,
93
+ } ) )
94
+ . filter ( filterColumn ) ;
95
+ const dataSearchIndex = getDataSearchIndex ( role , dbVersion ) ;
84
96
const searchIndex = _ . get ( role , 'compMod.searchIndex' , { } ) ;
97
+ const newProfiles = compMod ?. searchIndexProfiles ?. new ;
98
+ const oldProfiles = prepareSearchIndexProfile ( compMod ?. searchIndexProfiles ?. old , newProfiles , oldColumns )
85
99
const searchPropertiesCompare = _ . merge (
86
100
getDiffOptions ( compMod ?. searchIndexOptions ?. old , compMod ?. searchIndexOptions ?. new ) ,
87
- getDiffIndexProfiles ( compMod ?. searchIndexProfiles ?. old , compMod ?. searchIndexProfiles ?. new ) ,
101
+ getDiffIndexProfiles ( oldProfiles , newProfiles ) ,
88
102
) ;
89
103
90
104
let dropData ;
@@ -95,11 +109,11 @@ const getDataForSearchIndexScript = role => {
95
109
}
96
110
97
111
if ( ! searchIndex . old ) {
98
- addData = getModifyDataSearchIndex ( role , 'new' ) ;
112
+ addData = getModifyDataSearchIndex ( role , 'new' , dbVersion ) ;
99
113
} else if ( ! searchIndex . new ) {
100
114
dropData = dataSearchIndex ;
101
115
} else if ( ! _ . isEmpty ( searchPropertiesCompare . modifyData ) || ! _ . isEmpty ( searchPropertiesCompare . dropData ) ) {
102
- addData = getModifyDataSearchIndex ( role , 'new' ) ;
116
+ addData = getModifyDataSearchIndex ( role , 'new' , dbVersion ) ;
103
117
dropData = dataSearchIndex ;
104
118
}
105
119
return {
@@ -108,8 +122,8 @@ const getDataForSearchIndexScript = role => {
108
122
} ;
109
123
}
110
124
111
- const getFieldDataByKeyId = ( dataSources , idToNameHashTable , keyId ) => {
112
- const fieldData = getNamesByIds ( [ keyId ] , dataSources ) [ keyId ] || { } ;
125
+ const getFieldDataByKeyId = ( { dataSources, idToNameHashTable, keyId, allAttributes = false } ) => {
126
+ const fieldData = getNamesByIds ( [ keyId ] , dataSources , allAttributes ) [ keyId ] || { } ;
113
127
if ( fieldData . name ) {
114
128
return fieldData
115
129
}
@@ -119,10 +133,16 @@ const getFieldDataByKeyId = (dataSources, idToNameHashTable, keyId) => {
119
133
return { name } ;
120
134
}
121
135
122
- const getDataColumnIndex = ( dataSources , idToNameHashTable , column = { } , key = 'key' ) => {
136
+ const getDataColumnIndex = ( {
137
+ dataSources,
138
+ idToNameHashTable,
139
+ column = { } ,
140
+ key = 'key' ,
141
+ allAttributes = false ,
142
+ } ) => {
123
143
setDependencies ( dependencies ) ;
124
144
const keyId = _ . get ( column , `${ key } [0].keyId` , '' ) ;
125
- const fieldData = getFieldDataByKeyId ( dataSources , idToNameHashTable , keyId ) ;
145
+ const fieldData = getFieldDataByKeyId ( { dataSources, idToNameHashTable, keyId, allAttributes } ) || { } ;
126
146
127
147
return {
128
148
..._ . omit ( column , key ) ,
@@ -136,9 +156,11 @@ const getDataForSearchIndexColumns = (item, dataSources) => {
136
156
const columns = _ . get ( item , 'role.compMod.searchIndexColumns' , { } ) ;
137
157
const filterColumn = column => column . name && ! column . compositePartitionKey ;
138
158
const newColumns = ( columns . new || [ ] )
139
- . map ( column => getDataColumnIndex ( dataSources , newIdToNameHashTable , column ) ) . filter ( filterColumn ) ;
159
+ . map ( column => getDataColumnIndex ( { dataSources, idToNameHashTable : newIdToNameHashTable , column } ) )
160
+ . filter ( filterColumn ) ;
140
161
const oldColumns = ( columns . old || [ ] )
141
- . map ( column => getDataColumnIndex ( dataSources , oldIdToNameHashTable , column ) ) . filter ( filterColumn ) ;
162
+ . map ( column => getDataColumnIndex ( { dataSources, idToNameHashTable : oldIdToNameHashTable , column } ) )
163
+ . filter ( filterColumn ) ;
142
164
const isEqual = ( oldValue , newValue ) => oldValue ?. name === newValue ?. name ;
143
165
return getDataForScript ( newColumns , oldColumns , isEqual ) ;
144
166
}
@@ -280,7 +302,7 @@ const getAddIndexScript = data => {
280
302
const getCreatedIndex = data => {
281
303
const { item, dataSources, tableName, keyspaceName, isActivated, dbVersion } = data ;
282
304
const isSearchIndex = ! ! item . role ?. searchIndex ;
283
- const dataForSearchIndexScript = getDataSearchIndex ( item . role || { } ) ;
305
+ const dataForSearchIndexScript = getDataSearchIndex ( item . role || { } , dbVersion ) ;
284
306
285
307
const script = getIndexes ( {
286
308
searchIndex : isSearchIndex && dataForSearchIndexScript ,
@@ -314,7 +336,7 @@ const getDeletedIndex = data => {
314
336
const getUpdateSearchIndexScript = data => {
315
337
const { item, keyspaceName, tableName, dbVersion, isActivated, dataSources } = data ;
316
338
317
- const dataForScript = getDataForSearchIndexScript ( item . role ) ;
339
+ const dataForScript = getDataForSearchIndexScript ( item . role , dataSources , dbVersion ) ;
318
340
319
341
if ( dataForScript . dropData || dataForScript . addData ) {
320
342
const dropIndexSearchScript = getDropSearchIndexScript (
@@ -361,7 +383,7 @@ const prepareIndexes = (idToNameHashTable, dataSources, indexes = []) => {
361
383
return indexes . map ( index => {
362
384
const secIndexesKey = _ . get ( index , 'SecIndxKey' , [ ] ) . map ( key => ( {
363
385
...key ,
364
- name : getFieldDataByKeyId ( dataSources , idToNameHashTable , _ . get ( key , 'keyId' ) ) ?. name || '' ,
386
+ name : getFieldDataByKeyId ( { dataSources, idToNameHashTable, keyId : _ . get ( key , 'keyId' ) } ) ?. name || '' ,
365
387
} )
366
388
) ;
367
389
return {
0 commit comments