TypeORM migrations quản lý schema changes an toàn. Không dùng synchronize: true trong production — có thể làm mất data.
Workflow chuẩn:
bash
# 1. Generate migration từ entity changes
npx typeorm migration:generate -d src/data-source.ts src/migrations/AddUserPhone
# 2. Review file migration được tạo
# src/migrations/1234567890-AddUserPhone.ts
# 3. Apply
npx typeorm migration:run -d src/data-source.ts
# 4. Revert nếu cần
npx typeorm migration:revert -d src/data-source.tsData Source file cần tách biệt với app module để CLI có thể dùng:
typescript
// src/data-source.ts
export const AppDataSource = new DataSource({
type: 'postgres',
entities: ['src/**/*.entity.ts'],
migrations: ['src/migrations/*.ts'],
synchronize: false,
});Best practices: chạy migration tự động khi app start trong production (runMigrations: true), never drop column directly — add new column, migrate data, then drop old.
Luôn test migration revert trước khi deploy.