Behebe SQLite-Bindungsprobleme für produktiven Einsatz

- Verbessere SQLite-Konfiguration mit optimalen Performance-Einstellungen
- Erweitere Dockerfile mit umfassenden Abhängigkeiten für zuverlässige SQLite-Kompilierung
- Füge native Buildflag-Optionen und Umgebungsvariablen für ARM-Kompatibilität hinzu
- Optimiere Startup-Skript mit zuverlässigerer Datenbank-Initialisierung
- Setze korrekte Berechtigungen für Datenbank-Verzeichnis und -Dateien
- Entferne fehlerträchtige Mock-Implementierung zugunsten einer robusten Produktiv-Lösung

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Till Tomczak 2025-04-01 09:41:51 +02:00
parent 7eabb59b35
commit 4a994c3bf8
2 changed files with 48 additions and 37 deletions
packages/reservation-platform
Dockerfile
src/server/db

@ -2,8 +2,8 @@ FROM node:20-alpine
WORKDIR /app
# Install system dependencies
RUN apk add --no-cache python3 build-base g++ make sqlite sqlite-dev
# Install system dependencies for SQLite and native modules
RUN apk add --no-cache python3 build-base g++ make sqlite sqlite-dev gcc musl-dev git libffi-dev openssl-dev cmake
# Install pnpm
RUN npm install -g pnpm
@ -11,10 +11,20 @@ RUN npm install -g pnpm
# Copy package files
COPY package.json pnpm-lock.yaml ./
# Install dependencies with native bindings build approval
# Install dependencies with native bindings build approval, ensuring to build from source for all platforms
ENV CFLAGS="-fPIC" \
LDFLAGS="-fPIC" \
CXXFLAGS="-fPIC" \
npm_config_build_from_source=true \
npm_config_sqlite=/usr/local \
npm_config_sqlite_libname=sqlite3
# Durchführen der Installation mit umfassenden Flags für native Bindungen
RUN pnpm install --unsafe-perm --no-optional --frozen-lockfile
# Rebuild better-sqlite3 with explicit platform options for ARM compatibility
RUN npm_config_build_from_source=true pnpm rebuild better-sqlite3
# Explizit better-sqlite3 für die aktuelle Plattform neu bauen
RUN cd /app/node_modules/better-sqlite3 && \
npm run build-release
# Install tsx for running TypeScript files directly
RUN pnpm add -D tsx
@ -36,20 +46,26 @@ RUN echo '#!/bin/sh' > /app/startup.sh && \
echo 'set -e' >> /app/startup.sh && \
echo 'mkdir -p /app/db' >> /app/startup.sh && \
echo 'echo "Starting application..."' >> /app/startup.sh && \
echo 'echo "Attempting database setup..."' >> /app/startup.sh && \
echo 'echo "Konfiguriere DB-Verzeichnis..."' >> /app/startup.sh && \
echo 'DB_FILE="/app/db/sqlite.db"' >> /app/startup.sh && \
echo 'if [ ! -f "$DB_FILE" ]; then' >> /app/startup.sh && \
echo ' echo "Creating empty database file..."' >> /app/startup.sh && \
echo ' touch "$DB_FILE"' >> /app/startup.sh && \
echo ' chmod 666 "$DB_FILE"' >> /app/startup.sh && \
echo 'fi' >> /app/startup.sh && \
echo 'echo "Rebuilding native bindings..."' >> /app/startup.sh && \
echo 'npm_config_build_from_source=true pnpm rebuild better-sqlite3 || echo "Rebuild failed but continuing..."' >> /app/startup.sh && \
echo 'echo "Attempting database migration..."' >> /app/startup.sh && \
echo 'NODE_ENV=production npx tsx ./src/server/db/migrate.ts || echo "Migration failed but continuing..."' >> /app/startup.sh && \
echo 'echo "Migration completed or skipped"' >> /app/startup.sh && \
echo 'pnpm build || echo "Build failed, but continuing with existing build..."' >> /app/startup.sh && \
echo 'pnpm start || pnpm dev' >> /app/startup.sh && \
echo 'chmod 666 "$DB_FILE"' >> /app/startup.sh && \
echo 'chmod -R 777 /app/db' >> /app/startup.sh && \
echo 'export DB_PATH=$DB_FILE' >> /app/startup.sh && \
echo 'echo "Datenbank wird unter $DB_PATH verwendet"' >> /app/startup.sh && \
echo 'echo "Führe Datenbank-Migration aus..."' >> /app/startup.sh && \
echo 'NODE_ENV=production npx tsx ./src/server/db/migrate.ts' >> /app/startup.sh && \
echo 'echo "Migration abgeschlossen"' >> /app/startup.sh && \
echo 'echo "Starte Next.js Anwendung..."' >> /app/startup.sh && \
echo 'if [ -d ".next" ]; then' >> /app/startup.sh && \
echo ' pnpm start' >> /app/startup.sh && \
echo 'else' >> /app/startup.sh && \
echo ' echo "Build directory nicht gefunden, führe Build aus..."' >> /app/startup.sh && \
echo ' pnpm build && pnpm start' >> /app/startup.sh && \
echo 'fi' >> /app/startup.sh && \
chmod +x /app/startup.sh
# Start the application

@ -1,28 +1,23 @@
import { env } from "@/utils/env";
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "@/server/db/schema";
// Wrap database initialization in try-catch to handle native binding errors
let sqlite;
let db;
// Stellen sicher, dass DB_PATH tatsächlich gesetzt ist
const dbPath = env.DB_PATH || "/app/db/sqlite.db";
try {
// Dynamically import better-sqlite3 to handle binding errors more gracefully
const Database = require("better-sqlite3");
sqlite = new Database(env.DB_PATH);
const { drizzle } = require("drizzle-orm/better-sqlite3");
db = drizzle(sqlite, { schema });
} catch (error) {
console.error("Failed to initialize database:", error);
// Create mock database for SSR to prevent crashes
db = {
query: () => [],
select: () => ({ get: () => null, all: () => [] }),
insert: () => ({ values: () => ({ returning: () => [] }) }),
update: () => ({ set: () => ({ where: () => [] }) }),
delete: () => ({ where: () => [] }),
};
}
// Konfiguriere SQLite für zuverlässigeren Betrieb
const sqlite = new Database(dbPath, {
// Setze längeres Timeout für Operationen auf langsamen Geräten (RPi)
timeout: 30000,
// Aktiviere WAL-Modus für höhere Performance
journalMode: 'wal',
// Verbesserte Fehlerbehandlung
verbose: console.error,
});
export { db };
// Aktiviere Fremdschlüssel-Constraints
sqlite.pragma('foreign_keys = ON');
// Exportiere die Drizzle-Datenbankinstanz
export const db = drizzle(sqlite, { schema });