diff --git a/README.md b/README.md index 3abf65c..2592fab 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ shift({ before: ({ migration_id, name }) => { console.log('Migrating', migration_id, name); }, + transactionPerEachMigration: true, // defaults to false }) .then(() => console.log('All good')) .catch((err) => { diff --git a/index.js b/index.js index 0115c85..469f4c0 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,8 @@ export default async function({ sql, path = join(process.cwd(), 'migrations'), before = null, - after = null + after = null, + transactionPerEachMigration = false }) { const migrations = fs.readdirSync(path) .filter(x => fs.statSync(join(path, x)).isDirectory() && x.match(/^[0-9]{5}_/)) @@ -28,17 +29,27 @@ export default async function({ const current = await getCurrentMigration() const needed = migrations.slice(current ? current.id : 0) - return sql.begin(next) + return transactionPerEachMigration ? next(sql) : sql.begin(next) async function next(sql) { const current = needed.shift() if (!current) return + if (transactionPerEachMigration) { + await sql.begin(async (sql) => { + await step(sql, current); + }) + } else { + await step(sql, current); + } + await next(sql) + } + + async function step(sql, current) { before && before(current) await run(sql, current) after && after(current) - await next(sql) } async function run(sql, {