Skip to content

Commit

Permalink
fix: ignore changes for columns with update: false in persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
grainrigi committed Aug 2, 2023
1 parent ff6e875 commit 809ced3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/persistence/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ export class Subject {
(this.databaseEntityLoaded === false ||
(this.databaseEntityLoaded && this.databaseEntity)) &&
// ((this.entity && this.databaseEntity) || (!this.entity && !this.databaseEntity)) &&
this.changeMaps.length > 0
// ignore changes for columns which are obviously not updatable
this.changeMaps.some(
(change) => !change.column || change.column.isUpdate,
)
)
}

Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/10249/entity/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Column, Entity, PrimaryColumn } from "../../../../src"

@Entity()
export class Example {
@PrimaryColumn()
id: string

@Column({ update: false })
notUpdatable: string

@Column()
updatable: string
}
64 changes: 64 additions & 0 deletions test/github-issues/10249/issue-10249.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { expect } from "chai"
import { Example } from "./entity/example"

describe("github issues > #10249 Saving an entity is not possible if only columns with update: false are changed", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [Example],
enabledDrivers: ["postgres"],
schemaCreate: true,
dropSchema: true,
})),
)

beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))

it("should ignore changes for columns with `update: false` on saving entity", () =>
Promise.all(
dataSources.map(async (dataSource) => {
await Promise.all(
dataSources.map(async (dataSource) => {
const manager = dataSource.manager

// create entity
let exampleEntity = new Example()
exampleEntity.id = "1"
exampleEntity.notUpdatable = "value1"
exampleEntity.updatable = "value1"
await manager.save(exampleEntity)

// updates only updatable value
exampleEntity.notUpdatable = "value2"
exampleEntity.updatable = "value2"
await manager.save(exampleEntity)

exampleEntity = (await manager.findOneBy(Example, {
id: "1",
}))!
expect(exampleEntity.notUpdatable).to.be.eql("value1")
expect(exampleEntity.updatable).to.be.eql("value2")

// if `update: false` column only specified, do nothing
exampleEntity.notUpdatable = "value3"
await manager.save(exampleEntity)

exampleEntity = (await manager.findOneBy(Example, {
id: "1",
}))!
expect(exampleEntity.notUpdatable).to.be.eql("value1")
expect(exampleEntity.updatable).to.be.eql("value2")
}),
)
}),
))
})

0 comments on commit 809ced3

Please sign in to comment.