Skip to content

Commit

Permalink
Support orUpdate for SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
emibloque committed May 7, 2019
1 parent 360468b commit f8515ef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/query-builder/InsertQueryBuilder.ts
Expand Up @@ -249,7 +249,7 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
if (statement && statement.overwrite instanceof Array) {
if (this.connection.driver instanceof MysqlDriver) {
this.expressionMap.onUpdate.overwrite = statement.overwrite.map(column => `${column} = VALUES(${column})`).join(", ");
} else if (this.connection.driver instanceof PostgresDriver) {
} else if (this.connection.driver instanceof PostgresDriver || this.connection.driver instanceof AbstractSqliteDriver) {
this.expressionMap.onUpdate.overwrite = statement.overwrite.map(column => `${column} = EXCLUDED.${column}`).join(", ");
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/4096/entity/User.ts
@@ -0,0 +1,13 @@
import { Entity, PrimaryColumn, Column } from "../../../../src";

@Entity()
export class User {
@PrimaryColumn()
email: string;

@PrimaryColumn()
username: string;

@Column()
bio: string;
}
61 changes: 61 additions & 0 deletions test/github-issues/4096/issue-4096.ts
@@ -0,0 +1,61 @@
import "reflect-metadata";
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {expect} from "chai";
import {User} from "./entity/User";

describe("github issues > #4096 SQLite support for orUpdate", () => {
let connections: Connection[];

before(async () => connections = await createTestingConnections({
entities: [User],
enabledDrivers: ["sqlite"],
schemaCreate: true,
dropSchema: true,
}));

beforeEach(() => reloadTestingDatabases(connections));

after(() => closeTestingConnections(connections));

const user1 = new User();
user1.email = "example@example.org";
user1.username = "example";
user1.bio = "My bio";

const user2 = new User();
user2.email = "example@example.org";
user2.username = "example";
user2.bio = "Updated bio";

it("should overwrite using current value in SQLite", () => Promise.all(connections.map(async connection => {
try {
const UserRepository = connection.manager.getRepository(User);

await UserRepository
.createQueryBuilder()
.insert()
.into(User)
.values(user1)
.execute();

await UserRepository
.createQueryBuilder()
.insert()
.into(User)
.values(user2)
.orUpdate({
conflict_target: [ "email", "username" ],
overwrite: ["bio"],
})
.execute();

const users = await UserRepository.find();
expect(users).not.to.be.undefined;
expect(users).to.have.lengthOf(1);
expect(users[0]).to.includes({ bio: "Updated bio" });
} catch (err) {
throw new Error(err);
}
})));
});

0 comments on commit f8515ef

Please sign in to comment.