mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-07-06 05:33:00 +08:00
fix: refactor database initialization to use schema.js for SQL statements
This commit is contained in:
@@ -4,6 +4,15 @@ import fs from 'fs';
|
|||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { dirname } from 'path';
|
import { dirname } from 'path';
|
||||||
|
import {
|
||||||
|
APP_CONFIG_TABLE_SQL,
|
||||||
|
USER_NOTIFICATION_PREFERENCES_TABLE_SQL,
|
||||||
|
VAPID_KEYS_TABLE_SQL,
|
||||||
|
PUSH_SUBSCRIPTIONS_TABLE_SQL,
|
||||||
|
SESSION_NAMES_TABLE_SQL,
|
||||||
|
SESSION_NAMES_LOOKUP_INDEX_SQL,
|
||||||
|
DATABASE_SCHEMA_SQL
|
||||||
|
} from './schema.js';
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
@@ -24,7 +33,6 @@ const c = {
|
|||||||
|
|
||||||
// Use DATABASE_PATH environment variable if set, otherwise use default location
|
// Use DATABASE_PATH environment variable if set, otherwise use default location
|
||||||
const DB_PATH = process.env.DATABASE_PATH || path.join(__dirname, 'auth.db');
|
const DB_PATH = process.env.DATABASE_PATH || path.join(__dirname, 'auth.db');
|
||||||
const INIT_SQL_PATH = path.join(__dirname, 'init.sql');
|
|
||||||
|
|
||||||
// Ensure database directory exists if custom path is provided
|
// Ensure database directory exists if custom path is provided
|
||||||
if (process.env.DATABASE_PATH) {
|
if (process.env.DATABASE_PATH) {
|
||||||
@@ -62,11 +70,7 @@ const db = new Database(DB_PATH);
|
|||||||
// app_config must exist before any other module imports (auth.js reads the JWT secret at load time).
|
// app_config must exist before any other module imports (auth.js reads the JWT secret at load time).
|
||||||
// runMigrations() also creates this table, but it runs too late for existing installations
|
// runMigrations() also creates this table, but it runs too late for existing installations
|
||||||
// where auth.js is imported before initializeDatabase() is called.
|
// where auth.js is imported before initializeDatabase() is called.
|
||||||
db.exec(`CREATE TABLE IF NOT EXISTS app_config (
|
db.exec(APP_CONFIG_TABLE_SQL);
|
||||||
key TEXT PRIMARY KEY,
|
|
||||||
value TEXT NOT NULL,
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)`);
|
|
||||||
|
|
||||||
// Show app installation path prominently
|
// Show app installation path prominently
|
||||||
const appInstallPath = path.join(__dirname, '../..');
|
const appInstallPath = path.join(__dirname, '../..');
|
||||||
@@ -100,53 +104,12 @@ const runMigrations = () => {
|
|||||||
db.exec('ALTER TABLE users ADD COLUMN has_completed_onboarding BOOLEAN DEFAULT 0');
|
db.exec('ALTER TABLE users ADD COLUMN has_completed_onboarding BOOLEAN DEFAULT 0');
|
||||||
}
|
}
|
||||||
|
|
||||||
db.exec(`
|
db.exec(USER_NOTIFICATION_PREFERENCES_TABLE_SQL);
|
||||||
CREATE TABLE IF NOT EXISTS user_notification_preferences (
|
db.exec(VAPID_KEYS_TABLE_SQL);
|
||||||
user_id INTEGER PRIMARY KEY,
|
db.exec(PUSH_SUBSCRIPTIONS_TABLE_SQL);
|
||||||
preferences_json TEXT NOT NULL,
|
db.exec(APP_CONFIG_TABLE_SQL);
|
||||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
db.exec(SESSION_NAMES_TABLE_SQL);
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
db.exec(SESSION_NAMES_LOOKUP_INDEX_SQL);
|
||||||
)
|
|
||||||
`);
|
|
||||||
|
|
||||||
db.exec(`
|
|
||||||
CREATE TABLE IF NOT EXISTS vapid_keys (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
public_key TEXT NOT NULL,
|
|
||||||
private_key TEXT NOT NULL,
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)
|
|
||||||
`);
|
|
||||||
|
|
||||||
db.exec(`
|
|
||||||
CREATE TABLE IF NOT EXISTS push_subscriptions (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
user_id INTEGER NOT NULL,
|
|
||||||
endpoint TEXT NOT NULL UNIQUE,
|
|
||||||
keys_p256dh TEXT NOT NULL,
|
|
||||||
keys_auth TEXT NOT NULL,
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
|
||||||
)
|
|
||||||
`);
|
|
||||||
// Create app_config table if it doesn't exist (for existing installations)
|
|
||||||
db.exec(`CREATE TABLE IF NOT EXISTS app_config (
|
|
||||||
key TEXT PRIMARY KEY,
|
|
||||||
value TEXT NOT NULL,
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)`);
|
|
||||||
|
|
||||||
// Create session_names table if it doesn't exist (for existing installations)
|
|
||||||
db.exec(`CREATE TABLE IF NOT EXISTS session_names (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
session_id TEXT NOT NULL,
|
|
||||||
provider TEXT NOT NULL DEFAULT 'claude',
|
|
||||||
custom_name TEXT NOT NULL,
|
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
UNIQUE(session_id, provider)
|
|
||||||
)`);
|
|
||||||
db.exec('CREATE INDEX IF NOT EXISTS idx_session_names_lookup ON session_names(session_id, provider)');
|
|
||||||
|
|
||||||
console.log('Database migrations completed successfully');
|
console.log('Database migrations completed successfully');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -158,8 +121,7 @@ const runMigrations = () => {
|
|||||||
// Initialize database with schema
|
// Initialize database with schema
|
||||||
const initializeDatabase = async () => {
|
const initializeDatabase = async () => {
|
||||||
try {
|
try {
|
||||||
const initSQL = fs.readFileSync(INIT_SQL_PATH, 'utf8');
|
db.exec(DATABASE_SCHEMA_SQL);
|
||||||
db.exec(initSQL);
|
|
||||||
console.log('Database initialized successfully');
|
console.log('Database initialized successfully');
|
||||||
runMigrations();
|
runMigrations();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user