Skip to content

Commit

Permalink
feat: mariadb uuid inet4 inet6 column data type support (#9845)
Browse files Browse the repository at this point in the history
* feat: mariadb inet4, inet6, uuid data type support

* refactor: cleanup unnecessary methods

* style: mysqldriver formatting

* fix: handle length column metadata mariadb uuid

* fix: 8832 test suite to verify errors correctly

* style: fix 8832 test formatting

* fix: 8832 error testing cleanup

* fix: remove defaulting column type feature

* style: fix formatting

* fix: remove unnecessary dbms error test

* fix: remove unused import in test

* fix: ensure defaulting uuid generation column type
  • Loading branch information
smith-xyz authored Apr 25, 2023
1 parent f5b93c1 commit d8a2e37
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:

# mariadb
mariadb:
image: "mariadb:10.8.4"
image: "mariadb:10.10.3"
container_name: "typeorm-mariadb"
ports:
- "3307:3306"
Expand Down
5 changes: 4 additions & 1 deletion docs/entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ or
`timestamp`, `time`, `year`, `char`, `nchar`, `national char`, `varchar`, `nvarchar`, `national varchar`,
`text`, `tinytext`, `mediumtext`, `blob`, `longtext`, `tinyblob`, `mediumblob`, `longblob`, `enum`, `set`,
`json`, `binary`, `varbinary`, `geometry`, `point`, `linestring`, `polygon`, `multipoint`, `multilinestring`,
`multipolygon`, `geometrycollection`
`multipolygon`, `geometrycollection`, `uuid`, `inet4`, `inet6`

> Note: UUID, INET4, and INET6 are only available for mariadb and for respective versions that made them available.

### Column types for `postgres`

Expand Down
16 changes: 14 additions & 2 deletions src/driver/mysql/MysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ export class MysqlDriver implements Driver {
"multilinestring",
"multipolygon",
"geometrycollection",
// additional data types for mariadb
"uuid",
"inet4",
"inet6",
]

/**
Expand Down Expand Up @@ -331,6 +335,9 @@ export class MysqlDriver implements Driver {
update: false,
}

/** MariaDB supports uuid type for version 10.7.0 and up */
private uuidColumnTypeSuported = false

// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -421,6 +428,9 @@ export class MysqlDriver implements Driver {
if (VersionUtils.isGreaterOrEqual(dbVersion, "10.2.0")) {
this.cteCapabilities.enabled = true
}
if (VersionUtils.isGreaterOrEqual(dbVersion, "10.7.0")) {
this.uuidColumnTypeSuported = true
}
} else if (this.options.type === "mysql") {
if (VersionUtils.isGreaterOrEqual(dbVersion, "8.0.0")) {
this.cteCapabilities.enabled = true
Expand Down Expand Up @@ -720,7 +730,7 @@ export class MysqlDriver implements Driver {
return "blob"
} else if (column.type === Boolean) {
return "tinyint"
} else if (column.type === "uuid") {
} else if (column.type === "uuid" && !this.uuidColumnTypeSuported) {
return "varchar"
} else if (column.type === "json" && this.options.type === "mariadb") {
/*
Expand Down Expand Up @@ -825,8 +835,10 @@ export class MysqlDriver implements Driver {

/**
* fix https://github.com/typeorm/typeorm/issues/1139
* note that if the db did support uuid column type it wouldn't have been defaulted to varchar
*/
if (column.generationStrategy === "uuid") return "36"
if (column.generationStrategy === "uuid" && column.type === "varchar")
return "36"

switch (column.type) {
case String:
Expand Down
4 changes: 3 additions & 1 deletion src/driver/types/ColumnTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ export type SimpleColumnType =
| "set" // mysql
| "cidr" // postgres
| "inet" // postgres, cockroachdb
| "inet4" // mariadb
| "inet6" // mariadb
| "macaddr" // postgres
| "bit" // postgres, mssql
| "bit varying" // postgres
| "varbit" // postgres
| "tsvector" // postgres
| "tsquery" // postgres
| "uuid" // postgres, cockroachdb
| "uuid" // postgres, cockroachdb, mariadb
| "xml" // mssql, postgres
| "json" // mysql, postgres, cockroachdb, spanner
| "jsonb" // postgres, cockroachdb
Expand Down
8 changes: 8 additions & 0 deletions src/metadata-builder/JunctionEntityMetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export class JunctionEntityMetadataBuilder {
) ||
this.connection.driver.options.type ===
"aurora-mysql") &&
// some versions of mariadb support the column type and should not try to provide the length property
this.connection.driver.normalizeType(
referencedColumn,
) !== "uuid" &&
(referencedColumn.generationStrategy === "uuid" ||
referencedColumn.type === "uuid")
? "36"
Expand Down Expand Up @@ -166,6 +170,10 @@ export class JunctionEntityMetadataBuilder {
) ||
this.connection.driver.options.type ===
"aurora-mysql") &&
// some versions of mariadb support the column type and should not try to provide the length property
this.connection.driver.normalizeType(
inverseReferencedColumn,
) !== "uuid" &&
(inverseReferencedColumn.generationStrategy ===
"uuid" ||
inverseReferencedColumn.type === "uuid")
Expand Down
4 changes: 4 additions & 0 deletions src/metadata-builder/RelationJoinColumnBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ export class RelationJoinColumnBuilder {
) ||
this.connection.driver.options.type ===
"aurora-mysql") &&
// some versions of mariadb support the column type and should not try to provide the length property
this.connection.driver.normalizeType(
referencedColumn,
) !== "uuid" &&
(referencedColumn.generationStrategy ===
"uuid" ||
referencedColumn.type === "uuid")
Expand Down
6 changes: 5 additions & 1 deletion test/github-issues/6540/entity/order.entity.ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export enum OrderStatus {

@Entity()
export class Order extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
/**
* modified to remove the uuid since some versions of mariadb have uuid as a type
* which would create an additional upsert between the tests -> https://github.com/typeorm/typeorm/issues/8832
*/
@PrimaryGeneratedColumn()
id: string

@Column({ type: "enum", enum: OrderStatus })
Expand Down
10 changes: 10 additions & 0 deletions test/github-issues/8832/badEntity/BadInet4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class BadInet4 {
@PrimaryGeneratedColumn("uuid")
id?: string

@Column({ type: "inet4", length: "36" })
inet4: string
}
10 changes: 10 additions & 0 deletions test/github-issues/8832/badEntity/BadInet6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class BadInet6 {
@PrimaryGeneratedColumn("uuid")
id?: string

@Column({ type: "inet6", length: "36" })
inet6: string
}
10 changes: 10 additions & 0 deletions test/github-issues/8832/badEntity/BadUuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class BadUuid {
@PrimaryGeneratedColumn("uuid")
id?: string

@Column({ type: "uuid", length: "36" })
uuid: string
}
22 changes: 22 additions & 0 deletions test/github-issues/8832/entity/Address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToOne,
} from "../../../../src"
import { User } from "./User"

@Entity()
export class Address {
@PrimaryGeneratedColumn("increment")
id?: number

@Column()
city: string

@Column()
state: string

@ManyToOne(() => User, (user) => user.addresses)
user: User
}
30 changes: 30 additions & 0 deletions test/github-issues/8832/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
Column,
Entity,
OneToMany,
PrimaryGeneratedColumn,
} from "../../../../src"
import { Address } from "./Address"

@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
id?: string

/** can use a default but testing against mysql since they're shared drivers */
@Column({ type: "uuid" })
uuid: string

@Column({ type: "inet4" })
inet4: string

@Column({ type: "inet6" })
inet6: string

/** testing generation */
@Column({ type: "uuid", generated: "uuid" })
another_uuid_field?: string

@OneToMany(() => Address, (address) => address.user)
addresses?: Address[]
}
7 changes: 7 additions & 0 deletions test/github-issues/8832/entity/UuidEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class UuidEntity {
@PrimaryGeneratedColumn("uuid")
id?: string
}
Loading

0 comments on commit d8a2e37

Please sign in to comment.