Skip to content

Add TypeORM database migrations and disable synchronize in production #165

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

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