Search code examples
typescripttypeorm

typeorm migration api does not generate automatic code


hello I have a problem to generate the code of my migrations automatically. basically i following some tutorials when changing a column the migration code is generated automatically, but it doesn't happen to me.

my script on package.json:

  "scripts": {
    "commit": "git-cz",
    "build": "babel src --extensions \".js,.ts\" --out-dir dist --copy-files --no-copy-ignored",
    "start": "node dist/server.js",
    "dev:server": "ts-node-dev --inspect --respawn --transpile-only --ignore-watch node_modules -r tsconfig-paths/register src/shared/infra/http/server.ts",
    "test": "jest",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
    "lint": "eslint --fix"
  },

my entitiy ( i change random column to other name to test generate migration with automatic code)

users.entitiy.ts:

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { SharedProp } from './sharedProp.helper';

@Entity({ name: 'users' })
export class User extends SharedProp {
  constructor(isActive: boolean, login: string, password: string) {
    super();
    this.isActive = isActive;
    this.login = login;
    this.password = password;
  }
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'login', nullable: false })
  login: string;

  @Column({ nullable: false })
  password: string;

  @Column({ name: 'is_active', nullable: false })
  isActive: boolean;
}

and this is my ormconfig:

const rootDir = process.env.NODE_ENV === 'development' ? 'src' : 'build/src';

module.exports = {
  type: 'postgres',
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  synchronize: false,
  logging: false,
  entities: [rootDir + '/entities/**/*.{js,ts}'],
  migrations: [rootDir + '/migrations/*.{js,ts}'],
  subscribers: [rootDir + '/subscribers/**/*.{js,ts}'],
  seeds: [rootDir + '/migrations/seeds/**/*.{js,ts}'],
  factories: [rootDir + '/migrations/factories/**/*.{js,ts}'],
  cli: {
    entitiesDir: `${rootDir}/entities`,
    migrationsDir: `${rootDir}/migration`,
    subscribersDir: `${rootDir}/subscriber`,
  },
};

I started my server, changed the column password to passwords and executed the command:

yarn typeorm -- migration:create -n Password 
yarn run v1.22.4
warning From Yarn 1.0 onwards, scripts don't require "--" for options to 
be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.
$ node --require ts-node/register ./node_modules/typeorm/cli.js migration:create -n Password
Migration G:\emasati_stockcontrol/src/migration/1595290975334-Password.ts has been generated successfully.
Done in 1.86s.

but for some reason it is not generating the alter table automatically in the migration:

export class Password1595290975334 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
    }

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

}

Solution

  • Try add to package.json :

    "scripts": {
       ...
       "migration:generate": "yarn run typeorm migration:generate -n"
    }
    

    and execute this command:

    yarn run migration:generate -- [MigrationNameWithoutBrackets]