-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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 (#8371)
allow developers to create a Column of identity and choose between `ALWAYS` and `BY DEFAULT`. Closes: #8370
- Loading branch information
1 parent
f3e2b0e
commit a0f09de
Showing
11 changed files
with
152 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class User { | ||
// test PrimaryGeneratedColumn | ||
@PrimaryGeneratedColumn("identity", { generatedIdentity: "ALWAYS" }) | ||
id: number; | ||
|
||
// test explicit `ALWAYS` | ||
@Column({ | ||
type: "bigint", | ||
generated: "identity", | ||
generatedIdentity: "ALWAYS", | ||
}) | ||
secondId: number; | ||
|
||
// test explicit `BY DEFAULT` | ||
@Column({ | ||
type: "int", | ||
generated: "identity", | ||
generatedIdentity: "BY DEFAULT", | ||
}) | ||
thirdId: number; | ||
|
||
// test default `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,64 @@ | ||
import "reflect-metadata"; | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
} from "../../utils/test-utils"; | ||
import { Connection } from "../../../src"; | ||
import { User } from "./entity/UserEntity"; | ||
|
||
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 column 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); | ||
|
||
// primary key | ||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.contains( | ||
`"id" integer GENERATED ALWAYS AS IDENTITY NOT NULL` | ||
); | ||
|
||
// second id | ||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.contains( | ||
`"secondId" bigint GENERATED ALWAYS AS IDENTITY NOT NULL` | ||
); | ||
|
||
// third id | ||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.contains( | ||
`"thirdId" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL` | ||
); | ||
|
||
// fourth id | ||
expect(sqlInMemory.upQueries[0]) | ||
.to.have.property("query") | ||
.that.contains( | ||
`"fourthId" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL` | ||
); | ||
}) | ||
)); | ||
}); |