-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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: resolve issue delete column null on after update event subscriber #8227
Changes from 2 commits
73f1341
cbe5d58
e99369c
43b746f
7c736f0
7b63d6b
b8241a0
b64376b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { DeleteDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
|
||
@DeleteDateColumn() | ||
deletedAt: Date; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import "reflect-metadata"; | ||
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases, sleep } from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { Post } from "./entity/Post"; | ||
|
||
describe("github issues > #6327 softRemove DeleteDateColumn is null at Susbscriber's AfterUpdate method", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
subscribers: [__dirname + "/subscriber/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should send correct update and delete date columns to after update subscriber", () => Promise.all(connections.map(async connection => { | ||
|
||
const manager = connection.manager; | ||
|
||
const entity = new Post(); | ||
await manager.save(entity); | ||
|
||
const deletedEntity = await manager.softRemove(entity, { data: { action: "soft-delete" } }); | ||
const softDeleteDate = deletedEntity!.updatedAt; | ||
|
||
await sleep(1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the motivation behind this sleep for a second? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SQLite doesn't see changes in the date if it's less than a second. Should be a check for a driver? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes you can do it, also please add a detailed description above it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Let me think in writing ))) Just helps me to think))) Those are only my thoughts. You can skip it.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH I still didn't get why timeout is needed. Commit to the database happens after line 25, I don't understand why and why don't see changes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was working in saturday. Sorry. Running tests now. |
||
|
||
await manager.recover(deletedEntity, { data: { action: "restore", softDeleteDate } }); | ||
|
||
}))); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { expect } from "chai"; | ||
import { EntitySubscriberInterface, EventSubscriber, UpdateEvent } from "../../../../src"; | ||
import { Post } from "../entity/Post"; | ||
|
||
@EventSubscriber() | ||
export class PostSubscriber implements EntitySubscriberInterface<Post> { | ||
listenTo() { | ||
return Post; | ||
} | ||
|
||
afterUpdate(event: UpdateEvent<Post>): void { | ||
const { entity, queryRunner: { data } } = event; | ||
|
||
expect(["soft-delete", "restore"]).to.include(data!.action); | ||
|
||
if (data!.action === "soft-delete") { | ||
expect(Object.prototype.toString.call(entity!.deletedAt)).to.be.eq("[object Date]"); | ||
} | ||
|
||
if (data!.action === "restore") { | ||
expect(entity!.deletedAt).to.be.null; | ||
expect(entity!.updatedAt.getTime()).not.to.be.eq(data!.softDeleteDate.getTime()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line of code the only difference with
update
method? If so, I would like to re-useupdate
method to prevent another peace of complex code to be added into the repo... We can makeupdate
method to accept returning columns if needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh another diff I see is
withDeleted
. Maybe we can add a third parameter toupdate
, liketype: "default" | "soft-delete"
. Dunno, better to came up with better names, but having singleupdate
method is better for sure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Got it.
I think
withDeleted
should be used with updates too. So if the entity is changed when soft deleted, it will fireafterUpdate
event. It will not if there is nowithDeleted
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Let me think in writing ))) Just helps me to think))) Those are only my thoughts. You can skip it.
To get rid of
softDelete
method we do this:As
this.expressionMap
passed by reference inSoftDeleteQueryBulder.execute()
andUpdateQueryBuilder.execute()
methods andthis.expressionMap.extraReturningColumns
are the columns we need to updateand
expressionMap
is required in theReturningResultsEntityUpdator
constructorand
extraReturningColumns
are never null or undefinedwe don't need to get
updationColumns
like soinsead we can use
So that
updationColumns
are unique for update and soft-delete and we don't need to call the same method multiple times.Also, we can use
withDeleted()
for both update and soft delete as select is done by id. Guess it won't slow down a lot even with lots of soft-deleted rows. I already mentioned, that if the entity is changed while soft-deleted it won't make changes to the entity fired toafterUpdate
event. And we can lose the blame in history if the event is used for that case.