New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Unnecessary migrations for unsigned numeric types #6632
Changes from 2 commits
b6fef26
414a3fa
4382d2b
8a0fb4d
71fb286
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,10 +324,18 @@ export abstract class BaseQueryRunner { | |
return false; | ||
} | ||
|
||
if (this.connection.driver.dataTypeDefaults | ||
const defaultWidthForType = this.connection.driver.dataTypeDefaults | ||
&& this.connection.driver.dataTypeDefaults[column.type] | ||
&& this.connection.driver.dataTypeDefaults[column.type].width) { | ||
return this.connection.driver.dataTypeDefaults[column.type].width === width; | ||
&& this.connection.driver.dataTypeDefaults[column.type].width; | ||
|
||
if (defaultWidthForType) { | ||
// the default widths of these numeric types are 1 less when the column is unsigned. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this statement true for all databases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, actually only MariaDB. The method this code lives in ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having this check for all drivers is confusing. We need to move this logic to a driver-specific method, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, I agree. I can do this refractor as an addition to this PR. Ps if you are planning to make a release soon let me know so I can do the changes in time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah its already quite a while I didn't make a release, so yeah, expect a release soon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I just looked into this more. The I don't see any tests for Aurora in CI, so I've no way to be sure that my changes are valid for Aurora. I question the value of putting it in the Driver interface because as far as I can tell it is an (almost useless) feature of MySQL only. Here's a good explanation of what it actually does. Here's another suggestion:
What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, go ahead |
||
const typesWithReducedUnsignedDefault = ["int", "tinyint", "smallint", "mediumint"]; | ||
if (column.unsigned && -1 < typesWithReducedUnsignedDefault.indexOf(column.type)) { | ||
return (defaultWidthForType - 1) === width; | ||
} else { | ||
return defaultWidthForType === width; | ||
} | ||
} | ||
|
||
return false; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Column, Entity, PrimaryColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Test { | ||
@PrimaryColumn() | ||
id: number; | ||
|
||
@Column({ type: 'int', unsigned: true}) | ||
uInt: number; | ||
|
||
@Column({ type: 'tinyint', unsigned: true}) | ||
uTinyInt: number; | ||
|
||
@Column({ type: 'smallint', unsigned: true}) | ||
uSmallInt: number; | ||
|
||
@Column({ type: 'mediumint', unsigned: true}) | ||
uMediumInt: number; | ||
|
||
@Column({ type: 'bigint', unsigned: true}) | ||
uBigInt: number; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import "reflect-metadata"; | ||
import { expect } from "chai"; | ||
import { Connection } from "../../../src"; | ||
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Test } from "./entity/Test"; | ||
|
||
describe("github issues > #2943 Inappropriate migration generated", () => { | ||
|
||
let connections: Connection[]; | ||
|
||
before(async () => { | ||
connections = await createTestingConnections({ | ||
enabledDrivers: ['mariadb', 'mysql'], | ||
entities: [Test], | ||
schemaCreate: true, | ||
dropSchema: true | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should not create migrations for unsigned numeric types with no specified width", () => | ||
Promise.all(connections.map(async (connection) => { | ||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
|
||
expect(sqlInMemory.upQueries).to.eql([]); | ||
expect(sqlInMemory.downQueries).to.eql([]); | ||
}))); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tableColumn.zerofill
is set below, why did you move it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. The value of
tableColumn.unsigned
is used inside theisDefaultColumnWidth()
method, so it needs to be set prior to checking the width, which is why I moved it up. I missed the dependency on thezerofill
value. I'll move thezerofill
assignment above theunsigned
assignment.