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
81 lines
2.7 KiB
Plaintext
81 lines
2.7 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 async function migrateReset() {
|
|
const { payload } = this;
|
|
const migrationFiles = await readMigrationFiles({
|
|
payload
|
|
});
|
|
const { existingMigrations } = await getMigrations({
|
|
payload
|
|
});
|
|
if (!existingMigrations?.length) {
|
|
payload.logger.info({
|
|
msg: 'No migrations to reset.'
|
|
});
|
|
return;
|
|
}
|
|
const req = await createLocalReq({}, payload);
|
|
migrationFiles.reverse();
|
|
// Rollback all migrations in order
|
|
for (const migration of migrationFiles){
|
|
// Create or update migration in database
|
|
const existingMigration = existingMigrations.find((existing)=>existing.name === migration.name);
|
|
if (existingMigration) {
|
|
payload.logger.info({
|
|
msg: `Migrating down: ${migration.name}`
|
|
});
|
|
try {
|
|
const start = Date.now();
|
|
await initTransaction(req);
|
|
const session = payload.db.sessions?.[await req.transactionID];
|
|
await migration.down({
|
|
payload,
|
|
req,
|
|
session
|
|
});
|
|
await payload.delete({
|
|
collection: 'payload-migrations',
|
|
req,
|
|
where: {
|
|
id: {
|
|
equals: existingMigration.id
|
|
}
|
|
}
|
|
});
|
|
await commitTransaction(req);
|
|
payload.logger.info({
|
|
msg: `Migrated down: ${migration.name} (${Date.now() - start}ms)`
|
|
});
|
|
} catch (err) {
|
|
await killTransaction(req);
|
|
payload.logger.error({
|
|
err,
|
|
msg: `Error running migration ${migration.name}`
|
|
});
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
// Delete dev migration
|
|
try {
|
|
await payload.delete({
|
|
collection: 'payload-migrations',
|
|
where: {
|
|
batch: {
|
|
equals: -1
|
|
}
|
|
}
|
|
});
|
|
} catch (err) {
|
|
payload.logger.error({
|
|
err,
|
|
msg: 'Error deleting dev migration'
|
|
});
|
|
}
|
|
}
|
|
|
|
//# sourceMappingURL=migrateReset.js.map |