Skip to content

Commit

Permalink
fix: resolve duplicate update column in subscriber
Browse files Browse the repository at this point in the history
Closes: typeorm#9948
  • Loading branch information
spotykatch committed Apr 18, 2023
1 parent d08c104 commit d76d0c7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 57 deletions.
16 changes: 8 additions & 8 deletions test/github-issues/9948/entity/Post.ts
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
}
68 changes: 37 additions & 31 deletions test/github-issues/9948/issue-9948.ts
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)
}),
))
})
44 changes: 26 additions & 18 deletions test/github-issues/9948/subscriber/PostSubscriber.ts
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,
)
}
}

0 comments on commit d76d0c7

Please sign in to comment.