Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
59 lines
2.1 KiB
Plaintext
59 lines
2.1 KiB
Plaintext
import { commitTransaction } from '../../utilities/commitTransaction.js';
|
|
import { createLocalReq } from '../../utilities/createLocalReq.js';
|
|
import { initTransaction } from '../../utilities/initTransaction.js';
|
|
import { killTransaction } from '../../utilities/killTransaction.js';
|
|
import { getMigrations } from './getMigrations.js';
|
|
import { readMigrationFiles } from './readMigrationFiles.js';
|
|
export const migrate = async function migrate(args) {
|
|
const { payload } = this;
|
|
const migrationFiles = args?.migrations || await readMigrationFiles({
|
|
payload
|
|
});
|
|
const { existingMigrations, latestBatch } = await getMigrations({
|
|
payload
|
|
});
|
|
const newBatch = latestBatch + 1;
|
|
// Execute 'up' function for each migration sequentially
|
|
for (const migration of migrationFiles){
|
|
const existingMigration = existingMigrations.find((existing)=>existing.name === migration.name);
|
|
// Run migration if not found in database
|
|
if (existingMigration) {
|
|
continue;
|
|
}
|
|
const start = Date.now();
|
|
const req = await createLocalReq({}, payload);
|
|
payload.logger.info({
|
|
msg: `Migrating: ${migration.name}`
|
|
});
|
|
try {
|
|
await initTransaction(req);
|
|
const session = payload.db.sessions?.[await req.transactionID];
|
|
await migration.up({
|
|
payload,
|
|
req,
|
|
session
|
|
});
|
|
payload.logger.info({
|
|
msg: `Migrated: ${migration.name} (${Date.now() - start}ms)`
|
|
});
|
|
await payload.create({
|
|
collection: 'payload-migrations',
|
|
data: {
|
|
name: migration.name,
|
|
batch: newBatch
|
|
},
|
|
req
|
|
});
|
|
await commitTransaction(req);
|
|
} catch (err) {
|
|
await killTransaction(req);
|
|
payload.logger.error({
|
|
err,
|
|
msg: `Error running migration ${migration.name}`
|
|
});
|
|
throw err;
|
|
}
|
|
}
|
|
};
|
|
|
|
//# sourceMappingURL=migrate.js.map |