Jaskaran
Jaskaran

Reputation: 1

How to run a specific migration file using typeorm in node.js?

I'm currently working on a Node.js project using TypeORM with PostgreSQL. I have multiple migration files, but I want to run only one specific migration file instead of all pending migrations. Below are the details of my setup:

Data Source Configuration (db.ts):

import "dotenv/config";
import { DataSource } from "typeorm";
import { Dog } from "@modules/dog/entities/dog";
import { Breed } from "@modules/breed/entities/breed";
import { Color } from "@modules/color/entities/color";

export const AppDataSource = new DataSource({
  type: "postgres",
  host: process.env.HOST,
  port: 5432,
  username: process.env.DB_USERNAME,
  password: process.env.PASSWORD,
  database: process.env.DATABASE,
  entities: [Dog, Breed, Color],
  synchronize: false,
  logging: true,
  migrationsTableName: "migrations",
  migrations: ["./src/migrations/*.ts"],
});

Scripts in package.json:

"scripts": {
  "start": "nodemon --watch src --exec ts-node -r tsconfig-paths/register src/server.ts",
  "migration:generate": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:generate -d src/config/db.ts src/migrations/",
  "migration:run": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run -d=src/config/db.ts",
  "migration:revert": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:revert -d=src/config/db.ts"
}

Migration Files:

  1. Dogrename1721198056227.ts
import { MigrationInterface, QueryRunner } from "typeorm";

export class Dogrename1721198056227 implements MigrationInterface {
    name = 'Dogrename1721198056227'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "dog" RENAME COLUMN "name" TO "dog_name"`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "dog" RENAME COLUMN "dog_name" TO "name"`);
    }
}

  1. BreedRename1721198576725.ts
import { MigrationInterface, QueryRunner } from "typeorm";

export class BreedRename1721198576725 implements MigrationInterface {
    name = 'BreedRename1721198576725'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "breed" RENAME COLUMN "name" TO "breed_name"`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "breed" RENAME COLUMN "breed_name" TO "name"`);
    }
}

How can I run only one specific migration file, for instance, Dogrename1721198056227.ts, without running all pending migrations? Is there a way to achieve this using TypeORM ?

What I've Tried:

npm run migration:run

However, this command runs all pending migrations. I need to execute only one specific migration.

Additional Context:

Upvotes: 0

Views: 353

Answers (1)

Gennady Wertinsky
Gennady Wertinsky

Reputation: 9

The migration mechanism does not provide for this and needs to be “broken”: option 1: add migrations that should not be performed to the "migrations" table. But there will be problems when rolling back migrations. OR option 2: rename the inappropriate migration files so that they do not match the pattern ./src/migrations/*.ts. After that you can run npm run migration:run.

Upvotes: 0

Related Questions