Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 8393 #8394

Merged
merged 11 commits into from
Dec 11, 2021
12 changes: 7 additions & 5 deletions src/query-builder/UpdateQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,13 @@ export class UpdateQueryBuilder<Entity> extends QueryBuilder<Entity> implements
});
});

if (metadata.versionColumn && updatedColumns.indexOf(metadata.versionColumn) === -1)
updateColumnAndValues.push(this.escape(metadata.versionColumn.databaseName) + " = " + this.escape(metadata.versionColumn.databaseName) + " + 1");
if (metadata.updateDateColumn && updatedColumns.indexOf(metadata.updateDateColumn) === -1)
updateColumnAndValues.push(this.escape(metadata.updateDateColumn.databaseName) + " = CURRENT_TIMESTAMP"); // todo: fix issue with CURRENT_TIMESTAMP(6) being used, can "DEFAULT" be used?!

// Don't allow calling update only with columns that are `update: false`
if (updateColumnAndValues.length > 0 || Object.keys(valuesSet).length === 0) {
if (metadata.versionColumn && updatedColumns.indexOf(metadata.versionColumn) === -1)
updateColumnAndValues.push(this.escape(metadata.versionColumn.databaseName) + " = " + this.escape(metadata.versionColumn.databaseName) + " + 1");
if (metadata.updateDateColumn && updatedColumns.indexOf(metadata.updateDateColumn) === -1)
updateColumnAndValues.push(this.escape(metadata.updateDateColumn.databaseName) + " = CURRENT_TIMESTAMP"); // todo: fix issue with CURRENT_TIMESTAMP(6) being used, can "DEFAULT" be used?!
}
} else {
Object.keys(valuesSet).map(key => {
let value = valuesSet[key];
Expand Down
25 changes: 25 additions & 0 deletions test/github-issues/8393/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Column,
Entity,
UpdateDateColumn,
PrimaryGeneratedColumn,
} from "../../../../src";

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

// Extra, not required column just for the example
@Column()
title: string;

@UpdateDateColumn()
lastUpdated: Date;

@Column({
// Forcing this column to only be written on insert time
update: false
})
readOnlyColumn: number;
}
55 changes: 55 additions & 0 deletions test/github-issues/8393/issue-8393.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import "reflect-metadata";
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases
} from "../../utils/test-utils";
import { Connection, UpdateValuesMissingError } from "../../../src/";
import { expect } from "chai";
import { Post } from "./entity/Post";

describe("github issues > #8393 When trying to update `update: false` column with `@UpdateDateColumn` the update column is updated", () => {
let connections: Connection[];
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
}))
);
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should not update the @UpdateDateColumn column when trying to update un-updatable column", () =>
Promise.all(
connections.map(async (connection) => {
const post = new Post();
post.title = "Control flow based type analysis";
post.readOnlyColumn = 1;

await connection.manager.save(post);

const updateResultPromise = connection.manager.update(Post, post.id, {
// Make a change to read only column
readOnlyColumn: 2,
});

await expect(updateResultPromise).to.be.rejectedWith(UpdateValuesMissingError);

const updatedPost = await connection.manager.findOne(
Post,
post.id
);

expect(updatedPost).to.be.an("object");

expect(post.readOnlyColumn).to.be.equal(
updatedPost!.readOnlyColumn
);

// Gonna be false
expect(post.lastUpdated.toString()).to.be.eql(updatedPost!.lastUpdated.toString());
})
));
});