Skip to content

Commit 38e0eff

Browse files
authored
fix: resolve issue with migrations and unsigned int columns in aurora-data-api (typeorm#9478)
* fix: resolve issue with migrations and unsigned int columns in aurora-data-api Closes: typeorm#9477 * refactor: fix prettier * refactor: fix prettier again
1 parent efb4168 commit 38e0eff

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,12 @@ export class AuroraMysqlQueryRunner
21442144
tableColumn.name = dbColumn["COLUMN_NAME"]
21452145
tableColumn.type = dbColumn["DATA_TYPE"].toLowerCase()
21462146

2147+
// Unsigned columns are handled differently when it comes to width.
2148+
// Hence, we need to set the unsigned attribute before we check the width.
2149+
tableColumn.unsigned = tableColumn.zerofill
2150+
? true
2151+
: dbColumn["COLUMN_TYPE"].indexOf("unsigned") !== -1
2152+
21472153
if (
21482154
this.driver.withWidthColumnTypes.indexOf(
21492155
tableColumn.type as ColumnType,
@@ -2212,9 +2218,6 @@ export class AuroraMysqlQueryRunner
22122218
)
22132219
tableColumn.zerofill =
22142220
dbColumn["COLUMN_TYPE"].indexOf("zerofill") !== -1
2215-
tableColumn.unsigned = tableColumn.zerofill
2216-
? true
2217-
: dbColumn["COLUMN_TYPE"].indexOf("unsigned") !== -1
22182221
tableColumn.isGenerated =
22192222
dbColumn["EXTRA"].indexOf("auto_increment") !== -1
22202223
if (tableColumn.isGenerated)
@@ -2796,8 +2799,24 @@ export class AuroraMysqlQueryRunner
27962799
this.connection.driver.dataTypeDefaults[column.type].width
27972800

27982801
if (defaultWidthForType) {
2799-
return defaultWidthForType === width
2802+
// In MariaDB & MySQL 5.7, the default widths of certain numeric types are 1 less than
2803+
// the usual defaults when the column is unsigned.
2804+
// This also applies to Aurora MySQL.
2805+
const typesWithReducedUnsignedDefault = [
2806+
"int",
2807+
"tinyint",
2808+
"smallint",
2809+
"mediumint",
2810+
]
2811+
const needsAdjustment =
2812+
typesWithReducedUnsignedDefault.indexOf(column.type) !== -1
2813+
if (column.unsigned && needsAdjustment) {
2814+
return defaultWidthForType - 1 === width
2815+
} else {
2816+
return defaultWidthForType === width
2817+
}
28002818
}
2819+
28012820
return false
28022821
}
28032822
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Entity, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
@Entity()
4+
export class Test {
5+
@PrimaryGeneratedColumn({ unsigned: true })
6+
id: number
7+
}

test/github-issues/9477/issue-9477.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import "reflect-metadata"
2+
import { expect } from "chai"
3+
import { DataSource } from "../../../src"
4+
import {
5+
closeTestingConnections,
6+
createTestingConnections,
7+
reloadTestingDatabases,
8+
} from "../../utils/test-utils"
9+
import { Test } from "./entity/Test"
10+
11+
describe("github issues > #9477 Unsigned Integers Columns constantly dropped and recreated", () => {
12+
let connections: DataSource[]
13+
before(async () => {
14+
connections = await createTestingConnections({
15+
entities: [Test],
16+
schemaCreate: true,
17+
dropSchema: true,
18+
})
19+
})
20+
beforeEach(() => reloadTestingDatabases(connections))
21+
after(() => closeTestingConnections(connections))
22+
23+
it("should not create migrations for unsigned indices", () =>
24+
Promise.all(
25+
connections.map(async (connection) => {
26+
const sqlInMemory = await connection.driver
27+
.createSchemaBuilder()
28+
.log()
29+
expect(sqlInMemory.upQueries).to.eql([])
30+
expect(sqlInMemory.downQueries).to.eql([])
31+
}),
32+
))
33+
})

0 commit comments

Comments
 (0)