Skip to content

Commit 8aeb464

Browse files
goffrieConvex, Inc.
authored andcommitted
Modify modules by id in ModuleModel::apply (#34909)
GitOrigin-RevId: 0f3231c23c19b377273b265f50b222f338cb8a72
1 parent 8e98fae commit 8aeb464

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

crates/application/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,12 +2381,18 @@ impl<RT: Runtime> Application<RT> {
23812381
.await?;
23822382

23832383
// 3. Add the module
2384+
let path = CanonicalizedComponentModulePath {
2385+
component,
2386+
module_path: module_path.clone(),
2387+
};
2388+
let module_id = ModuleModel::new(&mut tx)
2389+
.get_metadata(path.clone())
2390+
.await?
2391+
.map(|m| m.id());
23842392
ModuleModel::new(&mut tx)
23852393
.put(
2386-
CanonicalizedComponentModulePath {
2387-
component,
2388-
module_path: module_path.clone(),
2389-
},
2394+
module_id,
2395+
path,
23902396
module.source,
23912397
source_package_id,
23922398
module.source_map,

crates/model/src/modules/mod.rs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
155155
let mut added_modules = BTreeSet::new();
156156

157157
// Add new modules.
158-
let mut remaining_modules: BTreeSet<_> = self
158+
let mut remaining_modules: BTreeMap<_, _> = self
159159
.get_application_metadata(component)
160160
.await?
161161
.into_iter()
162-
.map(|module| module.into_value().path)
162+
.map(|module| (module.path.clone(), module.id()))
163163
.collect();
164164
for module in modules {
165165
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() {
167168
added_modules.insert(path.clone());
168169
}
169170
let analyze_result = if !path.is_deps() {
@@ -178,6 +179,7 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
178179
None
179180
};
180181
self.put(
182+
existing_module_id,
181183
CanonicalizedComponentModulePath {
182184
component,
183185
module_path: path.clone(),
@@ -192,14 +194,9 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
192194
}
193195

194196
let mut removed_modules = BTreeSet::new();
195-
for path in remaining_modules {
197+
for (path, module_id) in remaining_modules {
196198
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?;
203200
}
204201
ModuleDiff::new(added_modules, removed_modules)
205202
}
@@ -310,8 +307,10 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
310307
}
311308

312309
/// Put a module's source at a given path.
310+
/// `module_id` is the existing module at this `path`.
313311
pub async fn put(
314312
&mut self,
313+
module_id: Option<ResolvedDocumentId>,
315314
path: CanonicalizedComponentModulePath,
316315
source: ModuleSource,
317316
source_package_id: SourcePackageId,
@@ -330,43 +329,42 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
330329
"AnalyzedModule is required for non-dependency modules"
331330
);
332331
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?;
335341
Ok(())
336342
}
337343

338344
async fn put_module_metadata(
339345
&mut self,
346+
module_id: Option<ResolvedDocumentId>,
340347
path: CanonicalizedComponentModulePath,
341348
source_package_id: SourcePackageId,
342349
analyze_result: Option<AnalyzedModule>,
343350
environment: ModuleEnvironment,
344351
sha256: Sha256Digest,
345352
) -> 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) => {
355362
SystemMetadataModel::new(self.tx, path.component.into())
356-
.replace(module_metadata.id(), new_metadata.try_into()?)
363+
.replace(module_id, new_metadata.try_into()?)
357364
.await?;
358-
359-
module_metadata.id()
365+
module_id
360366
},
361367
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-
370368
SystemMetadataModel::new(self.tx, path.component.into())
371369
.insert(&MODULES_TABLE, new_metadata.try_into()?)
372370
.await?
@@ -376,17 +374,18 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
376374
}
377375

378376
/// 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<()> {
380382
if !(self.tx.identity().is_admin() || self.tx.identity().is_system()) {
381383
anyhow::bail!(unauthorized_error("delete_module"));
382384
}
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?;
390389
Ok(())
391390
}
392391

0 commit comments

Comments
 (0)