Skip to content

Commit 14586d2

Browse files
committed
Optionally wrap each migration into its own transaction
1 parent e7a4cb1 commit 14586d2

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export default async function({
77
sql,
88
path = join(process.cwd(), 'migrations'),
99
before = null,
10-
after = null
10+
after = null,
11+
transactionPerEachMigration = false
1112
}) {
1213
const migrations = fs.readdirSync(path)
1314
.filter(x => fs.statSync(join(path, x)).isDirectory() && x.match(/^[0-9]{5}_/))
@@ -28,17 +29,27 @@ export default async function({
2829
const current = await getCurrentMigration()
2930
const needed = migrations.slice(current ? current.id : 0)
3031

31-
return sql.begin(next)
32+
return transactionPerEachMigration ? next(sql) : sql.begin(next)
3233

3334
async function next(sql) {
3435
const current = needed.shift()
3536
if (!current)
3637
return
3738

39+
if (transactionPerEachMigration) {
40+
await sql.begin(async (sql) => {
41+
await step(sql, current);
42+
})
43+
} else {
44+
await step(sql, current);
45+
}
46+
await next(sql)
47+
}
48+
49+
async function step(sql, current) {
3850
before && before(current)
3951
await run(sql, current)
4052
after && after(current)
41-
await next(sql)
4253
}
4354

4455
async function run(sql, {

0 commit comments

Comments
 (0)