From edf9c14be03b903ab8bc7757474c161391778264 Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Mon, 3 Aug 2026 11:59:44 +0530 Subject: [PATCH] fix: honor DATABASE_URL for the SQLite database location The variable was defined and documented but never read; the database path was hardcoded to /data/gitea-mirror.db. It now accepts sqlite://, the legacy file: scheme, or a plain path, with relative paths resolving against the working directory and the parent directory created on demand. Defaults are unchanged, including the compose files that pass the historical default value through. Docs updated to match. --- src/lib/db/index.ts | 27 ++++++++++++++++---- www/src/pages/docs/configuration.mdx | 2 +- www/src/pages/docs/environment-variables.mdx | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 60f927d..1d1c279 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -8,16 +8,33 @@ import { repairDuplicateSsoColumns, restoreSsoDataAfter0013 } from "./migration- // Skip database initialization in test environment let db: ReturnType; +/** + * Resolve the SQLite file location from DATABASE_URL, falling back to the + * historical default of /data/gitea-mirror.db. Accepts the documented + * sqlite:// scheme, the legacy file: scheme, or a plain path; relative + * paths resolve against the working directory. + */ +function resolveDbPath(): string { + const url = process.env.DATABASE_URL?.trim(); + if (!url) { + return path.join(process.cwd(), "data", "gitea-mirror.db"); + } + const raw = url.startsWith("sqlite://") + ? url.slice("sqlite://".length) + : url.startsWith("file:") + ? url.slice("file:".length) + : url; + return path.resolve(raw); +} + if (process.env.NODE_ENV !== "test") { - // Define the database URL - for development we'll use a local SQLite file - const dataDir = path.join(process.cwd(), "data"); - // Ensure data directory exists + const dbPath = resolveDbPath(); + const dataDir = path.dirname(dbPath); + // Ensure the directory exists if (!fs.existsSync(dataDir)) { fs.mkdirSync(dataDir, { recursive: true }); } - const dbPath = path.join(dataDir, "gitea-mirror.db"); - // Create an empty database file if it doesn't exist if (!fs.existsSync(dbPath)) { fs.writeFileSync(dbPath, ""); diff --git a/www/src/pages/docs/configuration.mdx b/www/src/pages/docs/configuration.mdx index 231d5fd..0bcb3bc 100644 --- a/www/src/pages/docs/configuration.mdx +++ b/www/src/pages/docs/configuration.mdx @@ -108,7 +108,7 @@ These are the ones that matter for nearly every deployment. The [environment var | `BASE_URL` | `/` | Path prefix when the app is served under a subpath, for example `/mirror`. | | `DATABASE_URL` | `sqlite://data/gitea-mirror.db` | SQLite database location. The container image sets `file:data/gitea-mirror.db`. | -> **Note:** The database file is always created at `data/gitea-mirror.db` relative to the process working directory, and the directory is created if it is missing. `DATABASE_URL` is accepted and reported but the SQLite driver does not currently resolve an alternative path from it. To relocate the database, mount or symlink the `data` directory rather than changing this variable. +> **Note:** `DATABASE_URL` accepts the `sqlite://` scheme, the legacy `file:` scheme, or a plain path. Relative paths resolve against the process working directory and the parent directory is created if it is missing. Point it at a mounted volume to relocate the database. ### Authentication and secrets diff --git a/www/src/pages/docs/environment-variables.mdx b/www/src/pages/docs/environment-variables.mdx index 18045dd..261c5f5 100644 --- a/www/src/pages/docs/environment-variables.mdx +++ b/www/src/pages/docs/environment-variables.mdx @@ -30,7 +30,7 @@ Variables fall into two groups. | `BASE_URL` | `/` | Path prefix for subpath deployments, for example `/mirror`. Applied at runtime, so one prebuilt image works under any prefix. | | `DATABASE_URL` | `sqlite://data/gitea-mirror.db` | SQLite database location. The container image sets `file:data/gitea-mirror.db`. | -> **Note:** The SQLite file is always created at `data/gitea-mirror.db` relative to the process working directory, and the `data` directory is created if missing. `DATABASE_URL` is accepted but the driver does not currently resolve an alternative path from it. Relocate the database by mounting or symlinking the `data` directory. +> **Note:** `DATABASE_URL` accepts the `sqlite://` scheme, the legacy `file:` scheme, or a plain path. Relative paths resolve against the process working directory and the parent directory is created if it is missing. Point it at a mounted volume to relocate the database. ## Authentication and secrets