fix: remove explicit BEGIN/COMMIT to avoid database locked error
sqlx connection pool can dispatch each execute to a different connection, so BEGIN on one and INSERT on another causes a lock. Sequential inserts without explicit transaction management avoids this. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
88219e657f
commit
764fdad6db
1 changed files with 16 additions and 29 deletions
|
|
@ -16,35 +16,22 @@ export async function insertBatch(
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
let insertedCount = 0;
|
let insertedCount = 0;
|
||||||
|
|
||||||
// Process in batches of 500
|
for (const tx of transactions) {
|
||||||
const batchSize = 500;
|
await db.execute(
|
||||||
for (let i = 0; i < transactions.length; i += batchSize) {
|
`INSERT INTO transactions (date, description, amount, source_id, file_id, original_description, category_id, supplier_id)
|
||||||
const batch = transactions.slice(i, i + batchSize);
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
||||||
|
[
|
||||||
await db.execute("BEGIN TRANSACTION", []);
|
tx.date,
|
||||||
try {
|
tx.description,
|
||||||
for (const tx of batch) {
|
tx.amount,
|
||||||
await db.execute(
|
tx.source_id,
|
||||||
`INSERT INTO transactions (date, description, amount, source_id, file_id, original_description, category_id, supplier_id)
|
tx.file_id,
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
tx.original_description,
|
||||||
[
|
tx.category_id ?? null,
|
||||||
tx.date,
|
tx.supplier_id ?? null,
|
||||||
tx.description,
|
]
|
||||||
tx.amount,
|
);
|
||||||
tx.source_id,
|
insertedCount++;
|
||||||
tx.file_id,
|
|
||||||
tx.original_description,
|
|
||||||
tx.category_id ?? null,
|
|
||||||
tx.supplier_id ?? null,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
insertedCount++;
|
|
||||||
}
|
|
||||||
await db.execute("COMMIT", []);
|
|
||||||
} catch (e) {
|
|
||||||
await db.execute("ROLLBACK", []);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return insertedCount;
|
return insertedCount;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue