-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use OUTPUT INTO on SqlServer for returning columns (#5361)
* test: Add test for GitHub issue #5160 * fix: use OUTPUT INTO on SqlServer for returning columns Closes #5160 * chore: fix typo in comment * fix: use full types with length e.g. nvarchar(255) * refactor: move output table declaration to driver * fix: don't try to drop temporary tables
- Loading branch information
Showing
8 changed files
with
185 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {CreateDateColumn} from "../../../../src/decorator/columns/CreateDateColumn"; | ||
import {UpdateDateColumn} from "../../../../src/decorator/columns/UpdateDateColumn"; | ||
import {VersionColumn} from "../../../../src/decorator/columns/VersionColumn"; | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
title: string; | ||
|
||
@CreateDateColumn() | ||
createDate: string; | ||
|
||
@UpdateDateColumn() | ||
updateDate: string; | ||
|
||
@Column({ default: 100 }) | ||
order: number; | ||
|
||
@VersionColumn() | ||
version: number; | ||
|
||
@Column({ default: 0 }) | ||
triggerValue: number; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import "reflect-metadata"; | ||
import {expect} from "chai"; | ||
import {Connection} from "../../../src"; | ||
import {Post} from "./entity/Post"; | ||
import {createTestingConnections, reloadTestingDatabases, closeTestingConnections} from "../../utils/test-utils"; | ||
import {SqlServerDriver} from "../../../src/driver/sqlserver/SqlServerDriver"; | ||
|
||
describe("github issues > #5160 (MSSQL) DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause", () => { | ||
|
||
let connections: Connection[]; | ||
|
||
before(async () => { | ||
connections = await createTestingConnections({ | ||
entities: [Post], | ||
schemaCreate: true, | ||
dropSchema: true | ||
}); | ||
}); | ||
beforeEach(async () => { | ||
await reloadTestingDatabases(connections); | ||
|
||
return Promise.all(connections.map(async connection => { | ||
if (!(connection.driver instanceof SqlServerDriver)) { | ||
return; | ||
} | ||
|
||
return connection.query(` | ||
CREATE OR ALTER TRIGGER issue5160_post | ||
ON post AFTER INSERT, UPDATE AS | ||
BEGIN | ||
UPDATE post | ||
SET triggerValue = 1 | ||
WHERE id IN (SELECT id FROM inserted); | ||
END` | ||
); | ||
})); | ||
}); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should update entity model after insertion to MSSQL table with trigger", () => Promise.all(connections.map(async connection => { | ||
if (!(connection.driver instanceof SqlServerDriver)) { | ||
return; | ||
} | ||
|
||
const post = new Post(); | ||
post.title = "about entity updation in query builder"; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(Post) | ||
.values(post) | ||
.execute(); | ||
|
||
post.id.should.be.a("number"); | ||
post.id.should.be.greaterThan(0); | ||
post.title.should.be.equal("about entity updation in query builder"); | ||
post.order.should.be.equal(100); | ||
post.createDate.should.be.instanceof(Date); | ||
post.updateDate.should.be.instanceof(Date); | ||
post.triggerValue.should.be.equal(0, "Returned values from INSERT...OUTPUT will not reflect data modified by triggers"); | ||
|
||
// for additional safety, re-fetch entity and check that the trigger fired and updated the field as expected | ||
const updatedPost = await connection.createQueryBuilder(Post, "post") | ||
.where({ id: post.id }) | ||
.getOne(); | ||
|
||
expect(updatedPost).is.not.undefined; | ||
updatedPost!.id.should.be.equal(post.id); | ||
updatedPost!.triggerValue.should.be.equal(1); | ||
}))); | ||
|
||
it("should update entity model after save to MSSQL table with trigger", () => Promise.all(connections.map(async connection => { | ||
if (!(connection.driver instanceof SqlServerDriver)) { | ||
return; | ||
} | ||
|
||
const post = new Post(); | ||
post.title = "about entity updation in query builder"; | ||
await connection.manager.save(post); | ||
post.version.should.be.equal(1); | ||
|
||
post.title = "changed title"; | ||
await connection.manager.save(post); | ||
post.version.should.be.equal(2); | ||
post.triggerValue.should.be.equal(0, "Returned values from UPDATE...OUTPUT will not reflect data modified by triggers"); | ||
|
||
// for additional safety, re-fetch entity and check that the trigger fired and updated the field as expected | ||
const updatedPost = await connection.createQueryBuilder(Post, "post") | ||
.where({ id: post.id }) | ||
.getOne(); | ||
|
||
expect(updatedPost).is.not.undefined; | ||
updatedPost!.id.should.be.equal(post.id); | ||
updatedPost!.triggerValue.should.be.equal(1); | ||
}))); | ||
}); |