# 手动创建

> 英文标题：Creating manually
> 官网原文：https://typeorm.io/docs/migrations/creating
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 12 块

---

<!-- EN -->
# Creating manually

<!-- ZH -->
# 手动创建

<!-- EN -->
You can create a new migration using CLI by specifying the name and location of the migration:

<!-- ZH -->
你可以通过 CLI 指定迁移的名称和位置来创建一个新迁移：

<!-- EN -->
```shell
npx typeorm migration:create <path/to/migrations>/<migration-name>
```

<!-- ZH -->
```shell
npx typeorm migration:create <path/to/migrations>/<migration-name>
```

<!-- EN -->
For example:

<!-- ZH -->
例如：

<!-- EN -->
```shell
npx typeorm migration:create src/db/migrations/post-refactoring
```

<!-- ZH -->
```shell
npx typeorm migration:create src/db/migrations/post-refactoring
```

<!-- EN -->
After you run the command you can see a new file generated in the `src/db/migrations` directory named `{TIMESTAMP}-post-refactoring.ts` where `{TIMESTAMP}` is the current timestamp when the migration was generated.

<!-- ZH -->
运行该命令后，你会在 `src/db/migrations` 目录下看到一个新生成的文件，名为 `{TIMESTAMP}-post-refactoring.ts`，其中 `{TIMESTAMP}` 是生成该迁移时的当前时间戳。

<!-- EN -->
Now you can open the file and add your migration sql queries there. You should see the following content inside your migration:

<!-- ZH -->
现在你可以打开这个文件，把迁移的 sql 查询加进去。你应该会在迁移文件中看到如下内容：

<!-- EN -->
```typescript
import { MigrationInterface, QueryRunner } from "typeorm"

export class PostRefactoringTIMESTAMP implements MigrationInterface {
    async up(queryRunner: QueryRunner): Promise<void> {}

    async down(queryRunner: QueryRunner): Promise<void> {}
}
```

<!-- ZH -->
```typescript
import { MigrationInterface, QueryRunner } from "typeorm"

export class PostRefactoringTIMESTAMP implements MigrationInterface {
    async up(queryRunner: QueryRunner): Promise<void> {}

    async down(queryRunner: QueryRunner): Promise<void> {}
}
```

<!-- EN -->
There are two methods you must fill with your migration code: `up` and `down`. `up` has to contain the code you need to perform the migration. `down` has to revert whatever `up` changed. `down` method is used to revert the last migration.

<!-- ZH -->
有两个方法必须填入你的迁移代码：`up` 和 `down`。`up` 要包含执行迁移所需的代码。`down` 要回滚 `up` 所做的一切。`down` 方法用于回滚最近一次迁移。

<!-- EN -->
Inside both `up` and `down` you have a `QueryRunner` object. All database operations are executed using this object. Learn more about [query runner](https://typeorm.io/docs/query-runner).

<!-- ZH -->
在 `up` 和 `down` 中你都有一个 `QueryRunner` 对象。所有数据库操作都通过这个对象执行。进一步了解 [query runner](https://typeorm.io/docs/query-runner)。

<!-- EN -->
Let's see what the migration looks like with our `Post` changes:

<!-- ZH -->
我们来看看加上 `Post` 变更后的迁移长什么样：

<!-- EN -->
```typescript
import { MigrationInterface, QueryRunner } from "typeorm"

export class PostRefactoringTIMESTAMP implements MigrationInterface {
    async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(
            `ALTER TABLE "post" RENAME COLUMN "title" TO "name"`,
        )
    }

    async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(
            `ALTER TABLE "post" RENAME COLUMN "name" TO "title"`,
        ) // reverts things made in "up" method
    }
}
```

<!-- ZH -->
```typescript
import { MigrationInterface, QueryRunner } from "typeorm"

export class PostRefactoringTIMESTAMP implements MigrationInterface {
    async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(
            `ALTER TABLE "post" RENAME COLUMN "title" TO "name"`,
        )
    }

    async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(
            `ALTER TABLE "post" RENAME COLUMN "name" TO "title"`,
        ) // 回滚 "up" 方法中所做的改动
    }
}
```
