Skip to content

Commit ff6750b

Browse files
committed
fix: ensure /app/data ownership on startup to prevent SQLITE_CANTOPEN (#42)
The entrypoint only ran chown when PUID/PGID differed from defaults, but /app/data is a mounted volume with unpredictable ownership. Now chown /app/data runs unconditionally on every startup. Also added mkdir -p for the data directory and a clearer error message when the database cannot be opened.
1 parent 9411b68 commit ff6750b

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

entrypoint.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ PGID=${PGID:-1000}
77
if [ "$(id -u node)" != "$PUID" ] || [ "$(id -g node)" != "$PGID" ]; then
88
groupmod -o -g "$PGID" node
99
usermod -o -u "$PUID" node
10-
chown -R node:node /app /home/node
10+
chown -R node:node /home/node
1111
fi
1212

13+
# Always fix ownership on /app/data — it's a mounted volume with unpredictable ownership
14+
chown -R node:node /app/data
15+
1316
exec gosu node "$@"

src/database.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import Database from 'better-sqlite3';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
24

35
export interface Run {
46
id: string;
@@ -42,7 +44,23 @@ export class SubsyncarrPlusDatabase {
4244
private db: Database.Database;
4345

4446
constructor(dbPath: string) {
45-
this.db = new Database(dbPath);
47+
const dir = path.dirname(dbPath);
48+
try {
49+
fs.mkdirSync(dir, { recursive: true });
50+
} catch {
51+
// ignore — will fail on db open if truly inaccessible
52+
}
53+
try {
54+
this.db = new Database(dbPath);
55+
} catch (err: unknown) {
56+
if (err instanceof Error && 'code' in err && err.code === 'SQLITE_CANTOPEN') {
57+
throw new Error(
58+
`Cannot open database at ${dbPath} — ensure the data directory exists and is writable. ` +
59+
`If using Docker, check that your volume mount target exists and has correct permissions (PUID/PGID).`,
60+
);
61+
}
62+
throw err;
63+
}
4664
this.initSchema();
4765
}
4866

0 commit comments

Comments
 (0)