Skip to content

Add TypeORM database migrations and disable synchronize in production #165

Closed

Description

@Josie123-Dev

What

Generate initial TypeORM migrations from the current entity definitions, add a migration generation script, and ensure synchronize: false in production configuration with explicit migration execution on startup.

Why

The backend uses TypeORM with synchronize: true which auto-creates/alters database tables on every startup. This is fine for development but catastrophic in production — it can drop columns, lose data, or cause downtime during schema changes. The deploy script references npm run typeorm migration:run but no migration files exist. A proper migration workflow is essential before any production deployment.

Scope

In scope:

  • Generate initial migration from current entity definitions (npm run typeorm migration:generate)
  • Add migration scripts to package.json: migration:generate, migration:run, migration:revert
  • Configure TypeORM to use synchronize: true only in development, false in production
  • Add migration execution to the application bootstrap (run pending migrations on startup)
  • Create a data-source.ts file for TypeORM CLI configuration

Out of scope:

  • Changing entity definitions
  • Adding new entities
  • Database seeding (separate issue)
  • Multi-database support

Acceptance Criteria

  • Initial migration file exists in backend/src/migrations/
  • npm run migration:generate creates a new migration from entity changes
  • npm run migration:run applies all pending migrations
  • npm run migration:revert reverts the last migration
  • synchronize is true only when NODE_ENV=development
  • Application runs migrations on startup in production mode
  • data-source.ts exists for TypeORM CLI usage

Technical Context

  • TypeORM config: backend/src/app.module.ts (TypeOrmModule.forRoot)
  • Entities are auto-discovered via autoLoadEntities: true
  • TypeORM migration docs: https://typeorm.io/migrations
  • Deploy script: scripts/deploy-backend.sh references npm run typeorm migration:run
  • Create backend/src/data-source.ts for CLI: npx typeorm -d src/data-source.ts migration:generate

Activity

  1. koder-ns commented on Aug 23, 2026

    @koder-ns

    please can i work on this

  2. GauravKarakoti commented on Aug 23, 2026

    @GauravKarakoti

    Allow me to work on this one

  3. meshackyaro commented on Aug 23, 2026

    @meshackyaro
    Contributor

    Hi maintainer, I am submitting my application to set up the TypeORM migration workflow and eliminate synchronize: true for production environments.


    Approach & Technical Implementation

    1. CLI Data Source Setup (data-source.ts)

    • Isolated Configuration: Create backend/src/data-source.ts exporting a dedicated DataSource instance for the TypeORM CLI. It will read database credentials directly from environment variables (process.env) to support CLI commands like typeorm migration:generate.
    • Entity & Migration Glob Patterns: Wire entity paths (src/entities/**/*.ts or explicit imports) and migration output paths (src/migrations/*.ts) so generated migrations are automatically picked up and placed in backend/src/migrations/.

    2. AppModule & Bootstrap Integration

    • Environment-Gated synchronize: Update TypeOrmModule.forRootAsync in app.module.ts to ensure synchronize evaluates strictly to process.env.NODE_ENV === 'development'.
    • Automated Startup Migrations: Configure migrationsRun: true in the production TypeORM options (or explicitly execute dataSource.runMigrations() inside backend/src/main.ts prior to app.listen()) so pending schema updates execute reliably on container/app startup before accepting traffic.

    3. NPM Scripts & Workflow

    • Add standard scripts in package.json referencing the new data-source.ts:
    "typeorm": "typeorm-ts-node-commonjs -d src/data-source.ts",
    "migration:generate": "npm run typeorm -- migration:generate src/migrations/InitialSchema",
    "migration:run": "npm run typeorm -- migration:run",
    "migration:revert": "npm run typeorm -- migration:revert"
    

    Execution Plan

    • Phase 1 (Data Source & Initial Migration): Create backend/src/data-source.ts, verify database connection via CLI, and generate the baseline InitialSchema migration capturing the current state of all entities.
    • Phase 2 (Configuration Updates): Update app.module.ts to toggle synchronize based on NODE_ENV and enable automatic startup migration execution for non-development environments.
    • Phase 3 (Package Scripts): Register migration:generate, migration:run, and migration:revert in package.json.
    • Phase 4 (Validation):
    1. Test running migration:run and migration:revert against a clean test database.
    2. Verify npm run start:prod executes pending migrations on startup with synchronize: false.
    3. Ensure compatibility with scripts/deploy-backend.sh.

    Ready to pick this up as soon as assigned!

  4. grantfox-oss commented on Aug 23, 2026

    @grantfox-oss

    🦊 GrantFox — @meshackyaro has been assigned to this issue as part of the Third Campaign campaign!

    Next steps:

    1. Open a Pull Request referencing this issue (e.g., Closes #165)
    2. Your PR will be reviewed by the GuardZero144 maintainers

    Good luck! Track your progress on GrantFox.

  5. grantfox-oss commented on Aug 24, 2026

    @grantfox-oss

    🎉 This issue has been marked as completed on GrantFox as part of the Third Campaign campaign!

    @meshackyaro's PR #175 was approved and merged by @Josie123-Dev.

    🏆 @meshackyaro: You earned 35 FoxPoints for this contribution! Your current tier: Explorer (605 total points). Track your full progress on GrantFox.

    👏 Great work, @meshackyaro! Keep contributing to GuardZero144.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third CampaignbackendBackend related issuesdatabaseDatabase and migrationsdifficulty:hardHard difficulty issuespriority:highHigh priority issues

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions