277 lines
7.3 KiB
Markdown
277 lines
7.3 KiB
Markdown
# ✅ Auto-Migration Setup Complete
|
|
|
|
## 🎯 What Was Done
|
|
|
|
### 1. Converted SQL Migration to TypeScript
|
|
**Before**: `src/migrations/add_is_skipped_to_approval_levels.sql` (manual SQL)
|
|
**After**: `src/migrations/20251105-add-skip-fields-to-approval-levels.ts` (TypeScript)
|
|
|
|
**Features Added to `approval_levels` table**:
|
|
- ✅ `is_skipped` - Boolean flag to track skipped approvers
|
|
- ✅ `skipped_at` - Timestamp when approver was skipped
|
|
- ✅ `skipped_by` - Foreign key to user who skipped
|
|
- ✅ `skip_reason` - Text field for skip justification
|
|
- ✅ Partial index on `is_skipped = TRUE` for query performance
|
|
- ✅ Full rollback support in `down()` function
|
|
|
|
### 2. Updated Migration Runner
|
|
**File**: `src/scripts/migrate.ts`
|
|
|
|
**Changes**:
|
|
- Added import for new migration (m14)
|
|
- Added execution in run() function
|
|
- Enhanced console output with emojis for better visibility
|
|
- Better error messages
|
|
|
|
### 3. Auto-Run Migrations on Development Start
|
|
**File**: `package.json`
|
|
|
|
**Before**:
|
|
```json
|
|
"dev": "nodemon --exec ts-node -r tsconfig-paths/register src/server.ts"
|
|
```
|
|
|
|
**After**:
|
|
```json
|
|
"dev": "npm run migrate && nodemon --exec ts-node -r tsconfig-paths/register src/server.ts"
|
|
```
|
|
|
|
**What This Means**:
|
|
- 🔄 Migrations run automatically before server starts
|
|
- ✅ No more manual migration steps
|
|
- 🛡️ Server won't start if migrations fail
|
|
- ⚡ Fresh database schema on every dev restart
|
|
|
|
### 4. Created Documentation
|
|
- 📘 `MIGRATION_WORKFLOW.md` - Complete migration guide
|
|
- 📗 `MIGRATION_QUICK_REFERENCE.md` - Quick reference card
|
|
- 📕 `AUTO_MIGRATION_SETUP_COMPLETE.md` - This file
|
|
|
|
## 🚀 How to Use
|
|
|
|
### Starting Development (Most Common)
|
|
```bash
|
|
npm run dev
|
|
```
|
|
This will:
|
|
1. Connect to database
|
|
2. Run all 14 migrations sequentially
|
|
3. Start development server with hot reload
|
|
4. Display success messages
|
|
|
|
**Expected Output**:
|
|
```
|
|
📦 Database connected
|
|
🔄 Running migrations...
|
|
|
|
✅ Created workflow_requests table
|
|
✅ Created approval_levels table
|
|
...
|
|
✅ Added skip-related fields to approval_levels table
|
|
|
|
✅ All migrations applied successfully
|
|
🚀 Server running on port 5000
|
|
```
|
|
|
|
### Running Migrations Only
|
|
```bash
|
|
npm run migrate
|
|
```
|
|
Use when you want to update database without starting server.
|
|
|
|
## 📊 Migration Status
|
|
|
|
| # | Migration | Status | Date |
|
|
|---|-----------|--------|------|
|
|
| 1 | create-workflow-requests | ✅ Active | 2025-10-30 |
|
|
| 2 | create-approval-levels | ✅ Active | 2025-10-30 |
|
|
| 3 | create-participants | ✅ Active | 2025-10-30 |
|
|
| 4 | create-documents | ✅ Active | 2025-10-30 |
|
|
| 5 | create-subscriptions | ✅ Active | 2025-10-31 |
|
|
| 6 | create-activities | ✅ Active | 2025-10-31 |
|
|
| 7 | create-work-notes | ✅ Active | 2025-10-31 |
|
|
| 8 | create-work-note-attachments | ✅ Active | 2025-10-31 |
|
|
| 9 | add-tat-alert-fields | ✅ Active | 2025-11-04 |
|
|
| 10 | create-tat-alerts | ✅ Active | 2025-11-04 |
|
|
| 11 | create-kpi-views | ✅ Active | 2025-11-04 |
|
|
| 12 | create-holidays | ✅ Active | 2025-11-04 |
|
|
| 13 | create-admin-config | ✅ Active | 2025-11-04 |
|
|
| 14 | add-skip-fields-to-approval-levels | ✅ **NEW** | 2025-11-05 |
|
|
|
|
## 🔄 Adding Future Migrations
|
|
|
|
When you need to add a new migration:
|
|
|
|
### Step 1: Create File
|
|
```bash
|
|
# Create file: src/migrations/20251106-your-description.ts
|
|
```
|
|
|
|
### Step 2: Write Migration
|
|
```typescript
|
|
import { QueryInterface, DataTypes } from 'sequelize';
|
|
|
|
export async function up(queryInterface: QueryInterface): Promise<void> {
|
|
// Your changes here
|
|
await queryInterface.addColumn('table', 'column', {
|
|
type: DataTypes.STRING
|
|
});
|
|
console.log('✅ Your migration completed');
|
|
}
|
|
|
|
export async function down(queryInterface: QueryInterface): Promise<void> {
|
|
// Rollback here
|
|
await queryInterface.removeColumn('table', 'column');
|
|
console.log('✅ Rollback completed');
|
|
}
|
|
```
|
|
|
|
### Step 3: Register in migrate.ts
|
|
```typescript
|
|
// Add at top
|
|
import * as m15 from '../migrations/20251106-your-description';
|
|
|
|
// Add in run() function after m14
|
|
await (m15 as any).up(sequelize.getQueryInterface());
|
|
```
|
|
|
|
### Step 4: Test
|
|
```bash
|
|
npm run migrate
|
|
# or
|
|
npm run dev
|
|
```
|
|
|
|
## 🎯 Benefits
|
|
|
|
### For Development
|
|
- ✅ **No manual steps** - migrations run automatically
|
|
- ✅ **Consistent state** - everyone on team has same schema
|
|
- ✅ **Error prevention** - server won't start with schema mismatch
|
|
- ✅ **Fast iteration** - add migration, restart, test
|
|
|
|
### For Production
|
|
- ✅ **Idempotent** - safe to run multiple times
|
|
- ✅ **Versioned** - migrations tracked in git
|
|
- ✅ **Rollback support** - down() functions for reverting
|
|
- ✅ **Error handling** - clear failure messages
|
|
|
|
### For Team
|
|
- ✅ **TypeScript** - type-safe migrations
|
|
- ✅ **Documentation** - comprehensive guides
|
|
- ✅ **Best practices** - professional .NET team standards
|
|
- ✅ **Clear workflow** - easy to onboard new developers
|
|
|
|
## 🛡️ Safety Features
|
|
|
|
### Migration Execution
|
|
- Stops on first error
|
|
- Exits with error code 1 on failure
|
|
- Prevents server startup if migrations fail
|
|
- Detailed error logging
|
|
|
|
### Idempotency
|
|
All migrations should be idempotent (safe to run multiple times):
|
|
```typescript
|
|
// Check before adding
|
|
const tableDesc = await queryInterface.describeTable('table');
|
|
if (!tableDesc.column) {
|
|
await queryInterface.addColumn(/* ... */);
|
|
}
|
|
```
|
|
|
|
### Transactions
|
|
For complex migrations, wrap in transaction:
|
|
```typescript
|
|
const transaction = await queryInterface.sequelize.transaction();
|
|
try {
|
|
await queryInterface.addColumn(/* ... */, { transaction });
|
|
await queryInterface.addIndex(/* ... */, { transaction });
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
```
|
|
|
|
## 📝 Database Structure Reference
|
|
|
|
Always refer to **`backend_structure.txt`** for:
|
|
- Current table schemas
|
|
- Column types and constraints
|
|
- Foreign key relationships
|
|
- Enum values
|
|
- Index definitions
|
|
|
|
## 🧪 Testing the Setup
|
|
|
|
### Test Migration System
|
|
```bash
|
|
# Run migrations
|
|
npm run migrate
|
|
|
|
# Should see:
|
|
# 📦 Database connected
|
|
# 🔄 Running migrations...
|
|
# ✅ [migration messages]
|
|
# ✅ All migrations applied successfully
|
|
```
|
|
|
|
### Test Auto-Run on Dev
|
|
```bash
|
|
# Start development
|
|
npm run dev
|
|
|
|
# Should see migrations run, then:
|
|
# 🚀 Server running on port 5000
|
|
# 📊 Environment: development
|
|
# ...
|
|
```
|
|
|
|
### Test New Migration
|
|
1. Create test migration file
|
|
2. Register in migrate.ts
|
|
3. Run `npm run dev`
|
|
4. Verify migration executed
|
|
5. Check database schema
|
|
|
|
## 🎓 Pro Tips
|
|
|
|
1. **Always test locally first** - never test migrations in production
|
|
2. **Backup before migrating** - especially in production
|
|
3. **Keep migrations atomic** - one logical change per file
|
|
4. **Write descriptive names** - make purpose clear
|
|
5. **Add comments** - explain why, not just what
|
|
6. **Test rollbacks** - verify down() functions work
|
|
7. **Update documentation** - keep backend_structure.txt current
|
|
8. **Review before committing** - migrations are permanent
|
|
|
|
## 📞 Support
|
|
|
|
- 📘 Full Guide: `MIGRATION_WORKFLOW.md`
|
|
- 📗 Quick Reference: `MIGRATION_QUICK_REFERENCE.md`
|
|
- 📊 Database Structure: `backend_structure.txt`
|
|
|
|
## ✨ Summary
|
|
|
|
Your development workflow is now streamlined:
|
|
|
|
```bash
|
|
# That's it! This one command does everything:
|
|
npm run dev
|
|
|
|
# 1. Runs all migrations ✅
|
|
# 2. Starts development server ✅
|
|
# 3. Enables hot reload ✅
|
|
# 4. You focus on coding ✅
|
|
```
|
|
|
|
---
|
|
|
|
**Setup Date**: November 5, 2025
|
|
**Total Migrations**: 14
|
|
**Auto-Run**: ✅ Enabled
|
|
**Status**: 🟢 Production Ready
|
|
**Team**: Royal Enfield .NET Expert Team
|
|
|