📙 For any Help, Contact me on Discord (@raideno).
Create Migrations

Create Migration

Now that we have extended our built-in entity we need to map / reflect those changes to the database.

IMPORTANT Note: Here we will create the migrations for the product entity to add the rich_description field, depending on your usecase / fields you added this might change and you migrations might be different, to know more about migrations:

1- Generate Migration File

npx typeorm migration:create src/migrations/ProductCorrect

After running this command you'll have a simillar file:

src/migrations/1699007037122-ProductCorrect.ts

src/migrations/1696669623641-BlogPostCreate.ts
import { MigrationInterface, QueryRunner } from "typeorm";
 
export class ProductCorrect1699007037122 implements MigrationInterface {
    name = "ProductCorrect1699007037122";
 
    public async up(queryRunner: QueryRunner): Promise<void> {
        // Complete...
    }
 
    public async down(queryRunner: QueryRunner): Promise<void> {
        // Complete...
    }
}

2- Writing The Migration

src/migrations/1696669623641-BlogPostCreate.ts
import { MigrationInterface, QueryRunner } from "typeorm";
 
export class ProductCorrect1699007037122 implements MigrationInterface {
    name = "ProductCorrect1699007037122";
 
    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE "product" ADD COLUMN "rich_description" character varying
        `);
    }
 
    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE "product" DROP COLUMN "rich_description"
        `);
    }
}

3- Running The Migration

  1. Build your code
npm run build
  1. Run migrations
npx medusa migrations run

Need Help for your migrations ?