Skip to content

Commit

Permalink
feat: add support for non-generated columns with uuid_generate_v4() d…
Browse files Browse the repository at this point in the history
…efault (#9065)

* fix: support postgres column with varchar datatype and uuid_generate_v4() default

Closes: #9063

* added test

Co-authored-by: Dmitry Zotov <dmzt08@gmail.com>
  • Loading branch information
knarfchan and AlexMesser committed Aug 24, 2022
1 parent eb8f0c6 commit dadb658
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ObjectLiteral } from "../../common/ObjectLiteral"
import { TypeORMError } from "../../error"
import { QueryFailedError } from "../../error/QueryFailedError"
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
import { TransactionNotStartedError } from "../../error/TransactionNotStartedError"
import { ColumnType } from "../types/ColumnTypes"
import { ReadStream } from "../../platform/PlatformTools"
import { BaseQueryRunner } from "../../query-runner/BaseQueryRunner"
import { QueryResult } from "../../query-runner/QueryResult"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { TableIndexOptions } from "../../schema-builder/options/TableIndexOptions"
import { Table } from "../../schema-builder/table/Table"
Expand All @@ -16,16 +17,15 @@ import { TableIndex } from "../../schema-builder/table/TableIndex"
import { TableUnique } from "../../schema-builder/table/TableUnique"
import { View } from "../../schema-builder/view/View"
import { Broadcaster } from "../../subscriber/Broadcaster"
import { InstanceChecker } from "../../util/InstanceChecker"
import { OrmUtils } from "../../util/OrmUtils"
import { VersionUtils } from "../../util/VersionUtils"
import { Query } from "../Query"
import { ColumnType } from "../types/ColumnTypes"
import { IsolationLevel } from "../types/IsolationLevel"
import { PostgresDriver } from "./PostgresDriver"
import { ReplicationMode } from "../types/ReplicationMode"
import { VersionUtils } from "../../util/VersionUtils"
import { TypeORMError } from "../../error"
import { QueryResult } from "../../query-runner/QueryResult"
import { MetadataTableType } from "../types/MetadataTableType"
import { InstanceChecker } from "../../util/InstanceChecker"
import { ReplicationMode } from "../types/ReplicationMode"
import { PostgresDriver } from "./PostgresDriver"

/**
* Runs queries on a single postgres database connection.
Expand Down Expand Up @@ -3596,8 +3596,13 @@ export class PostgresQueryRunner
dbColumn["column_default"],
)
) {
tableColumn.isGenerated = true
tableColumn.generationStrategy = "uuid"
if (tableColumn.type === "uuid") {
tableColumn.isGenerated = true
tableColumn.generationStrategy = "uuid"
} else {
tableColumn.default =
dbColumn["column_default"]
}
} else if (
dbColumn["column_default"] === "now()" ||
dbColumn["column_default"].indexOf(
Expand Down
28 changes: 28 additions & 0 deletions test/github-issues/9063/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Column,
Entity,
Generated,
PrimaryGeneratedColumn,
} from "../../../../src"

@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number

@Column({ generated: "uuid" })
generatedUuid1: string

@Generated("uuid")
@Column({ type: "uuid" })
generatedUuid2: string

@Column({ type: "uuid", default: () => "uuid_generate_v4()" })
generatedUuid3: string

@Column({ type: "character varying", default: () => "uuid_generate_v4()" })
nonGeneratedUuid1: string

@Column({ type: "character varying", default: () => "gen_random_uuid()" })
nonGeneratedUuid2: string
}
63 changes: 63 additions & 0 deletions test/github-issues/9063/issue-9063.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import "../../utils/test-setup"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src"
import { expect } from "chai"

describe("github issues > #9063 Support postgres column with varchar datatype and uuid_generate_v4() default", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["postgres"],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("it should be able to set special keyword as column name for simple-enum types", () =>
Promise.all(
connections.map(async (connection) => {
const queryRunner = connection.createQueryRunner()
const table = await queryRunner.getTable("post")
const generatedUuid1 =
table!.findColumnByName("generatedUuid1")!
const generatedUuid2 =
table!.findColumnByName("generatedUuid2")!
const generatedUuid3 =
table!.findColumnByName("generatedUuid3")!
const nonGeneratedUuid1 =
table!.findColumnByName("nonGeneratedUuid1")!
const nonGeneratedUuid2 =
table!.findColumnByName("nonGeneratedUuid2")!

expect(generatedUuid1.isGenerated).to.be.true
expect(generatedUuid1.type).to.equal("uuid")
expect(generatedUuid1.default).to.be.undefined

expect(generatedUuid2.isGenerated).to.be.true
expect(generatedUuid2.type).to.equal("uuid")
expect(generatedUuid2.default).to.be.undefined

expect(generatedUuid3.isGenerated).to.be.true
expect(generatedUuid3.type).to.equal("uuid")
expect(generatedUuid3.default).to.be.undefined

expect(nonGeneratedUuid1.isGenerated).to.be.false
expect(nonGeneratedUuid1.type).to.equal("character varying")
expect(nonGeneratedUuid1.default).to.equal("uuid_generate_v4()")

expect(nonGeneratedUuid2.isGenerated).to.be.false
expect(nonGeneratedUuid2.type).to.equal("character varying")
expect(nonGeneratedUuid2.default).to.equal("gen_random_uuid()")

await queryRunner.release()
}),
))
})

0 comments on commit dadb658

Please sign in to comment.