feat(db): add TypeORM migration support for production environments - #241
Open
hashbk wants to merge 1 commit into
Open
feat(db): add TypeORM migration support for production environments#241hashbk wants to merge 1 commit into
hashbk wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
synchronize: truewith a proper migration system for production safetysynchronize: truefor fast iteration; production uses explicit migration files for auditable, controllable schema changessrc/entities/index.ts) shared byapp.module.tsanddata-source.tsto prevent entity driftChanges
src/entities/index.tssrc/data-source.tssrc/database/migration.service.tssrc/migrations/1785305180672-InitialSchema.tssrc/migrations/README.mdsrc/app.module.tssrc/database/database.module.tssrc/database/database-init.service.tssrc/modules/user/entities/invitation.entity.tstype: 'varchar'for nullable note columnpackage.jsontsconfig.json.env.exampleBehavior Matrix
truefalseKey Design Decisions
MigrationService over
migrationsRun: true: TypeORM'smigrationsRunexecutes 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.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.Centralized entity list: A shared
ALL_ENTITIESarray insrc/entities/index.tsprevents the common problem of entity lists drifting betweenapp.module.tsanddata-source.ts.Test Plan
synchronize: trueworks, all tables creatednpm run migration:generate,npm run migration:show