Skip to content

Commit

Permalink
fix: resolves issue with mssql column recreation when length max is i…
Browse files Browse the repository at this point in the history
…n lower case

Closes: typeorm#9399
  • Loading branch information
ertl committed Feb 10, 2023
1 parent 58fc088 commit 1741d7b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/driver/sqlserver/SqlServerDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,10 @@ export class SqlServerDriver implements Driver {
// of data type precedence to the expressions specified in the formula.
if (columnMetadata.asExpression) return false

return tableColumn.length !== this.getColumnLength(columnMetadata)
return (
tableColumn.length?.toUpperCase() !==
this.getColumnLength(columnMetadata)?.toUpperCase()
)
}

protected lowerDefaultValueIfNecessary(value: string | undefined) {
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/9399/entity/ExampleEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Entity, Generated } from "../../../../src"
import { PrimaryGeneratedColumn } from "../../../../src"
import { Column } from "../../../../src"

@Entity()
export class ExampleEntity {
@Generated("increment")
@PrimaryGeneratedColumn()
id: number

@Column({ type: "nvarchar", length: "max" })
value: string
}
35 changes: 35 additions & 0 deletions test/github-issues/9399/issue-9399.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
} from "../../utils/test-utils"
import { DataSource } from "../../../src"
import { expect } from "chai"

describe("github issues > #9399 mssql: Column is dropped and recreated in every migration", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["mssql"],
})),
)
after(() => closeTestingConnections(dataSources))

it("No migration should be created", () =>
Promise.all(
dataSources.map(async (dataSource) => {
await dataSource.runMigrations()
const sqlInMemory = await dataSource.driver
.createSchemaBuilder()
.log()

expect(sqlInMemory.upQueries).to.eql([])
expect(sqlInMemory.downQueries).to.eql([])
}),
))
// you can add additional tests if needed
})

0 comments on commit 1741d7b

Please sign in to comment.