fix: database should not overwrite

This commit is contained in:
eric
2026-02-23 17:32:19 +01:00
parent 724666ea36
commit 82259117dd
6 changed files with 43 additions and 3 deletions

4
.gitignore vendored
View File

@@ -1,4 +1,6 @@
.env .env
node_modules/ node_modules/
.direnv/ .direnv/
.pre-commit-config.yaml .pre-commit-config.yaml
data/
src/database/db.sqlite3

View File

@@ -32,6 +32,10 @@ COPY --from=builder /app/tsconfig.json ./
# Set environment variables # Set environment variables
ENV NODE_ENV=production ENV NODE_ENV=production
ENV LOG_LEVEL=info ENV LOG_LEVEL=info
ENV DATABASE_PATH=/data/db.sqlite3
# Persist runtime data (SQLite)
VOLUME ["/data"]
# Run the bot # Run the bot
CMD ["bun", "run", "src/index.ts"] CMD ["bun", "run", "src/index.ts"]

View File

@@ -47,10 +47,31 @@ src/
| `ELEVENLABS_API_KEY` | ElevenLabs API key for voiceover | | `ELEVENLABS_API_KEY` | ElevenLabs API key for voiceover |
| `ELEVENLABS_VOICE_ID` | Default ElevenLabs voice ID (optional) | | `ELEVENLABS_VOICE_ID` | Default ElevenLabs voice ID (optional) |
| `ELEVENLABS_MODEL` | ElevenLabs model ID (default: `eleven_multilingual_v2`) | | `ELEVENLABS_MODEL` | ElevenLabs model ID (default: `eleven_multilingual_v2`) |
| `DATABASE_PATH` | SQLite file path (default: `./data/db.sqlite3`) |
| `WEB_PORT` | Port for web dashboard (default: 3000) | | `WEB_PORT` | Port for web dashboard (default: 3000) |
| `WEB_BASE_URL` | Base URL for web dashboard | | `WEB_BASE_URL` | Base URL for web dashboard |
| `SESSION_SECRET` | Secret for session encryption | | `SESSION_SECRET` | Secret for session encryption |
## Data Persistence
- Bot options and other runtime state are stored in SQLite.
- Keep `DATABASE_PATH` on persistent storage so updates/redeploys do not reset settings.
- Backward compatibility: if `DATABASE_PATH` is unset and `src/database/db.sqlite3` exists, it is used automatically.
- Recommended one-time migration:
```bash
mkdir -p data && cp src/database/db.sqlite3 data/db.sqlite3
```
- Docker example:
```bash
docker run -d \
--name joel-discord \
-v joel_data:/data \
--env-file .env \
your-image:latest
```
## Scripts ## Scripts
| Script | Description | | Script | Description |

View File

@@ -5,7 +5,7 @@ export default {
out: "./src/database/drizzle", out: "./src/database/drizzle",
dialect: "turso", dialect: "turso",
dbCredentials: { dbCredentials: {
url: "./src/database/db.sqlite3", url: process.env.DATABASE_PATH ?? "./data/db.sqlite3",
}, },
verbose: true, verbose: true,
strict: true, strict: true,

View File

@@ -4,9 +4,22 @@
import { drizzle } from "drizzle-orm/bun-sqlite"; import { drizzle } from "drizzle-orm/bun-sqlite";
import { Database } from "bun:sqlite"; import { Database } from "bun:sqlite";
import { dirname } from "node:path";
import { existsSync, mkdirSync } from "node:fs";
import * as schema from "./schema"; import * as schema from "./schema";
const DATABASE_PATH = `${import.meta.dir}/db.sqlite3`; const DEFAULT_DATABASE_PATH = "./data/db.sqlite3";
const LEGACY_DATABASE_PATH = `${import.meta.dir}/db.sqlite3`;
const DATABASE_PATH =
Bun.env.DATABASE_PATH ??
(existsSync(DEFAULT_DATABASE_PATH)
? DEFAULT_DATABASE_PATH
: existsSync(LEGACY_DATABASE_PATH)
? LEGACY_DATABASE_PATH
: DEFAULT_DATABASE_PATH);
mkdirSync(dirname(DATABASE_PATH), { recursive: true });
const sqlite = new Database(DATABASE_PATH); const sqlite = new Database(DATABASE_PATH);

Binary file not shown.