# TypeORM Migrations ## Steps for Dev ### 1. Install typeorm globally ```bash sudo npm install -g typeorm ``` ### 2. Make ormconfig.js file and put it in project root. ```javascript module.exports = { "type": "type", // postgres, mysql "host": "localhost", "port": 5432, "username": "username", "password": "password", "database": "test", "dropSchema" : false, "entities": ["./src/domain/**/*.ts"], // path to domain classes "migrationsTableName": "migrations", "migrations": ["./src/migrations/**/*.ts"], // path to migrations directory "cli": { "migrationsDir": "./src/migrations" } } ``` ### 3. Make changes to domain classes example: *adding a column to a pre existing class* ```typescript // important check for @Entity , @Column attributes at start of entity import {Column, Entity} from "typeorm"; @Entity('x') export class X{ // pre existing code ... ... ... // add new column @Column({nullable: true, array: true, type: String}) newColumnName: string[]; } ``` ### 4. Generate a migration with a name On console ```bash npx ts-node ./node_modules/.bin/typeorm migration:generate -n AddNewColumnToX ``` ### 5. Check for migration in migration folder *you should see this code* `instance for a postgres db` ```typescript import {MigrationInterface, QueryRunner} from "typeorm"; export class AddNewColumnToX1534491159669 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise<any> { await queryRunner.query(`ALTER TABLE "X" ADD "newColumnName" character varying array`); } public async down(queryRunner: QueryRunner): Promise<any> { await queryRunner.query(`ALTER TABLE "X" DROP COLUMN "newColumnName"`); } } ``` ### 6. Run Migrations *on console* ```bash npx ts-node ./node_modules/.bin/typeorm migration:run ``` *on first migration , this should result* ```sql query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations' query: CREATE TABLE "migrations" ("id" SERIAL NOT NULL, "timestamp" bigint NOT NULL, "name" character varying NOT NULL, CONSTRAINT "PK_8c82d7f526340ab734260ea46be" PRIMARY KEY ("id")) query: SELECT * FROM "migrations" "migrations" 0 migrations are already loaded in the database. 1 migrations were found in the source code. 1 migrations are new migrations that needs to be executed. query: START TRANSACTION query: ALTER TABLE "X" ADD "newColumnName" character varying array query: INSERT INTO "migrations"("timestamp", "name") VALUES ($1, $2) -- PARAMETERS: [1534491159669,"AddNewColumnToX1534491159669"] Migration AddNewColumnToX1534491159669 has been executed successfully. query: COMMIT ``` ## Steps where there is no ts-node ### 1. Have js files generated in dev - check if 'migrations' directory exists in 'dist' directory - check for required migration files. - if required files do not exist, get them generated using while build using `run tsc` ### 2. Use Js path in ormconfig.js ```javascript module.exports = { "type": "type", "host": config.get('db.host'), "port": 5432, "username": config.get('db.user'), "password": config.get('db.password'), "database": config.get('db.db'), "dropSchema": config.get('db.dropSchema'), "entities": ["./dist/domain/**/*.js"], "migrationsTableName": "migrations", "migrations": ["./dist/migrations/**/*.js"], "cli": { "migrationsDir": "./dist/migrations" } } ``` **-- _check for migration folder in dist folder and generated js files for migrations, only proceed if required migration files exist in dist folder_ ** ### 3. Run Migration ``` typeorm migration:run ```