Create database migration scripts — up/down functions, data transformation, Docker-local testing. Triggers: 'migration', 'schema change', 'data migration', 'database update'.
Create safe, reversible database migration scripts for MongoDB schema changes.
src/migrations/<timestamp>-<description>.js (e.g., 20260305-add-status-to-orders.js).up(db) and down(db).up applies the change: .db.collection('orders').updateMany(...)down reverses it: restore previous field values, drop added indexes, etc.bulkWrite) for large datasets to avoid timeouts.// Template
module.exports = {
async up(db) {
await db.collection('orders').updateMany(
{ status: { $exists: false } },
{ $set: { status: 'pending' } }
);
},
async down(db) {
await db.collection('orders').updateMany(
{ status: 'pending' },
{ $unset: { status: '' } }
);
},
};
up — archive or rename instead.docker compose up -d mongo.seed-data skill if needed).node src/migrations/<file>.js up.node src/migrations/<file>.js down.state/STATE.md describing the migration.state/AI_AGENT_HANDOFF.md if the migration affects other agents' work.up and down functions both implemented.