Re_Backend/MIGRATION_QUICK_REFERENCE.md

121 lines
2.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 🚀 Migration Quick Reference
## Daily Development Workflow
### Starting Development (Auto-runs Migrations)
```bash
npm run dev
```
**This will automatically run all new migrations before starting the server!**
### Run Migrations Only
```bash
npm run migrate
```
## Adding a New Migration (3 Steps)
### 1⃣ Create Migration File
Location: `src/migrations/YYYYMMDD-description.ts`
```typescript
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`
```typescript
// 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
```bash
npm run migrate
```
## Common Operations
### Add Column
```typescript
await queryInterface.addColumn('table', 'column', {
type: DataTypes.STRING(100),
allowNull: false,
defaultValue: 'value'
});
```
### Add Foreign Key
```typescript
await queryInterface.addColumn('table', 'foreign_id', {
type: DataTypes.UUID,
references: { model: 'other_table', key: 'id' },
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
});
```
### Add Index
```typescript
await queryInterface.addIndex('table', ['column'], {
name: 'idx_table_column'
});
```
### Create Table
```typescript
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 flag
- `skipped_at` - Timestamp
- `skipped_by` - User reference
- `skip_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