TypeORM-如何创建新表并在生产模式下自动运行迁移? [英] TypeORM - How to create new table and run migration automatically in production mode?

查看:1583
本文介绍了TypeORM-如何创建新表并在生产模式下自动运行迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序在生产模式下运行时,我想在MySQL中创建新表并自动运行TypeORM迁移.

注意:此新表不是在生产模式下启动应用程序之前创建的.

根据迁移文档,它需要使用 typeorm migration:run 命令以运行迁移.

由于我的新表仅在名为 CreateNewTableTimeStamp(inputTableName).up 的应用程序时创建,因此将触发在我的数据库中创建新表.

但是我没有找到解决方案如何自动执行此迁移,因为每次应用程序调用此方法来创建新表时,我都无法手动运行 typeorm migration:run .

创建此表后,我将在之后将新数据写入该新表中.

有人可以协助解决这个问题吗?

谢谢.

我的新表格代码:

class CreateNewTableTimeStamp implements MigrationInterface  {

  tableName: string;

  constructor (inputTableName: string) {
    this.tableName = inputTableName
  }

  async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.createTable(new Table({
          name: this.tableName,
          columns: [
              {
                  name: "id",
                  type: "int",
                  isPrimary: true
              },
              {
                  name: "email",
                  type: "varchar",
              }
          ]
      }), true)
  }

  async down(queryRunner: QueryRunner): Promise<any> {
    const table = await queryRunner.getTable(this.tableName);
    await queryRunner.dropTable(this.tableName);
  }
}

解决方案

正如@zenbeni在评论中提到的那样,不建议从服务器代码运行迁移,因为迁移应始终是不变的并可以重播. >

因此,我将更改设计以免从服务器代码进行迁移.

I would like to create new table in MySQL and run TypeORM migration automatically when application running in production mode.

Note: This new table is not created prior starting of application in production mode.

According to Migration Documentation, it need to use typeorm migration:run command to run migration.

Due to my new table only created when application called CreateNewTableTimeStamp(inputTableName).up, at this point it will trigger to create new table into my database.

But I found no solution how to do this migration automatically, since it is impossible for me to run typeorm migration:run manually each time application called this method to create new table.

After this table is created, I will write new data into this new table afterwards.

Could anyone assist on this issue?

Thanks.

My New Table Code:

class CreateNewTableTimeStamp implements MigrationInterface  {

  tableName: string;

  constructor (inputTableName: string) {
    this.tableName = inputTableName
  }

  async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.createTable(new Table({
          name: this.tableName,
          columns: [
              {
                  name: "id",
                  type: "int",
                  isPrimary: true
              },
              {
                  name: "email",
                  type: "varchar",
              }
          ]
      }), true)
  }

  async down(queryRunner: QueryRunner): Promise<any> {
    const table = await queryRunner.getTable(this.tableName);
    await queryRunner.dropTable(this.tableName);
  }
}

解决方案

As mentioned by @zenbeni in the comment, it is not recommended to running migrations from your server code, as the migration should always be immutable and replayed.

Therefore, I would change my design not to do migrations from my server code.

这篇关于TypeORM-如何创建新表并在生产模式下自动运行迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆