Skip to content

Commit d8a2e37

Browse files
authored
feat: mariadb uuid inet4 inet6 column data type support (#9845)
* 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
1 parent f5b93c1 commit d8a2e37

14 files changed

+376
-6
lines changed

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ services:
1414

1515
# mariadb
1616
mariadb:
17-
image: "mariadb:10.8.4"
17+
image: "mariadb:10.10.3"
1818
container_name: "typeorm-mariadb"
1919
ports:
2020
- "3307:3306"

docs/entities.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ or
369369
`timestamp`, `time`, `year`, `char`, `nchar`, `national char`, `varchar`, `nvarchar`, `national varchar`,
370370
`text`, `tinytext`, `mediumtext`, `blob`, `longtext`, `tinyblob`, `mediumblob`, `longblob`, `enum`, `set`,
371371
`json`, `binary`, `varbinary`, `geometry`, `point`, `linestring`, `polygon`, `multipoint`, `multilinestring`,
372-
`multipolygon`, `geometrycollection`
372+
`multipolygon`, `geometrycollection`, `uuid`, `inet4`, `inet6`
373+
374+
> Note: UUID, INET4, and INET6 are only available for mariadb and for respective versions that made them available.
375+
373376

374377
### Column types for `postgres`
375378

src/driver/mysql/MysqlDriver.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ export class MysqlDriver implements Driver {
152152
"multilinestring",
153153
"multipolygon",
154154
"geometrycollection",
155+
// additional data types for mariadb
156+
"uuid",
157+
"inet4",
158+
"inet6",
155159
]
156160

157161
/**
@@ -331,6 +335,9 @@ export class MysqlDriver implements Driver {
331335
update: false,
332336
}
333337

338+
/** MariaDB supports uuid type for version 10.7.0 and up */
339+
private uuidColumnTypeSuported = false
340+
334341
// -------------------------------------------------------------------------
335342
// Constructor
336343
// -------------------------------------------------------------------------
@@ -421,6 +428,9 @@ export class MysqlDriver implements Driver {
421428
if (VersionUtils.isGreaterOrEqual(dbVersion, "10.2.0")) {
422429
this.cteCapabilities.enabled = true
423430
}
431+
if (VersionUtils.isGreaterOrEqual(dbVersion, "10.7.0")) {
432+
this.uuidColumnTypeSuported = true
433+
}
424434
} else if (this.options.type === "mysql") {
425435
if (VersionUtils.isGreaterOrEqual(dbVersion, "8.0.0")) {
426436
this.cteCapabilities.enabled = true
@@ -720,7 +730,7 @@ export class MysqlDriver implements Driver {
720730
return "blob"
721731
} else if (column.type === Boolean) {
722732
return "tinyint"
723-
} else if (column.type === "uuid") {
733+
} else if (column.type === "uuid" && !this.uuidColumnTypeSuported) {
724734
return "varchar"
725735
} else if (column.type === "json" && this.options.type === "mariadb") {
726736
/*
@@ -825,8 +835,10 @@ export class MysqlDriver implements Driver {
825835

826836
/**
827837
* fix https://github.com/typeorm/typeorm/issues/1139
838+
* note that if the db did support uuid column type it wouldn't have been defaulted to varchar
828839
*/
829-
if (column.generationStrategy === "uuid") return "36"
840+
if (column.generationStrategy === "uuid" && column.type === "varchar")
841+
return "36"
830842

831843
switch (column.type) {
832844
case String:

src/driver/types/ColumnTypes.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ export type SimpleColumnType =
177177
| "set" // mysql
178178
| "cidr" // postgres
179179
| "inet" // postgres, cockroachdb
180+
| "inet4" // mariadb
181+
| "inet6" // mariadb
180182
| "macaddr" // postgres
181183
| "bit" // postgres, mssql
182184
| "bit varying" // postgres
183185
| "varbit" // postgres
184186
| "tsvector" // postgres
185187
| "tsquery" // postgres
186-
| "uuid" // postgres, cockroachdb
188+
| "uuid" // postgres, cockroachdb, mariadb
187189
| "xml" // mssql, postgres
188190
| "json" // mysql, postgres, cockroachdb, spanner
189191
| "jsonb" // postgres, cockroachdb

src/metadata-builder/JunctionEntityMetadataBuilder.ts

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export class JunctionEntityMetadataBuilder {
102102
) ||
103103
this.connection.driver.options.type ===
104104
"aurora-mysql") &&
105+
// some versions of mariadb support the column type and should not try to provide the length property
106+
this.connection.driver.normalizeType(
107+
referencedColumn,
108+
) !== "uuid" &&
105109
(referencedColumn.generationStrategy === "uuid" ||
106110
referencedColumn.type === "uuid")
107111
? "36"
@@ -166,6 +170,10 @@ export class JunctionEntityMetadataBuilder {
166170
) ||
167171
this.connection.driver.options.type ===
168172
"aurora-mysql") &&
173+
// some versions of mariadb support the column type and should not try to provide the length property
174+
this.connection.driver.normalizeType(
175+
inverseReferencedColumn,
176+
) !== "uuid" &&
169177
(inverseReferencedColumn.generationStrategy ===
170178
"uuid" ||
171179
inverseReferencedColumn.type === "uuid")

src/metadata-builder/RelationJoinColumnBuilder.ts

+4
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ export class RelationJoinColumnBuilder {
208208
) ||
209209
this.connection.driver.options.type ===
210210
"aurora-mysql") &&
211+
// some versions of mariadb support the column type and should not try to provide the length property
212+
this.connection.driver.normalizeType(
213+
referencedColumn,
214+
) !== "uuid" &&
211215
(referencedColumn.generationStrategy ===
212216
"uuid" ||
213217
referencedColumn.type === "uuid")

test/github-issues/6540/entity/order.entity.ts.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export enum OrderStatus {
1616

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

2226
@Column({ type: "enum", enum: OrderStatus })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
@Entity()
4+
export class BadInet4 {
5+
@PrimaryGeneratedColumn("uuid")
6+
id?: string
7+
8+
@Column({ type: "inet4", length: "36" })
9+
inet4: string
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
@Entity()
4+
export class BadInet6 {
5+
@PrimaryGeneratedColumn("uuid")
6+
id?: string
7+
8+
@Column({ type: "inet6", length: "36" })
9+
inet6: string
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
@Entity()
4+
export class BadUuid {
5+
@PrimaryGeneratedColumn("uuid")
6+
id?: string
7+
8+
@Column({ type: "uuid", length: "36" })
9+
uuid: string
10+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {
2+
Entity,
3+
PrimaryGeneratedColumn,
4+
Column,
5+
ManyToOne,
6+
} from "../../../../src"
7+
import { User } from "./User"
8+
9+
@Entity()
10+
export class Address {
11+
@PrimaryGeneratedColumn("increment")
12+
id?: number
13+
14+
@Column()
15+
city: string
16+
17+
@Column()
18+
state: string
19+
20+
@ManyToOne(() => User, (user) => user.addresses)
21+
user: User
22+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
Column,
3+
Entity,
4+
OneToMany,
5+
PrimaryGeneratedColumn,
6+
} from "../../../../src"
7+
import { Address } from "./Address"
8+
9+
@Entity()
10+
export class User {
11+
@PrimaryGeneratedColumn("uuid")
12+
id?: string
13+
14+
/** can use a default but testing against mysql since they're shared drivers */
15+
@Column({ type: "uuid" })
16+
uuid: string
17+
18+
@Column({ type: "inet4" })
19+
inet4: string
20+
21+
@Column({ type: "inet6" })
22+
inet6: string
23+
24+
/** testing generation */
25+
@Column({ type: "uuid", generated: "uuid" })
26+
another_uuid_field?: string
27+
28+
@OneToMany(() => Address, (address) => address.user)
29+
addresses?: Address[]
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Entity, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
@Entity()
4+
export class UuidEntity {
5+
@PrimaryGeneratedColumn("uuid")
6+
id?: string
7+
}

0 commit comments

Comments
 (0)