Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Database {
db.exec("COMMIT");
return result;
} catch (err) {
db.exec("ROLLBACK");
if (db.inTransaction) db.exec("ROLLBACK");
throw err;
}
};
Expand Down
22 changes: 22 additions & 0 deletions integration-tests/tests/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,28 @@ test.serial("Database.transaction().immediate()", async (t) => {
t.is(db.inTransaction, false);
});

test.serial("Database.transaction() rethrows the error when SQLite already rolled back", async (t) => {
const db = t.context.db;
const insert = await db.prepare(
"INSERT OR ROLLBACK INTO users(id, name, email) VALUES (?, ?, ?)"
);
const insertMany = db.transaction(async (users) => {
for (const user of users) await insert.run(user.id, user.name, user.email);
});
await t.throwsAsync(async () => {
await insertMany([
{ id: 3, name: "Joey", email: "joey@example.org" },
{ id: 1, name: "Sally", email: "sally@example.org" },
]);
}, {
instanceOf: t.context.errorType,
code: "SQLITE_CONSTRAINT_PRIMARYKEY",
});
t.is(db.inTransaction, false);
const stmt = await db.prepare("SELECT * FROM users WHERE id = ?");
t.is(await stmt.get(3), undefined);
});

test.serial("Database.pragma()", async (t) => {
const db = t.context.db;
await db.pragma("cache_size = 2000");
Expand Down
21 changes: 21 additions & 0 deletions integration-tests/tests/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,27 @@ test.serial("Database.transaction().immediate()", async (t) => {
t.is(db.inTransaction, false);
});

test.serial("Database.transaction() rethrows the error when SQLite already rolled back", async (t) => {
const db = t.context.db;
const insert = db.prepare(
"INSERT OR ROLLBACK INTO users(id, name, email) VALUES (?, ?, ?)"
);
const insertMany = db.transaction((users) => {
for (const user of users) insert.run(user.id, user.name, user.email);
});
t.throws(() => {
insertMany([
{ id: 3, name: "Joey", email: "joey@example.org" },
{ id: 1, name: "Sally", email: "sally@example.org" },
]);
}, {
instanceOf: t.context.errorType,
code: "SQLITE_CONSTRAINT_PRIMARYKEY",
});
t.is(db.inTransaction, false);
t.is(db.prepare("SELECT * FROM users WHERE id = ?").get(3), undefined);
});

test.serial("values", async (t) => {
const db = t.context.db;

Expand Down
2 changes: 1 addition & 1 deletion promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Database {
await db.exec("COMMIT");
return result;
} catch (err) {
await db.exec("ROLLBACK");
if (db.inTransaction) await db.exec("ROLLBACK");
throw err;
}
};
Expand Down