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: don't use temporary table name to create foreign key, unique, ch…
…eck constraint with SQLite In migration steps, finally temporary table name is renamed to original table name. these constraints name should be created from original table name not temporary table name. Closes: typeorm#9176
- Loading branch information
1 parent
d285fd0
commit be43115
Showing
13 changed files
with
416 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src" | ||
|
||
@Entity() | ||
export class Author { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
name: string | ||
} |
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,22 @@ | ||
import { | ||
Column, | ||
Entity, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
} from "../../../../src" | ||
import { Author } from "./Author" | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
title: string | ||
|
||
@Column() | ||
text: string | ||
|
||
@ManyToOne((type) => Author, { cascade: true, nullable: false }) | ||
author: Author | ||
} |
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,33 @@ | ||
import { | ||
Check, | ||
Column, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Unique, | ||
} from "../../../../src" | ||
|
||
@Entity() | ||
@Unique(["firstName", "lastName", "middleName"]) | ||
export class User { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
firstName: string | ||
|
||
@Column() | ||
lastName: string | ||
|
||
@Column() | ||
middleName: string | ||
} | ||
|
||
@Entity() | ||
@Check(`"age" > 18`) | ||
export class CheckedUser { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
age: number | ||
} |
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,113 @@ | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { Author } from "./entity/Author" | ||
import { Post } from "./entity/Post" | ||
import { CheckedUser, User } from "./entity/User" | ||
import { CreatePostTable1656926770819 } from "./migration/1656926770819-CreatePostTable" | ||
import { CreateAuthorTable1656939116999 } from "./migration/1656939116999-CreateAuthorTable" | ||
import { AddAuthorIdColumn1656939646470 } from "./migration/1656939646470-AddAuthorIdColumn" | ||
import { CreateUserTable1657066872930 } from "./migration/1657066872930-CreateUserTable" | ||
import { CreateUniqueConstraintToUser1657067039714 } from "./migration/1657067039714-CreateUniqueConstraintToUser" | ||
import { CreateCheckedUserTable1657067039715 } from "./migration/1657067039715-CreateCheckedUserTable" | ||
import { CreateCheckConstraintToUser1657067039716 } from "./migration/1657067039716-CreateCheckConstraintToUser" | ||
import { expect } from "chai" | ||
|
||
describe("github issues > #9176 The names of foreign keys created by queryRunner.createForeignKey and schema:sync are different with SQLite", () => { | ||
describe("github issues > #9176 foreign keys", () => { | ||
let dataSources: DataSource[] | ||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [Author, Post], | ||
enabledDrivers: ["sqlite" /*, "better-sqlite3"*/], | ||
migrations: [ | ||
CreatePostTable1656926770819, | ||
CreateAuthorTable1656939116999, | ||
AddAuthorIdColumn1656939646470, | ||
], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
})), | ||
) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should not generate queries when created foreign key with queryRunnner.createForeignKey", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
await dataSource.runMigrations() | ||
const sqlInMemory = await dataSource.driver | ||
.createSchemaBuilder() | ||
.log() | ||
|
||
expect(sqlInMemory.upQueries).to.empty | ||
expect(sqlInMemory.downQueries).to.empty | ||
}), | ||
)) | ||
}) | ||
|
||
describe("github issues > #9176 unique constraint", () => { | ||
let dataSources: DataSource[] | ||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [User], | ||
enabledDrivers: ["sqlite" /*, "better-sqlite3"*/], | ||
migrations: [ | ||
CreateUserTable1657066872930, | ||
CreateUniqueConstraintToUser1657067039714, | ||
], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
})), | ||
) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should not generate queries when created unique constraint with queryRunnner.createUniqueConstraint", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
await dataSource.runMigrations() | ||
const sqlInMemory = await dataSource.driver | ||
.createSchemaBuilder() | ||
.log() | ||
|
||
expect(sqlInMemory.upQueries).to.empty | ||
expect(sqlInMemory.downQueries).to.empty | ||
}), | ||
)) | ||
}) | ||
|
||
describe("github issues > #9176 check constraint", () => { | ||
let dataSources: DataSource[] | ||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [CheckedUser], | ||
enabledDrivers: ["sqlite" /*, "better-sqlite3"*/], | ||
migrations: [ | ||
CreateCheckedUserTable1657067039715, | ||
CreateCheckConstraintToUser1657067039716, | ||
], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
})), | ||
) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should not generate queries when created check constraint with queryRunnner.createCheckConstraint", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
await dataSource.runMigrations() | ||
const sqlInMemory = await dataSource.driver | ||
.createSchemaBuilder() | ||
.log() | ||
|
||
expect(sqlInMemory.upQueries).to.empty | ||
expect(sqlInMemory.downQueries).to.empty | ||
}), | ||
)) | ||
}) | ||
}) |
35 changes: 35 additions & 0 deletions
35
test/github-issues/9176/migration/1656926770819-CreatePostTable.ts
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,35 @@ | ||
import { MigrationInterface, QueryRunner, Table } from "../../../../src" | ||
|
||
export class CreatePostTable1656926770819 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.createTable( | ||
new Table({ | ||
name: "post", | ||
columns: [ | ||
{ | ||
name: "id", | ||
type: "integer", | ||
isPrimary: true, | ||
isGenerated: true, | ||
generationStrategy: "increment", | ||
}, | ||
{ | ||
name: "title", | ||
type: "varchar", | ||
}, | ||
{ | ||
name: "text", | ||
type: "varchar", | ||
}, | ||
], | ||
}), | ||
true, | ||
true, | ||
true, | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropTable("post") | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/github-issues/9176/migration/1656939116999-CreateAuthorTable.ts
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,31 @@ | ||
import { MigrationInterface, QueryRunner, Table } from "../../../../src" | ||
|
||
export class CreateAuthorTable1656939116999 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.createTable( | ||
new Table({ | ||
name: "author", | ||
columns: [ | ||
{ | ||
name: "id", | ||
type: "integer", | ||
isPrimary: true, | ||
isGenerated: true, | ||
generationStrategy: "increment", | ||
}, | ||
{ | ||
name: "name", | ||
type: "varchar", | ||
}, | ||
], | ||
}), | ||
true, | ||
true, | ||
true, | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropTable("author") | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
test/github-issues/9176/migration/1656939646470-AddAuthorIdColumn.ts
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,38 @@ | ||
import { | ||
MigrationInterface, | ||
QueryRunner, | ||
TableColumn, | ||
TableForeignKey, | ||
} from "../../../../src" | ||
|
||
export class AddAuthorIdColumn1656939646470 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.addColumn( | ||
"post", | ||
new TableColumn({ | ||
name: "authorId", | ||
type: "integer", | ||
}), | ||
) | ||
await queryRunner.createForeignKey( | ||
"post", | ||
new TableForeignKey({ | ||
columnNames: ["authorId"], | ||
referencedTableName: "author", | ||
referencedColumnNames: ["id"], | ||
}), | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropForeignKey( | ||
"post", | ||
new TableForeignKey({ | ||
columnNames: ["authorId"], | ||
referencedTableName: "author", | ||
referencedColumnNames: ["id"], | ||
}), | ||
) | ||
await queryRunner.dropColumn("post", "authorId") | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
test/github-issues/9176/migration/1657066872930-CreateUserTable.ts
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,39 @@ | ||
import { MigrationInterface, QueryRunner, Table } from "../../../../src" | ||
|
||
export class CreateUserTable1657066872930 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.createTable( | ||
new Table({ | ||
name: "user", | ||
columns: [ | ||
{ | ||
name: "id", | ||
type: "integer", | ||
isPrimary: true, | ||
isGenerated: true, | ||
generationStrategy: "increment", | ||
}, | ||
{ | ||
name: "firstName", | ||
type: "varchar", | ||
}, | ||
{ | ||
name: "lastName", | ||
type: "varchar", | ||
}, | ||
{ | ||
name: "middleName", | ||
type: "varchar", | ||
}, | ||
], | ||
}), | ||
true, | ||
true, | ||
true, | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.dropTable("user") | ||
} | ||
} |
Oops, something went wrong.