forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve issues for mssql migration when simple-enum was changed
- Changes are now detected - Incorrect update Statement was split into a DROP CONSTRAINT and ADD CONSTRAINT Statement Closes: typeorm#7785 typeorm#9457
- Loading branch information
Showing
6 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Entity } from "../../../../src/decorator/entity/Entity" | ||
import { Column } from "../../../../src/decorator/columns/Column" | ||
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn" | ||
import {Generated} from "../../../../src"; | ||
|
||
export enum ExampleEnum { | ||
EnumValue1 = "enumvalue1", | ||
EnumValue2 = "enumvalue2", | ||
EnumValue3 = "enumvalue3", | ||
EnumValue4 = "enumvalue4", | ||
} | ||
|
||
@Entity() | ||
export class ExampleEntity { | ||
@Generated("increment") | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column({ | ||
length: 255, | ||
type: "simple-enum", | ||
enum: ExampleEnum, | ||
}) | ||
enumcolumn: ExampleEnum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
} from "../../utils/test-utils" | ||
import {DataSource} from "../../../src/data-source/DataSource" | ||
import {expect} from "chai"; | ||
|
||
describe("github issues > #9457 No changes in database schema were found, when simple-enum is changed.", () => { | ||
let dataSources: DataSource[] | ||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
migrations: [__dirname + "/migration/*{.js,.ts}"], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
enabledDrivers: ["mssql"] | ||
})), | ||
) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should drop and recreate 'CHECK' constraint to match enum values", async () => { | ||
await Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
await dataSource.runMigrations() | ||
|
||
const sqlInMemory = await dataSource.driver | ||
.createSchemaBuilder() | ||
.log() | ||
|
||
expect(sqlInMemory.upQueries.length).to.eql(2); | ||
expect(sqlInMemory.upQueries[0].query).to.eql('ALTER TABLE "example_entity" DROP CONSTRAINT "CHK_be8ed063b3976da24df4213baf_ENUM"') | ||
expect(sqlInMemory.upQueries[1].query).to.eql(`ALTER TABLE "example_entity" ADD CONSTRAINT "CHK_be8ed063b3976da24df4213baf_ENUM" CHECK ( enumcolumn IN ('enumvalue1','enumvalue2','enumvalue3','enumvalue4') )`) | ||
|
||
expect(sqlInMemory.downQueries.length).to.eql(2); | ||
expect(sqlInMemory.downQueries[0].query).to.eql('ALTER TABLE "example_entity" DROP CONSTRAINT "CHK_be8ed063b3976da24df4213baf_ENUM"') | ||
expect(sqlInMemory.downQueries[1].query).to.eql(`ALTER TABLE "example_entity" ADD CONSTRAINT "CHK_be8ed063b3976da24df4213baf_ENUM" CHECK ( enumcolumn IN ('enumvalue1','enumvalue2','enumvalue3') )`) | ||
})) | ||
}) | ||
|
||
// you can add additional tests if needed | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {MigrationInterface, QueryRunner} from "../../../../src" | ||
|
||
export class init1676011161422 implements MigrationInterface { | ||
name = "init1676011161422" | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "example_entity" | ||
( | ||
"id" int NOT NULL IDENTITY(1,1), | ||
"enumcolumn" nvarchar(255) CONSTRAINT CHK_be8ed063b3976da24df4213baf_ENUM CHECK (enumcolumn IN ('enumvalue1','enumvalue2','enumvalue3')) NOT NULL, | ||
CONSTRAINT "PK_fccd73330168066a434dbac114f" PRIMARY KEY ("id") | ||
)`, | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "example_entity"`) | ||
} | ||
} |