@@ -143,26 +143,83 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
143
143
144
144
updateTable : async ( {
145
145
instanceName,
146
- tableName,
147
- reservedThroughput,
146
+ ...params
148
147
} : {
149
148
instanceName : string ;
150
149
tableName : string ;
150
+ primaryKey : Array < { name : string ; type : 'STRING' | 'INTEGER' | 'BINARY' } > ;
151
+ columns : Array < {
152
+ name : string ;
153
+ type : 'STRING' | 'INTEGER' | 'DOUBLE' | 'BOOLEAN' | 'BINARY' ;
154
+ } > ;
151
155
reservedThroughput : {
152
156
read : number ;
153
157
write : number ;
154
158
} ;
155
159
} ) => {
156
160
const client = loadTableStore ( instanceName , regionId , credentials ) ;
161
+ const {
162
+ tableMeta : { tableName, primaryKey, definedColumn : columns } ,
163
+ reservedThroughput,
164
+ } = formatOtsTableParam ( params ) ;
165
+
166
+ const table = await client . describeTable ( { tableName } ) ;
167
+ if ( ! table ?. tableMeta ?. tableName ) {
168
+ throw new Error ( `Table ${ tableName } does not exist in instance ${ instanceName } ` ) ;
169
+ }
170
+ if ( table ?. tableMeta ?. tableName !== tableName ) {
171
+ throw new Error (
172
+ `Table name mismatch: expected ${ tableName } , but got ${ table ?. tableMeta ?. tableName } ` ,
173
+ ) ;
174
+ }
157
175
158
- const result = await client . updateTable ( {
159
- tableName : tableName ,
160
- tableOptions : {
161
- timeToLive : - 1 , // 永不过期
162
- maxVersions : 1 , // 只保留最新版本
176
+ const tableDiff = table . tableMeta . primaryKey . filter ( ( pk ) =>
177
+ primaryKey . find ( ( { name, type } ) => name === pk . name && type === pk . type ) ,
178
+ ) ;
179
+
180
+ if ( tableDiff . length !== table . tableMeta . primaryKey . length ) {
181
+ throw new Error (
182
+ `Primary update not support: expected ${ JSON . stringify ( table . tableMeta . primaryKey ) } , but got ${ JSON . stringify (
183
+ primaryKey ,
184
+ ) } `,
185
+ ) ;
186
+ }
187
+
188
+ const columnsDiff = ( table . tableMeta . definedColumn || [ ] ) . reduce (
189
+ ( acc , existingCol ) => {
190
+ const matchingCol = columns . find ( ( col ) => col . name === existingCol . name ) ;
191
+
192
+ if ( ! matchingCol ) {
193
+ // Column exists in table but not in new columns - delete
194
+ acc . deletedColumns . push ( existingCol ) ;
195
+ } else if ( matchingCol . type !== existingCol . type ) {
196
+ // Column exists in both but type is different
197
+ acc . typeChangedColumns . push ( matchingCol ) ;
198
+ }
199
+
200
+ return acc ;
163
201
} ,
164
- reservedThroughput : { capacityUnit : reservedThroughput } ,
165
- } ) ;
202
+ {
203
+ newColumns : columns . filter (
204
+ ( col ) =>
205
+ ! ( table . tableMeta . definedColumn || [ ] ) . find ( ( existing ) => existing . name === col . name ) ,
206
+ ) ,
207
+ deletedColumns : [ ] as typeof table . tableMeta . definedColumn ,
208
+ modifiedColumns : [ ] as typeof columns ,
209
+ } ,
210
+ ) ;
211
+
212
+ if ( columnsDiff . modifiedColumns . length ) {
213
+ throw new Error (
214
+ `Column update not supported, please revert the changes for ${ JSON . stringify (
215
+ columnsDiff . modifiedColumns . map ( ( { name } ) => name ) ,
216
+ ) } `,
217
+ ) ;
218
+ }
219
+
220
+ // @TODO : Handle column deletion and addition if needed
221
+
222
+ const result = await client . updateTable ( tableParam ) ;
166
223
167
224
logger . info ( result , `Table updated successfully` ) ;
168
225
0 commit comments