fix: update UpdateDateColumn on upsert (#10458)

Closes: #9015
This commit is contained in:
Lukas Spiss 2023-12-29 15:21:57 +01:00 committed by GitHub
parent 696e688d00
commit fdb9866ad2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 120 additions and 11 deletions

View File

@ -526,25 +526,47 @@ export class InsertQueryBuilder<
)}`
}
const updatePart: string[] = []
if (Array.isArray(overwrite)) {
query += ` ${conflictTarget} DO UPDATE SET `
query += overwrite
?.map(
updatePart.push(
...overwrite?.map(
(column) =>
`${this.escape(
column,
)} = EXCLUDED.${this.escape(column)}`,
)
.join(", ")
query += " "
),
)
} else if (columns) {
query += ` ${conflictTarget} DO UPDATE SET `
query += columns
.map(
updatePart.push(
...columns.map(
(column) =>
`${this.escape(column)} = :${column}`,
)
.join(", ")
),
)
}
if (updatePart.length > 0) {
query += ` ${conflictTarget} DO UPDATE SET `
updatePart.push(
...this.expressionMap
.mainAlias!.metadata.columns.filter(
(column) =>
column.isUpdateDate &&
!overwrite?.includes(
column.databaseName,
),
)
.map(
(column) =>
`${this.escape(
column.databaseName,
)} = DEFAULT`,
),
)
query += updatePart.join(", ")
query += " "
}

View File

@ -0,0 +1,22 @@
import {
BaseEntity,
Entity,
Column,
UpdateDateColumn,
PrimaryGeneratedColumn,
} from "../../../../src"
@Entity()
export class Post extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column({ type: "varchar", unique: true })
title: string
@Column({ type: "varchar" })
description: string
@UpdateDateColumn()
updated_at: Date
}

View File

@ -0,0 +1,65 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource, Repository } from "../../../src"
import { Post } from "./entity/Post"
import {
reloadTestingDatabases,
closeTestingConnections,
setupSingleTestingConnection,
} from "../../utils/test-utils"
describe("github issues > #9015 @UpdateDateColumn not updating on upsert", () => {
let dataSource: DataSource
let repository: Repository<Post>
before(async () => {
const options = setupSingleTestingConnection("postgres", {
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
})
if (!options) return
dataSource = new DataSource(options)
await dataSource.initialize()
})
beforeEach(async () => {
if (!dataSource) return
await reloadTestingDatabases([dataSource])
repository = dataSource.getRepository(Post)
})
after(() => closeTestingConnections([dataSource]))
it("should update the @UpdateDateColumn", async () => {
if (!dataSource) return
const oldDate = new Date("1970-01-01")
const post = new Post()
post.id = 1
post.title = "Some post"
post.description = "Some description"
post.updated_at = oldDate
await repository.save(post)
await repository.upsert(
{
title: post.title,
description: "Some new description",
},
{
conflictPaths: { title: true },
skipUpdateIfNoValuesChanged: true,
},
)
const postReloaded = await repository.findOne({
where: { id: post.id },
})
expect(postReloaded).to.exist
expect(postReloaded!.description).to.be.equal("Some new description")
expect(postReloaded!.updated_at.toString()).to.not.equal(
oldDate.toString(),
)
})
})