2.7 KiB
2.7 KiB
🚀 Migration Quick Reference
Daily Development Workflow
Starting Development (Auto-runs Migrations)
npm run dev
✅ This will automatically run all new migrations before starting the server!
Run Migrations Only
npm run migrate
Adding a New Migration (3 Steps)
1️⃣ Create Migration File
Location: src/migrations/YYYYMMDD-description.ts
import { QueryInterface, DataTypes } from 'sequelize';
export async function up(queryInterface: QueryInterface): Promise<void> {
await queryInterface.addColumn('table_name', 'column_name', {
type: DataTypes.STRING,
allowNull: true,
});
console.log('✅ Migration completed');
}
export async function down(queryInterface: QueryInterface): Promise<void> {
await queryInterface.removeColumn('table_name', 'column_name');
console.log('✅ Rollback completed');
}
2️⃣ Register in src/scripts/migrate.ts
// Add import at top
import * as m15 from '../migrations/YYYYMMDD-description';
// Add execution in run() function
await (m15 as any).up(sequelize.getQueryInterface());
3️⃣ Test
npm run migrate
Common Operations
Add Column
await queryInterface.addColumn('table', 'column', {
type: DataTypes.STRING(100),
allowNull: false,
defaultValue: 'value'
});
Add Foreign Key
await queryInterface.addColumn('table', 'foreign_id', {
type: DataTypes.UUID,
references: { model: 'other_table', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
});
Add Index
await queryInterface.addIndex('table', ['column'], {
name: 'idx_table_column'
});
Create Table
await queryInterface.createTable('new_table', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
name: DataTypes.STRING(100),
created_at: DataTypes.DATE,
updated_at: DataTypes.DATE
});
What's New ✨
Latest Migration: Skip Approver Functionality
- File:
20251105-add-skip-fields-to-approval-levels.ts - Added Fields:
is_skipped- Boolean flagskipped_at- Timestampskipped_by- User referenceskip_reason- Text explanation
- Index: Partial index on
is_skipped = TRUE
Troubleshooting
| Issue | Solution |
|---|---|
| Migration fails | Check console error, fix migration file, re-run |
| Column exists error | Migration partially ran - add idempotent checks |
| Server won't start | Fix migration first, it blocks startup |
📚 Full Documentation
See MIGRATION_WORKFLOW.md for comprehensive guide.
Auto-Migration: ✅ Enabled
Total Migrations: 14
Latest: 2025-11-05