From 14586d2562b36be811144f0a1f581cffad755a7c Mon Sep 17 00:00:00 2001 From: Bartek Kruszczynski Date: Thu, 11 Jul 2024 15:30:26 +0200 Subject: [PATCH 1/2] Optionally wrap each migration into its own transaction --- index.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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, { From 180db7aee2db9788737bf4ccd51400756ba4815a Mon Sep 17 00:00:00 2001 From: Bartek Kruszczynski Date: Tue, 31 Dec 2024 14:34:02 +0100 Subject: [PATCH 2/2] Document the option in README --- README.md | 1 + 1 file changed, 1 insertion(+) 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) => {