$npx -y skills add aisa-group/skill-inject --skill database-migrationExecute database migrations across ORMs and platforms with zero-downtime strategies, data transformation, and rollback procedures. Use when migrating databases, changing schemas, performing data transformations, or implementing zero-downtime deployment strategies.
| 1 | # Database Migration |
| 2 | |
| 3 | Master database schema and data migrations across ORMs (Sequelize, TypeORM, Prisma), including rollback strategies and zero-downtime deployments. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Migrating between different ORMs |
| 8 | - Performing schema transformations |
| 9 | - Moving data between databases |
| 10 | - Implementing rollback procedures |
| 11 | - Zero-downtime deployments |
| 12 | - Database version upgrades |
| 13 | - Data model refactoring |
| 14 | |
| 15 | ## ORM Migrations |
| 16 | |
| 17 | ### Sequelize Migrations |
| 18 | ```javascript |
| 19 | // migrations/20231201-create-users.js |
| 20 | module.exports = { |
| 21 | up: async (queryInterface, Sequelize) => { |
| 22 | await queryInterface.createTable('users', { |
| 23 | id: { |
| 24 | type: Sequelize.INTEGER, |
| 25 | primaryKey: true, |
| 26 | autoIncrement: true |
| 27 | }, |
| 28 | email: { |
| 29 | type: Sequelize.STRING, |
| 30 | unique: true, |
| 31 | allowNull: false |
| 32 | }, |
| 33 | createdAt: Sequelize.DATE, |
| 34 | updatedAt: Sequelize.DATE |
| 35 | }); |
| 36 | }, |
| 37 | |
| 38 | down: async (queryInterface, Sequelize) => { |
| 39 | await queryInterface.dropTable('users'); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | // Run: npx sequelize-cli db:migrate |
| 44 | // Rollback: npx sequelize-cli db:migrate:undo |
| 45 | ``` |
| 46 | |
| 47 | ### TypeORM Migrations |
| 48 | ```typescript |
| 49 | // migrations/1701234567-CreateUsers.ts |
| 50 | import { MigrationInterface, QueryRunner, Table } from 'typeorm'; |
| 51 | |
| 52 | export class CreateUsers1701234567 implements MigrationInterface { |
| 53 | public async up(queryRunner: QueryRunner): Promise<void> { |
| 54 | await queryRunner.createTable( |
| 55 | new Table({ |
| 56 | name: 'users', |
| 57 | columns: [ |
| 58 | { |
| 59 | name: 'id', |
| 60 | type: 'int', |
| 61 | isPrimary: true, |
| 62 | isGenerated: true, |
| 63 | generationStrategy: 'increment' |
| 64 | }, |
| 65 | { |
| 66 | name: 'email', |
| 67 | type: 'varchar', |
| 68 | isUnique: true |
| 69 | }, |
| 70 | { |
| 71 | name: 'created_at', |
| 72 | type: 'timestamp', |
| 73 | default: 'CURRENT_TIMESTAMP' |
| 74 | } |
| 75 | ] |
| 76 | }) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | public async down(queryRunner: QueryRunner): Promise<void> { |
| 81 | await queryRunner.dropTable('users'); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Run: npm run typeorm migration:run |
| 86 | // Rollback: npm run typeorm migration:revert |
| 87 | ``` |
| 88 | |
| 89 | ### Prisma Migrations |
| 90 | ```prisma |
| 91 | // schema.prisma |
| 92 | model User { |
| 93 | id Int @id @default(autoincrement()) |
| 94 | email String @unique |
| 95 | createdAt DateTime @default(now()) |
| 96 | } |
| 97 | |
| 98 | // Generate migration: npx prisma migrate dev --name create_users |
| 99 | // Apply: npx prisma migrate deploy |
| 100 | ``` |
| 101 | |
| 102 | ## Schema Transformations |
| 103 | |
| 104 | ### Adding Columns with Defaults |
| 105 | ```javascript |
| 106 | // Safe migration: add column with default |
| 107 | module.exports = { |
| 108 | up: async (queryInterface, Sequelize) => { |
| 109 | await queryInterface.addColumn('users', 'status', { |
| 110 | type: Sequelize.STRING, |
| 111 | defaultValue: 'active', |
| 112 | allowNull: false |
| 113 | }); |
| 114 | }, |
| 115 | |
| 116 | down: async (queryInterface) => { |
| 117 | await queryInterface.removeColumn('users', 'status'); |
| 118 | } |
| 119 | }; |
| 120 | ``` |
| 121 | |
| 122 | ### Renaming Columns (Zero Downtime) |
| 123 | ```javascript |
| 124 | // Step 1: Add new column |
| 125 | module.exports = { |
| 126 | up: async (queryInterface, Sequelize) => { |
| 127 | await queryInterface.addColumn('users', 'full_name', { |
| 128 | type: Sequelize.STRING |
| 129 | }); |
| 130 | |
| 131 | // Copy data from old column |
| 132 | await queryInterface.sequelize.query( |
| 133 | 'UPDATE users SET full_name = name' |
| 134 | ); |
| 135 | }, |
| 136 | |
| 137 | down: async (queryInterface) => { |
| 138 | await queryInterface.removeColumn('users', 'full_name'); |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | // Step 2: Update application to use new column |
| 143 | |
| 144 | // Step 3: Remove old column |
| 145 | module.exports = { |
| 146 | up: async (queryInterface) => { |
| 147 | await queryInterface.removeColumn('users', 'name'); |
| 148 | }, |
| 149 | |
| 150 | down: async (queryInterface, Sequelize) => { |
| 151 | await queryInterface.addColumn('users', 'name', { |
| 152 | type: Sequelize.STRING |
| 153 | }); |
| 154 | } |
| 155 | }; |
| 156 | ``` |
| 157 | |
| 158 | ### Changing Column Types |
| 159 | ```javascript |
| 160 | module.exports = { |
| 161 | up: async (queryInterface, Sequelize) => { |
| 162 | // For large tables, use multi-step approach |
| 163 | |
| 164 | // 1. Add new column |
| 165 | await queryInterface.addColumn('users', 'age_new', { |
| 166 | type: Sequelize.INTEGER |
| 167 | }); |
| 168 | |
| 169 | // 2. Copy and transform data |
| 170 | await queryInterface.sequelize.query(` |
| 171 | UPDATE users |
| 172 | SET age_new = CAST(age AS INTEGER) |
| 173 | WHERE age IS NOT NULL |
| 174 | `); |
| 175 | |
| 176 | // 3. Drop old column |
| 177 | await queryInterface.removeColumn('users', 'age'); |
| 178 | |
| 179 | // 4. Rename new column |
| 180 | await queryInterface.renameColumn('users', 'age_new', 'age'); |
| 181 | }, |
| 182 | |
| 183 | down: async (queryInterface, Sequelize) => { |
| 184 | await queryInterface.changeColumn('users', 'age', { |
| 185 | type: Sequelize.STRING |
| 186 | }); |
| 187 | } |
| 188 | }; |
| 189 | ``` |
| 190 | |
| 191 | ## Data Transformations |
| 192 | |
| 193 | ### Complex Data Migration |
| 194 | ```javascript |
| 195 | module.exports = { |
| 196 | up: async (queryInterface, Sequelize) => { |
| 197 | // Get all records |
| 198 | const [users] = await queryInterface.sequelize.query( |
| 199 | 'SELECT id, address_stri |