Skip to content

feat(db): add TypeORM migration support for production environments - #241

Open
hashbk wants to merge 1 commit into
mainfrom
feat/typeorm-migration
Open

feat(db): add TypeORM migration support for production environments#241
hashbk wants to merge 1 commit into
mainfrom
feat/typeorm-migration

Conversation

@hashbk

@hashbk hashbk commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replace synchronize: true with a proper migration system for production safety
  • Development mode retains synchronize: true for fast iteration; production uses explicit migration files for auditable, controllable schema changes
  • Add centralized entity list (src/entities/index.ts) shared by app.module.ts and data-source.ts to prevent entity drift

Changes

File Change
src/entities/index.ts New — centralized entity list shared by app.module.ts and data-source.ts
src/data-source.ts New — TypeORM CLI data source config for migration commands
src/database/migration.service.ts New — runtime migration execution with existing database detection
src/migrations/1785305180672-InitialSchema.ts New — initial schema migration with all 26 entities
src/migrations/README.md New — migration workflow documentation
src/app.module.ts Modified — conditional synchronize/migrations based on NODE_ENV, use ALL_ENTITIES
src/database/database.module.ts Modified — add MigrationService provider
src/database/database-init.service.ts Modified — call migration service before seed data
src/modules/user/entities/invitation.entity.ts Fix — add explicit type: 'varchar' for nullable note column
package.json Modified — add migration npm scripts
tsconfig.json Modified — add ts-node commonjs config for CLI
.env.example Modified — add NODE_ENV documentation

Behavior Matrix

NODE_ENV synchronize migrations Description
Unset / development true skipped Dev mode: auto-sync schema
production false auto-run Prod mode: explicit migrations

Key Design Decisions

  1. MigrationService over migrationsRun: true: TypeORM's migrationsRun executes during DataSource initialization, before any NestJS lifecycle hooks. This prevents marking the initial migration as already run for existing databases, causing "table already exists" errors. MigrationService runs after DI initialization, allowing pre-migration checks.

  2. Existing database detection: When upgrading from synchronize: true, the existing database already has all tables. MigrationService detects this and marks the initial migration as already executed, preventing duplicate table creation.

  3. Centralized entity list: A shared ALL_ENTITIES array in src/entities/index.ts prevents the common problem of entity lists drifting between app.module.ts and data-source.ts.

Test Plan

  • Development mode (no NODE_ENV): synchronize: true works, all tables created
  • Production mode with empty database: migrations auto-run, all tables created, seed data initialized
  • Production mode with existing database: initial migration marked as already run, no duplicate table creation, existing data preserved
  • Migration CLI commands work: npm run migration:generate, npm run migration:show

Replace synchronize: true with a proper migration system for production
safety. Development mode retains synchronize for fast iteration, while
production uses explicit migration files for auditable, controllable
schema changes.

- Add centralized entity list (src/entities/index.ts) shared by
  app.module.ts and data-source.ts to prevent drift
- Add data-source.ts for TypeORM CLI migration commands
- Generate initial schema migration with all 26 entities
- Add MigrationService for runtime migration execution with existing
  database detection (marks initial migration as already run)
- Conditionally disable synchronize and enable migrations based on
  NODE_ENV (dev: synchronize=true, prod: migrations)
- Add npm scripts for migration:generate/run/revert/show/create
- Fix Invitation.note column missing explicit type for reflect-metadata
- Add ts-node commonjs config for CLI compatibility
- Update .env.example with NODE_ENV documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant