@@ -155,15 +155,16 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
155
155
let mut added_modules = BTreeSet :: new ( ) ;
156
156
157
157
// Add new modules.
158
- let mut remaining_modules: BTreeSet < _ > = self
158
+ let mut remaining_modules: BTreeMap < _ , _ > = self
159
159
. get_application_metadata ( component)
160
160
. await ?
161
161
. into_iter ( )
162
- . map ( |module| module. into_value ( ) . path )
162
+ . map ( |module| ( module. path . clone ( ) , module . id ( ) ) )
163
163
. collect ( ) ;
164
164
for module in modules {
165
165
let path = module. path . canonicalize ( ) ;
166
- if !remaining_modules. remove ( & path) {
166
+ let existing_module_id = remaining_modules. remove ( & path) ;
167
+ if existing_module_id. is_none ( ) {
167
168
added_modules. insert ( path. clone ( ) ) ;
168
169
}
169
170
let analyze_result = if !path. is_deps ( ) {
@@ -178,6 +179,7 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
178
179
None
179
180
} ;
180
181
self . put (
182
+ existing_module_id,
181
183
CanonicalizedComponentModulePath {
182
184
component,
183
185
module_path : path. clone ( ) ,
@@ -192,14 +194,9 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
192
194
}
193
195
194
196
let mut removed_modules = BTreeSet :: new ( ) ;
195
- for path in remaining_modules {
197
+ for ( path, module_id ) in remaining_modules {
196
198
removed_modules. insert ( path. clone ( ) ) ;
197
- ModuleModel :: new ( self . tx )
198
- . delete ( CanonicalizedComponentModulePath {
199
- component,
200
- module_path : path,
201
- } )
202
- . await ?;
199
+ self . delete ( component, module_id) . await ?;
203
200
}
204
201
ModuleDiff :: new ( added_modules, removed_modules)
205
202
}
@@ -310,8 +307,10 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
310
307
}
311
308
312
309
/// Put a module's source at a given path.
310
+ /// `module_id` is the existing module at this `path`.
313
311
pub async fn put (
314
312
& mut self ,
313
+ module_id : Option < ResolvedDocumentId > ,
315
314
path : CanonicalizedComponentModulePath ,
316
315
source : ModuleSource ,
317
316
source_package_id : SourcePackageId ,
@@ -330,43 +329,42 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
330
329
"AnalyzedModule is required for non-dependency modules"
331
330
) ;
332
331
let sha256 = hash_module_source ( & source, source_map. as_ref ( ) ) ;
333
- self . put_module_metadata ( path, source_package_id, analyze_result, environment, sha256)
334
- . await ?;
332
+ self . put_module_metadata (
333
+ module_id,
334
+ path,
335
+ source_package_id,
336
+ analyze_result,
337
+ environment,
338
+ sha256,
339
+ )
340
+ . await ?;
335
341
Ok ( ( ) )
336
342
}
337
343
338
344
async fn put_module_metadata (
339
345
& mut self ,
346
+ module_id : Option < ResolvedDocumentId > ,
340
347
path : CanonicalizedComponentModulePath ,
341
348
source_package_id : SourcePackageId ,
342
349
analyze_result : Option < AnalyzedModule > ,
343
350
environment : ModuleEnvironment ,
344
351
sha256 : Sha256Digest ,
345
352
) -> anyhow:: Result < ResolvedDocumentId > {
346
- let module_id = match self . module_metadata ( path . clone ( ) ) . await ? {
347
- Some ( module_metadata ) => {
348
- let new_metadata = ModuleMetadata {
349
- path : path . module_path ,
350
- source_package_id ,
351
- environment ,
352
- analyze_result : analyze_result . clone ( ) ,
353
- sha256 ,
354
- } ;
353
+ let new_metadata = ModuleMetadata {
354
+ path : path . module_path ,
355
+ source_package_id ,
356
+ environment ,
357
+ analyze_result : analyze_result . clone ( ) ,
358
+ sha256 ,
359
+ } ;
360
+ let module_id = match module_id {
361
+ Some ( module_id ) => {
355
362
SystemMetadataModel :: new ( self . tx , path. component . into ( ) )
356
- . replace ( module_metadata . id ( ) , new_metadata. try_into ( ) ?)
363
+ . replace ( module_id , new_metadata. try_into ( ) ?)
357
364
. await ?;
358
-
359
- module_metadata. id ( )
365
+ module_id
360
366
} ,
361
367
None => {
362
- let new_metadata = ModuleMetadata {
363
- path : path. module_path ,
364
- source_package_id,
365
- environment,
366
- analyze_result : analyze_result. clone ( ) ,
367
- sha256,
368
- } ;
369
-
370
368
SystemMetadataModel :: new ( self . tx , path. component . into ( ) )
371
369
. insert ( & MODULES_TABLE , new_metadata. try_into ( ) ?)
372
370
. await ?
@@ -376,17 +374,18 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
376
374
}
377
375
378
376
/// Delete a module, making it inaccessible for subsequent transactions.
379
- pub async fn delete ( & mut self , path : CanonicalizedComponentModulePath ) -> anyhow:: Result < ( ) > {
377
+ pub async fn delete (
378
+ & mut self ,
379
+ component : ComponentId ,
380
+ module_id : ResolvedDocumentId ,
381
+ ) -> anyhow:: Result < ( ) > {
380
382
if !( self . tx . identity ( ) . is_admin ( ) || self . tx . identity ( ) . is_system ( ) ) {
381
383
anyhow:: bail!( unauthorized_error( "delete_module" ) ) ;
382
384
}
383
- let namespace = path. component . into ( ) ;
384
- if let Some ( module_metadata) = self . module_metadata ( path) . await ? {
385
- let module_id = module_metadata. id ( ) ;
386
- SystemMetadataModel :: new ( self . tx , namespace)
387
- . delete ( module_id)
388
- . await ?;
389
- }
385
+ let namespace = component. into ( ) ;
386
+ SystemMetadataModel :: new ( self . tx , namespace)
387
+ . delete ( module_id)
388
+ . await ?;
390
389
Ok ( ( ) )
391
390
}
392
391
0 commit comments