forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve duplicate update column in subscriber
Closes: typeorm#9948
- Loading branch information
1 parent
d08c104
commit d76d0c7
Showing
3 changed files
with
71 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src" | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
name: string; | ||
@Column() | ||
name: string | ||
|
||
@Column({ default: 0 }) | ||
updatedNameColumnsCount: number; | ||
} | ||
@Column({ default: 0 }) | ||
updatedNameColumnsCount: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
import "reflect-metadata"; | ||
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { expect } from "chai"; | ||
import { Post } from "./entity/Post"; | ||
import { expect } from "chai" | ||
import { Post } from "./entity/Post" | ||
|
||
describe("github issues > #9948 Subscribers with both 'beforeUpdate' and 'afterUpdate' methods defined cause duplicate 'updatedColumn' entries", () => { | ||
|
||
let dataSources: DataSource[]; | ||
before(async () => dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
subscribers: [__dirname + "/subscriber/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})); | ||
beforeEach(() => reloadTestingDatabases(dataSources)); | ||
after(() => closeTestingConnections(dataSources)); | ||
|
||
it("should not duplicate update column", () => Promise.all(dataSources.map(async dataSource => { | ||
|
||
const manager = dataSource.manager; | ||
|
||
const initialPost = new Post(); | ||
initialPost.name = 'post-init'; | ||
await manager.save(initialPost); | ||
|
||
initialPost.name = 'post-update'; | ||
const updatedPost = await manager.save(initialPost); | ||
|
||
expect(updatedPost.updatedNameColumnsCount).to.be.eq(1); | ||
|
||
}))); | ||
|
||
}); | ||
let dataSources: DataSource[] | ||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
subscribers: [__dirname + "/subscriber/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(dataSources)) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should not duplicate update column", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const manager = dataSource.manager | ||
|
||
const initialPost = new Post() | ||
initialPost.name = "post-init" | ||
await manager.save(initialPost) | ||
|
||
initialPost.name = "post-update" | ||
const updatedPost = await manager.save(initialPost) | ||
|
||
expect(updatedPost.updatedNameColumnsCount).to.be.eq(1) | ||
}), | ||
)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
import { EntitySubscriberInterface, EventSubscriber, UpdateEvent } from "../../../../src"; | ||
import { Post } from "../entity/Post"; | ||
import { | ||
EntitySubscriberInterface, | ||
EventSubscriber, | ||
UpdateEvent, | ||
} from "../../../../src" | ||
import { Post } from "../entity/Post" | ||
|
||
@EventSubscriber() | ||
export class PostSubscriber implements EntitySubscriberInterface { | ||
listenTo(): string | Function { | ||
return Post | ||
} | ||
|
||
listenTo(): string | Function { | ||
return Post; | ||
} | ||
beforeUpdate(event: UpdateEvent<Post>): void | Promise<Post> { | ||
event.entity!.updatedNameColumnsCount = event.updatedColumns.reduce( | ||
(p, c) => { | ||
return p + (c.propertyName === "name" ? 1 : 0) | ||
}, | ||
0, | ||
) | ||
} | ||
|
||
beforeUpdate(event: UpdateEvent<Post>): void | Promise<Post> { | ||
event.entity!.updatedNameColumnsCount = event.updatedColumns.reduce((p, c) => { | ||
return p + (c.propertyName === 'name' ? 1 : 0); | ||
}, 0); | ||
} | ||
|
||
afterUpdate(event: UpdateEvent<Post>): void | Promise<Post> { | ||
event.entity!.updatedNameColumnsCount = event.updatedColumns.reduce((p, c) => { | ||
return p + (c.propertyName === 'name' ? 1 : 0); | ||
}, 0); | ||
} | ||
|
||
} | ||
afterUpdate(event: UpdateEvent<Post>): void | Promise<Post> { | ||
event.entity!.updatedNameColumnsCount = event.updatedColumns.reduce( | ||
(p, c) => { | ||
return p + (c.propertyName === "name" ? 1 : 0) | ||
}, | ||
0, | ||
) | ||
} | ||
} |