Skip to content

Commit

Permalink
Revert "feat: soft delete recursive cascade (#8436)" (#8654)
Browse files Browse the repository at this point in the history
This reverts commit d0f32b3.
  • Loading branch information
pleerock committed Feb 17, 2022
1 parent 6ab9b3a commit 6b0b15b
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 191 deletions.
44 changes: 2 additions & 42 deletions src/persistence/SubjectExecutor.ts
Expand Up @@ -18,7 +18,6 @@ import {ClosureSubjectExecutor} from "./tree/ClosureSubjectExecutor";
import {MaterializedPathSubjectExecutor} from "./tree/MaterializedPathSubjectExecutor";
import {OrmUtils} from "../util/OrmUtils";
import { UpdateResult } from "../query-builder/result/UpdateResult";
import {RelationMetadata} from "../metadata/RelationMetadata";

/**
* Executes all database operations (inserts, updated, deletes) that must be executed
Expand Down Expand Up @@ -608,12 +607,8 @@ export class SubjectExecutor {
} else { // in this case identifier is just conditions object to update by
softDeleteQueryBuilder.where(subject.identifier);
}

updateResult = await softDeleteQueryBuilder.execute();
// Move throw all the relation of the subject
for (const relation of subject.metadata.relations) {
// Call recursive function that get the parents primary keys that in used on the inverse side in all one to many relations
await this.executeSoftRemoveRecursive(relation, [Reflect.get(subject.identifier, subject.metadata.primaryColumns[0].propertyName)]);
}
}

subject.generatedMap = updateResult.generatedMaps[0];
Expand All @@ -640,45 +635,10 @@ export class SubjectExecutor {
// }
}));
}

/**
* Recovers all given subjects in the database.
*/

protected async executeSoftRemoveRecursive(relation: RelationMetadata, ids: any[]): Promise<void> {
// We want to delete the entities just when the relation is cascade soft remove
if (relation.isCascadeSoftRemove){

let primaryPropertyName = relation.inverseEntityMetadata.primaryColumns[0].propertyName;
let updateResult: UpdateResult;
let softDeleteQueryBuilder = this.queryRunner
.manager
.createQueryBuilder()
.softDelete()
.from(relation.inverseEntityMetadata.target)
// We get back list of the affected rows primary keys for call again
.returning([primaryPropertyName])
.updateEntity(this.options && this.options.reload === false ? false : true)
.callListeners(false);
// soft remove only where parent id is in the list
softDeleteQueryBuilder.where(`${relation.inverseSidePropertyPath} in (:...ids)`, {ids: ids});
updateResult = await softDeleteQueryBuilder.execute();
let parentIds;
// Only in oracle the returning value is a list of the affected row primary keys and not list of dictionary
if (this.queryRunner.connection.driver instanceof OracleDriver){
parentIds = updateResult.raw[0];
}
else {
parentIds = updateResult.raw.map((row: any) => row[Object.keys(row)[0]]);
}
if (parentIds.length) {
// This is the recursive - check the relations of the relation
for (const subRelation of relation.inverseEntityMetadata.relations) {
await this.executeSoftRemoveRecursive(subRelation, parentIds);
}
}
}
}

protected async executeRecoverOperations(): Promise<void> {
await Promise.all(this.recoverSubjects.map(async subject => {

Expand Down
75 changes: 0 additions & 75 deletions test/github-issues/8416/8416.ts

This file was deleted.

24 changes: 0 additions & 24 deletions test/github-issues/8416/entity/Author.ts

This file was deleted.

20 changes: 0 additions & 20 deletions test/github-issues/8416/entity/Category.ts

This file was deleted.

30 changes: 0 additions & 30 deletions test/github-issues/8416/entity/Post.ts

This file was deleted.

0 comments on commit 6b0b15b

Please sign in to comment.