How to run Typeorm Migrations on Heroku?

Asked

Viewed 377 times

2

By putting my code into production at Heroku, Migrations database are not running. How could I run them once I start my application?

Postgresql is the database used.

Archive of database configuration:

import { createConnection, getConnectionOptions } from 'typeorm';
import * as PostgressConnectionStringParser from 'pg-connection-string';

import User from '../models/User';
import Task from '../models/Task';
import Update from '../models/Update';

async function connect(): Promise<void> {
  const connectionOptions = await getConnectionOptions();

  if (process.env.NODE_ENV !== 'dev') {
    const databaseUrl: string = process.env.DATABASE_URL as string;
    const configDatabaseOptions = PostgressConnectionStringParser.parse(
      databaseUrl,
    );

    Object.assign(connectionOptions, {
      host: configDatabaseOptions.host,
      port: Number(configDatabaseOptions.port),
      username: configDatabaseOptions.user,
      password: configDatabaseOptions.password,
      database: configDatabaseOptions.database,
      entities: [Task, Update, User],
      migrations: ['./dist/database/migrations/*.js}'],
      cli: { migrationsDir: './dist/database/migrations' },
      synchronize: true,
      extra: {
        ssl: true,
      },
    });
  }

  const connection = await createConnection(connectionOptions);
  await connection.runMigrations();
}

connect();

Error message when trying to access an entity:

2020-05-28T16:04:45.999767+00:00 app[web.1]: RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection?
2020-05-28T16:04:45.999783+00:00 app[web.1]:     at new RepositoryNotFoundError (/app/node_modules/typeorm/error/RepositoryNotFoundError.js:11:28)
2020-05-28T16:04:45.999784+00:00 app[web.1]:     at EntityManager.getRepository (/app/node_modules/typeorm/entity-manager/EntityManager.js:660:19)
2020-05-28T16:04:45.999785+00:00 app[web.1]:     at Connection.getRepository (/app/node_modules/typeorm/connection/Connection.js:343:29)
2020-05-28T16:04:45.999786+00:00 app[web.1]:     at Object.getRepository (/app/node_modules/typeorm/index.js:281:55)
2020-05-28T16:04:45.999786+00:00 app[web.1]:     at CreateSessionService.<anonymous> (/app/dist/services/CreateSessionService.js:57:53)
2020-05-28T16:04:45.999787+00:00 app[web.1]:     at step (/app/dist/services/CreateSessionService.js:33:23)
2020-05-28T16:04:45.999788+00:00 app[web.1]:     at Object.next (/app/dist/services/CreateSessionService.js:14:53)
2020-05-28T16:04:45.999788+00:00 app[web.1]:     at /app/dist/services/CreateSessionService.js:8:71
2020-05-28T16:04:45.999789+00:00 app[web.1]:     at new Promise (<anonymous>)
2020-05-28T16:04:45.999790+00:00 app[web.1]:     at __awaiter (/app/dist/services/CreateSessionService.js:4:12)
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.