Skip to content

Commit 2d69b68

Browse files
committed
seed: one transaction per folder
1 parent 15fec13 commit 2d69b68

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

scripts/seed.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,28 @@ async function seed() {
2424
.map((f) => path.join(folder_path, f.name))
2525
.sort()
2626

27-
for (const file of files) {
28-
const base = path.basename(file)
29-
const is_valid = base?.match(/^[A-Za-z0-9_.,\-()]+$/)
30-
if (!is_valid) {
31-
throw new Error(`Invalid file name: ${base}`)
32-
}
33-
const sql = await fs.readFile(file, 'utf8')
34-
const tx = await db.transaction()
35-
try {
27+
const tx = await db.transaction()
28+
29+
try {
30+
for (const file of files) {
31+
const base = path.basename(file)
32+
const is_valid = base?.match(/^[A-Za-z0-9_.,\-()]+$/)
33+
if (!is_valid) {
34+
throw new Error(`Invalid file name: ${base}`)
35+
}
36+
const sql = await fs.readFile(file, 'utf8')
37+
3638
await tx.executeMultiple(sql)
37-
await tx.commit()
39+
3840
const operation = file.includes('clear') ? 'Clear data' : 'Insert data'
3941
console.info(`${operation}: ${base}`)
40-
} catch (err) {
41-
console.error(`Failed to process ${file}`, err)
42-
await tx.rollback()
43-
process.exit(1)
4442
}
43+
44+
await tx.commit()
45+
} catch (err) {
46+
console.error(`Failed to seed ${folder}`, err)
47+
await tx.rollback()
48+
process.exit(1)
4549
}
4650
}
4751

0 commit comments

Comments
 (0)