Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create typeorm_metadata table when running migrations (#4956)
* fix: create typeorm_metatable when running migrations * remove redundant check * create typeorm_metadata table in MigrationExecutor * remove redundant call to createMetadataTableIfNecessary * move create metadata table logic * add missing test (#4956) * fix migration name * pass query runner instance to metadata creation * do not create metadata table in log() Avoid creating the metatable when you don't intend to modify the database, such as when generating migrations
- Loading branch information
Showing
7 changed files
with
169 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { PrimaryGeneratedColumn, UpdateDateColumn } from "../../../../src"; | ||
|
||
import { Entity } from "../../../../src/decorator/entity/Entity"; | ||
|
||
@Entity("foo") | ||
export class Foo { | ||
@PrimaryGeneratedColumn({ name: "id" }) | ||
id: number; | ||
|
||
@UpdateDateColumn({ name: "updated_at" }) | ||
updatedAt: Date; | ||
} |
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,13 @@ | ||
import { Connection, ViewColumn, ViewEntity } from "../../../../src"; | ||
|
||
import { Foo } from "./Foo"; | ||
|
||
@ViewEntity({ | ||
name: "foo_view", | ||
expression: (connection: Connection) => | ||
connection.createQueryBuilder(Foo, "foo").select(`foo.updatedAt`), | ||
}) | ||
export class FooView { | ||
@ViewColumn() | ||
updatedAt: Date; | ||
} |
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,78 @@ | ||
import "reflect-metadata"; | ||
|
||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
} from "../../utils/test-utils"; | ||
|
||
import { Connection } from "../../../src/connection/Connection"; | ||
import { afterEach } from "mocha"; | ||
import { expect } from "chai"; | ||
|
||
describe("github issues > #4956 create typeorm_metatable when running migrations.", () => { | ||
let connections: Connection[]; | ||
|
||
afterEach(async () => { | ||
await closeTestingConnections(connections); | ||
}); | ||
|
||
it("should create typeorm_metadata table when running migrations with views", async () => { | ||
connections = await createTestingConnections({ | ||
entities: [__dirname + "/entities/*{.js,.ts}"], | ||
migrations: [__dirname + "/migrations/WithView{.js,.ts}"], | ||
enabledDrivers: ["mysql", "mariadb"], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
}); | ||
|
||
await Promise.all( | ||
connections.map(async (connection) => { | ||
const queryRunner = connection.createQueryRunner(); | ||
const typeormMetadataTableName = "typeorm_metadata"; | ||
|
||
const hasMetadataTable = await queryRunner.hasTable( | ||
typeormMetadataTableName | ||
); | ||
|
||
expect(hasMetadataTable).to.be.false; | ||
|
||
await connection.runMigrations(); | ||
|
||
const hasPostMigrationMetadataTable = | ||
await queryRunner.hasTable(typeormMetadataTableName); | ||
|
||
expect(hasPostMigrationMetadataTable).to.be.true; | ||
}) | ||
); | ||
}); | ||
|
||
it("should not create typeorm_metadata table when running migrations if there are no views", async () => { | ||
connections = await createTestingConnections({ | ||
entities: [__dirname + "/entities/Foo{.js,.ts}"], | ||
migrations: [__dirname + "/migrations/WithoutView{.js,.ts}"], | ||
enabledDrivers: ["mysql", "mariadb"], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
}); | ||
|
||
await Promise.all( | ||
connections.map(async (connection) => { | ||
const queryRunner = connection.createQueryRunner(); | ||
const typeormMetadataTableName = "typeorm_metadata"; | ||
|
||
const hasMetadataTable = await queryRunner.hasTable( | ||
typeormMetadataTableName | ||
); | ||
|
||
expect(hasMetadataTable).to.be.false; | ||
|
||
await connection.runMigrations(); | ||
|
||
const hasPostMigrationMetadataTable = | ||
await queryRunner.hasTable(typeormMetadataTableName); | ||
|
||
expect(hasPostMigrationMetadataTable).to.be.false; | ||
}) | ||
); | ||
}); | ||
}); |
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,28 @@ | ||
import { MigrationInterface, QueryRunner } from "../../../../src"; | ||
|
||
export class WithView1623518107000 implements MigrationInterface { | ||
name = "WithView1623518107000"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
"CREATE TABLE `foo` (`id` int NOT NULL AUTO_INCREMENT, `updated_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (`id`)) ENGINE=InnoDB" | ||
); | ||
await queryRunner.query( | ||
"CREATE VIEW `foo_view` AS SELECT updated_at FROM `foo`" | ||
); | ||
await queryRunner.query( | ||
"INSERT INTO `typeorm_metadata`(`type`, `schema`, `name`, `value`) VALUES (?, ?, ?, ?)", | ||
["VIEW", null, "foo_view", "SELECT `updated_at` FROM `foo`"] | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
"DELETE FROM `typeorm_metadata` WHERE `type` = 'VIEW' AND `schema` = ? AND `name` = ?", | ||
[null, "foo_view"] | ||
); | ||
|
||
await queryRunner.query("DROP VIEW `foo_view`"); | ||
await queryRunner.query("DROP Table `foo`"); | ||
} | ||
} |
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,15 @@ | ||
import { MigrationInterface, QueryRunner } from "../../../../src"; | ||
|
||
export class WithoutView1623518107000 implements MigrationInterface { | ||
name = "WithoutView1623518107000"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
"CREATE TABLE `foo` (`id` int NOT NULL AUTO_INCREMENT, `updated_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (`id`)) ENGINE=InnoDB" | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query("DROP Table `foo`"); | ||
} | ||
} |