Skip to content

Commit b1b5ff1

Browse files
Fix useSql on Windows when the db is locked (#66)
1 parent 526d0c0 commit b1b5ff1

File tree

5 files changed

+161
-482
lines changed

5 files changed

+161
-482
lines changed

docs/utils-reference/getting-started.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ npm install --save @raycast/utils
1616

1717
## Changelog
1818

19+
### v2.2.3
20+
21+
- Fixed an issue with `useSQL` on Windows where the query would refuse to be executed because the database is locked
22+
1923
### v2.2.2
2024

2125
- Fix `useCachedState` to preserve Date objects more precisely.

package-lock.json

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@raycast/utils",
3-
"version": "2.2.2",
3+
"version": "2.2.3",
44
"description": "Set of utilities to streamline building Raycast extensions",
55
"author": "Raycast Technologies Ltd.",
66
"homepage": "https://developers.raycast.com/utilities/getting-started",

src/sql-utils.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ export async function baseExecuteSQL<T = unknown>(
4444

4545
try {
4646
db.open();
47+
48+
const statement = db.prepare(query);
49+
checkAborted(abortSignal);
50+
51+
const result = statement.all();
52+
53+
db.close();
54+
55+
return result as T[];
4756
} catch (error: any) {
48-
console.log(error);
49-
if (error.message.match("(5)") || error.message.match("(14)")) {
57+
if (error.errcode === 5 || error.errcode === 14 || error.message.match("(5)") || error.message.match("(14)")) {
5058
// That means that the DB is busy because of another app is locking it
5159
// This happens when Chrome or Arc is opened: they lock the History db.
5260
// As an ugly workaround, we duplicate the file and read that instead
@@ -69,17 +77,19 @@ export async function baseExecuteSQL<T = unknown>(
6977
db = new sqlite3.DatabaseSync(workaroundCopiedDb, { open: false, readOnly: true });
7078
db.open();
7179
checkAborted(abortSignal);
72-
}
73-
}
7480

75-
const statement = db.prepare(query);
76-
checkAborted(abortSignal);
81+
const statement = db.prepare(query);
82+
checkAborted(abortSignal);
83+
84+
const result = statement.all();
7785

78-
const result = statement.all();
86+
db.close();
7987

80-
db.close();
88+
return result as T[];
89+
}
8190

82-
return result as T[];
91+
throw error;
92+
}
8393
}
8494

8595
async function sqliteFallback<T = unknown>(

0 commit comments

Comments
 (0)