Re_Backend/MIGRATION_QUICK_REFERENCE.md

2.7 KiB
Raw Blame History

🚀 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 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