Skip to content

[BE-47] Replace TypeORM synchronize: true with proper migrations #1237

Description

@yusuftomilola

Overview

AppModule sets synchronize: true in the TypeORM config, which automatically alters the database schema on every app start. This can silently drop columns on any non-local environment and is explicitly unsafe for production by the TypeORM docs.

Context

  • TypeORM config: backend/src/app.module.tssynchronize: true on line ~85
  • All entity files are in backend/src/*/entities/
  • TypeORM CLI uses a separate DataSource config file pointing to entity paths

Tasks

  • Set synchronize: false in TypeOrmModule.forRootAsync() config for all environments
  • Create backend/src/data-source.ts as the TypeORM CLI data source file:
    • Imports all entity files explicitly
    • Reads DB config from .env via dotenv
    • Points migrations to src/migrations/**/*.ts
  • Add the following scripts to backend/package.json:
    • "migration:generate": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:generate -d src/data-source.ts"
    • "migration:run": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:run -d src/data-source.ts"
    • "migration:revert": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:revert -d src/data-source.ts"
  • Generate the initial migration from the current entity state and commit it:
    npm run migration:generate -- src/migrations/InitialSchema
  • Ensure the initial migration runs cleanly against a fresh database: npm run migration:run
  • Document in .env.example that migrations must be run manually after deployment

Files to Modify / Create

  • backend/src/app.module.ts
  • New: backend/src/data-source.ts
  • New: backend/src/migrations/ (generated migration file)
  • backend/package.json

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions