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 PrimaryGeneratedColumn of identity and choose between `ALWAYS` and `BY DEFAULT`. Closes: typeorm#8370
- Loading branch information
1 parent
0334d10
commit 63b64f0
Showing
8 changed files
with
133 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class User { | ||
// yest primary generated column | ||
@PrimaryGeneratedColumn("identity", { | ||
generatedIdentity: "ALWAYS", | ||
}) | ||
id: number; | ||
|
||
// test generatedIdentity=ALWAYS | ||
@Column({ | ||
type: "bigint", | ||
generated: "identity", | ||
generatedIdentity: "ALWAYS", | ||
}) | ||
secondId: number; | ||
|
||
// test generatedIdentity=BY DEFAULT | ||
@Column({ | ||
type: "int", | ||
generated: "identity", | ||
generatedIdentity: "BY DEFAULT", | ||
}) | ||
thirdId: number; | ||
|
||
// test default behavior of `generatedIdentity` | ||
@Column({ type: "int", generated: "identity" }) | ||
fourthId: 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,59 @@ | ||
import "reflect-metadata"; | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
} from "../../utils/test-utils"; | ||
import { Connection } from "../../../src"; | ||
import { User } from "./entity/User"; | ||
|
||
import { expect } from "chai"; | ||
|
||
describe("github issues > #8370 Add support for Postgres GENERATED ALWAYS AS IDENTITY", () => { | ||
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 identity columns", () => | ||
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.contain( | ||
`"id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL` | ||
); | ||
|
||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.contain( | ||
`"secondId" bigint GENERATED ALWAYS AS IDENTITY NOT NULL` | ||
); | ||
|
||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.contain( | ||
`"thirdId" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL` | ||
); | ||
|
||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.contain( | ||
`"fourthId" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL` | ||
); | ||
}) | ||
)); | ||
}); |