Skip to content

Commit

Permalink
fix: resolve duplicate subscriber updated columns (#9958)
Browse files Browse the repository at this point in the history
Closes: #9948
  • Loading branch information
spotykatch committed Apr 18, 2023
1 parent b064049 commit 3d67901
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/persistence/SubjectChangedColumnsComputer.ts
Expand Up @@ -166,7 +166,10 @@ export class SubjectChangedColumnsComputer {
if (normalizedValue === databaseValue) return
}
}
subject.diffColumns.push(column)

if (!subject.diffColumns.includes(column))
subject.diffColumns.push(column)

subject.changeMaps.push({
column: column,
value: entityValue,
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/9948/entity/Post.ts
@@ -0,0 +1,13 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number

@Column()
name: string

@Column({ default: 0 })
updatedNameColumnsCount: number
}
40 changes: 40 additions & 0 deletions test/github-issues/9948/issue-9948.ts
@@ -0,0 +1,40 @@
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"

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)
}),
))
})
31 changes: 31 additions & 0 deletions test/github-issues/9948/subscriber/PostSubscriber.ts
@@ -0,0 +1,31 @@
import {
EntitySubscriberInterface,
EventSubscriber,
UpdateEvent,
} from "../../../../src"
import { Post } from "../entity/Post"

@EventSubscriber()
export class PostSubscriber implements EntitySubscriberInterface {
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,
)
}

afterUpdate(event: UpdateEvent<Post>): void | Promise<Post> {
event.entity!.updatedNameColumnsCount = event.updatedColumns.reduce(
(p, c) => {
return p + (c.propertyName === "name" ? 1 : 0)
},
0,
)
}
}

0 comments on commit 3d67901

Please sign in to comment.