(this: DrizzleAdapter)
| 17 | * Run all migration down functions before running up |
| 18 | */ |
| 19 | export async function migrateRefresh(this: DrizzleAdapter) { |
| 20 | const { payload } = this |
| 21 | const migrationFiles = await readMigrationFiles({ payload }) |
| 22 | |
| 23 | const { existingMigrations, latestBatch } = await getMigrations({ |
| 24 | payload, |
| 25 | }) |
| 26 | |
| 27 | if (!existingMigrations?.length) { |
| 28 | payload.logger.info({ msg: 'No migrations to rollback.' }) |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | payload.logger.info({ |
| 33 | msg: `Rolling back batch ${latestBatch} consisting of ${existingMigrations.length} migration(s).`, |
| 34 | }) |
| 35 | |
| 36 | const req = await createLocalReq({}, payload) |
| 37 | |
| 38 | // Reverse order of migrations to rollback |
| 39 | existingMigrations.reverse() |
| 40 | |
| 41 | for (const migration of existingMigrations) { |
| 42 | try { |
| 43 | const migrationFile = migrationFiles.find((m) => m.name === migration.name) |
| 44 | if (!migrationFile) { |
| 45 | throw new Error(`Migration ${migration.name} not found locally.`) |
| 46 | } |
| 47 | |
| 48 | payload.logger.info({ msg: `Migrating down: ${migration.name}` }) |
| 49 | const start = Date.now() |
| 50 | await initTransaction(req) |
| 51 | const db = await getTransaction(this, req) |
| 52 | await migrationFile.down({ db, payload, req }) |
| 53 | payload.logger.info({ |
| 54 | msg: `Migrated down: ${migration.name} (${Date.now() - start}ms)`, |
| 55 | }) |
| 56 | |
| 57 | const tableExists = await migrationTableExists(this, db) |
| 58 | if (tableExists) { |
| 59 | await payload.delete({ |
| 60 | collection: 'payload-migrations', |
| 61 | req, |
| 62 | where: { |
| 63 | name: { |
| 64 | equals: migration.name, |
| 65 | }, |
| 66 | }, |
| 67 | }) |
| 68 | } |
| 69 | await commitTransaction(req) |
| 70 | } catch (err: unknown) { |
| 71 | await killTransaction(req) |
| 72 | payload.logger.error({ |
| 73 | err, |
| 74 | msg: parseError(err, `Error running migration ${migration.name}. Rolling back.`), |
| 75 | }) |
| 76 | process.exit(1) |
nothing calls this directly
no test coverage detected
searching dependent graphs…