forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Postgres 10+ GENERATED ALWAYS AS IDENTITY
allow developers to create a Column of identity and choose between `ALWAYS` and `BY DEFAULT`. Closes: typeorm#8370
- Loading branch information
1 parent
0334d10
commit fb4af30
Showing
15 changed files
with
196 additions
and
9 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
27 changes: 27 additions & 0 deletions
27
src/decorator/options/PrimaryGeneratedColumnIdentityOptions.ts
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,27 @@ | ||
import {PrimaryGeneratedColumnType} from "../../driver/types/ColumnTypes"; | ||
|
||
/** | ||
* Describes all options for PrimaryGeneratedColumn decorator with identity generation strategy. | ||
*/ | ||
export interface PrimaryGeneratedColumnIdentityOptions { | ||
|
||
/** | ||
* Column type. Must be one of the value from the ColumnTypes class. | ||
*/ | ||
type?: PrimaryGeneratedColumnType; | ||
|
||
/** | ||
* Column name in the database. | ||
*/ | ||
name?: string; | ||
|
||
/** | ||
* Column comment. Not supported by all database types. | ||
*/ | ||
comment?: string; | ||
|
||
/** | ||
* Identity column type. Supports only in Postgres 10+. | ||
*/ | ||
generatedIdentity?: "ALWAYS"|"BY DEFAULT"; | ||
} |
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
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,7 @@ | ||
import { Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class UserAlways { | ||
@PrimaryGeneratedColumn("identity", { generatedIdentity: "ALWAYS" }) | ||
id!: 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,7 @@ | ||
import { Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class UserDefault { | ||
@PrimaryGeneratedColumn("identity", { generatedIdentity: "BY DEFAULT" }) | ||
id!: 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,7 @@ | ||
import { Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn("identity") | ||
id!: 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,110 @@ | ||
import "reflect-metadata"; | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
} from "../../utils/test-utils"; | ||
import { Connection } from "../../../src"; | ||
import { User } from "./entity/UserEntity"; | ||
|
||
import { expect } from "chai"; | ||
import { UserAlways } from "./entity/UserAlways"; | ||
import { UserDefault } from "./entity/UserDefault"; | ||
|
||
describe("github issues > #8370 Add support for Postgres GENERATED ALWAYS AS IDENTITY", () => { | ||
|
||
describe("User entity", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [User], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"], | ||
})) | ||
); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should produce proper SQL for creating a table with `BY DEFAULT` identity column for default options", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const sqlInMemory = await connection.driver | ||
.createSchemaBuilder() | ||
.log(); | ||
expect(sqlInMemory) | ||
.to.have.property("upQueries") | ||
.that.is.an("array") | ||
.and.has.length(1); | ||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.eql( | ||
`CREATE TABLE "user" ("id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL, CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))` | ||
); | ||
}) | ||
)); | ||
}); | ||
|
||
describe("UserAlways entity", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [UserAlways], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"], | ||
})) | ||
); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should produce proper SQL for creating a table with `ALWAYS` identity column", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const sqlInMemory = await connection.driver | ||
.createSchemaBuilder() | ||
.log(); | ||
expect(sqlInMemory) | ||
.to.have.property("upQueries") | ||
.that.is.an("array") | ||
.and.has.length(1); | ||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.eql( | ||
`CREATE TABLE "user_always" ("id" integer GENERATED ALWAYS AS IDENTITY NOT NULL, CONSTRAINT "PK_08a31cb18e870610e560b6c0230" PRIMARY KEY ("id"))` | ||
); | ||
}) | ||
)); | ||
}); | ||
|
||
describe("UserDefault entity", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [UserDefault], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"], | ||
})) | ||
); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should produce proper SQL for creating a table with `BY DEFAULT` identity column", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const sqlInMemory = await connection.driver | ||
.createSchemaBuilder() | ||
.log(); | ||
expect(sqlInMemory) | ||
.to.have.property("upQueries") | ||
.that.is.an("array") | ||
.and.has.length(1); | ||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.eql( | ||
`CREATE TABLE "user_default" ("id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL, CONSTRAINT "PK_25101c5a870759239a1a9ef429c" PRIMARY KEY ("id"))` | ||
); | ||
}) | ||
)); | ||
}); | ||
}); |