Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure transitive deletion triggers are run for CASCADE_DELETE_INVALIDATE_CACHE #66

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions packages/entity/src/EntityMutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,17 @@ export class DeleteMutator<
return await enforceAsyncResult(this.deleteAsync());
}

private async deleteInTransactionAsync(): Promise<Result<void>> {
private async deleteInTransactionAsync(
processedEntityIdentifiersFromTransitiveDeletions: Set<string> = new Set(),
skipDatabaseDeletion: boolean = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also write:

Suggested change
skipDatabaseDeletion: boolean = false
{ skipDatabaseDeletion = false }: { skipDatabaseDeletion: boolean} = {}

(yeah it's verbose but it's what we've got) This said I think the existing code is reasonable to read as-is.

Copy link
Member Author

@wschurman wschurman Jul 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll probably leave it as-is. This makes me wonder how much effort goes into optimizing these one-off named param objects like this in v8.

): Promise<Result<void>> {
const internalResult = await this.queryContext.runInTransactionIfNotInTransactionAsync(
(innerQueryContext) => this.deleteInternalAsync(innerQueryContext)
(innerQueryContext) =>
this.deleteInternalAsync(
innerQueryContext,
processedEntityIdentifiersFromTransitiveDeletions,
skipDatabaseDeletion
)
);
if (internalResult.ok) {
await this.executeTriggers(
Expand All @@ -445,7 +453,8 @@ export class DeleteMutator<

private async deleteInternalAsync(
queryContext: EntityTransactionalQueryContext,
processedEntityIdentifiersFromTransitiveDeletions: Set<string> = new Set()
processedEntityIdentifiersFromTransitiveDeletions: Set<string>,
skipDatabaseDeletion: boolean
): Promise<Result<TEntity>> {
const authorizeDeleteResult = await asyncResult(
this.privacyPolicy.authorizeDeleteAsync(this.viewerContext, queryContext, this.entity)
Expand All @@ -463,11 +472,13 @@ export class DeleteMutator<
await this.executeTriggers(this.mutationTriggers.beforeAll, queryContext, this.entity);
await this.executeTriggers(this.mutationTriggers.beforeDelete, queryContext, this.entity);

await this.databaseAdapter.deleteAsync(
queryContext,
this.entityConfiguration.idField,
this.entity.getID()
);
if (!skipDatabaseDeletion) {
await this.databaseAdapter.deleteAsync(
queryContext,
this.entityConfiguration.idField,
this.entity.getID()
);
}

const entityLoader = this.entityLoaderFactory.forLoad(this.viewerContext, queryContext);
await entityLoader.invalidateFieldsAsync(this.entity.getAllDatabaseFields());
Expand Down Expand Up @@ -567,18 +578,12 @@ export class DeleteMutator<
case EntityEdgeDeletionBehavior.CASCADE_DELETE_INVALIDATE_CACHE: {
await Promise.all(
inboundReferenceEntities.map((inboundReferenceEntity) =>
this.processEntityDeletionForInboundEdgesAsync(
inboundReferenceEntity,
queryContext,
processedEntityIdentifiers
)
)
);
await Promise.all(
inboundReferenceEntities.map((inboundReferenceEntity) =>
loaderFactory
.forLoad(queryContext)
.invalidateFieldsAsync(inboundReferenceEntity.getAllDatabaseFields())
mutatorFactory
.forDelete(inboundReferenceEntity, queryContext)
.deleteInTransactionAsync(
processedEntityIdentifiers,
/* skipDatabaseDeletion */ true // deletion is handled by DB
)
)
);
break;
Expand All @@ -599,7 +604,10 @@ export class DeleteMutator<
inboundReferenceEntities.map((inboundReferenceEntity) =>
mutatorFactory
.forDelete(inboundReferenceEntity, queryContext)
.deleteInternalAsync(queryContext, processedEntityIdentifiers)
.deleteInTransactionAsync(
processedEntityIdentifiers,
/* skipDatabaseDeletion */ false
)
)
);
}
Expand Down