#9481·typeorm

ConnectionMetadataBuilder.buildMigrations attempts to import migration files, even when no migration should be run

Author: darioackermannCreated Oct 20, 2022Updated Sep 23, 2026
Labelsbug

Issue Description

Expected Behavior

When the migrationsRun flag is not set (defaults to false), migrations should not be imported upon start of the application

Actual Behavior

TypeORM tries to import migrations regardless whether the flag is set to false which in our eyes is unwanted behavior. In our case the application crashes since it tries to parse typescript files without having the parser available

IMO migration files should not be touched (at all) when migrations are disabled.

Updated: Please see next post

Note that the syntax error is expected since it tries to execute TypeScript without being able to parse it, but we'd like to prevent migrations from running in first place.

We currently work arround this by deleting the migration folder during our CI/CD process, but that's not a solution we want to have there for long.

Steps to Reproduce

updated - see next post

My Environment

Dependency Version
Operating System Windows/Linux/AWS Lambda
Node.js version 16.18.0
Typescript version 4.8.4
TypeORM version 0.3.10

Additional Context

We have recently added migrations to our TypeORM configuration. The workcycle is as follows:

  1. Developer changes entities as needed
  2. Developer generates migrations locally and checks it to the versioning system
  3. CI/CD Pipeline applies the migration upon deployment

For reference, these are our configs:

app.module.ts

typescript
import {getDataSourceConfig} from "./data-source-config";
@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: async() : Promise<TypeOrmModuleOptions> => {
        return {...await getDataSourceConfig(), autoLoadEntities: true, migrationsRun: false}
      }
    }),
    // ...
  ] 
 // ...
})

data-source-config.ts

typescript
export const getDataSourceConfig = async(): Promise<DataSourceOptions> => {
  // needs to be async since we're fetching secret
  // logic

    return {
        name: "default",
        type: "postgres",
        host: host,
        port: port,
        username: username,
        password: password,
        database: db,
        entities: [path.join(__dirname, "..", "**", "entities", "*.entity.js")], // points to the dist entities
        synchronize: false,
        migrationsRun: false, // note the flag here
        logging: true,
        migrations: [path.join(__dirname, "..", "migrations", "*")] // points to migrations, a subfolder of the root folder
    };

})

Relevant Database Driver(s)

TypeORM Issue - not DB related

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

cc @SleepyMorpheus